Shell Script to delete oldest folders in particualr directory
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Shell Script to delete oldest folders in particualr directory
Hi Guys,
I wanted shell script to delete 30 days oldest folder from a path /jenkins/build/ and in it there are 4 folders with names as(1,2,3,4) and also there some other old folders too but i have to write a condition were it will check this 1,2,3,4 and will delete the oldest of this 4 folders only without touching any other old folders.
I think you want find, and a for loop. look up the man page on find.
A note of advice. The folks on this forum value when people make sincere attempts to figure it out on their own without instantly asking for the how to. Once you get something going, post it and they will help debug and offer suggestions. They generally will not write your scripts for you, unless of course it is a convoluted thing and they just love the challenge of it.
Last edited by jmgibson1981; 02-04-2017 at 05:32 AM.
I have prepared the below command which will delete a directory 30 day old.
Now problem is this will delete all the things in the path /jenkins/build/job which are 30 days old and I want condition to put here like.
In this path /jenkins/build/job there are 1,2,3,4 directory i have to delete 30 days old from it and also retain a copy of it too, if all of the folders (1,2,3,4) are like 30 days old then as per there modification time delete 2 of them and maintain a copy.
I am not good at scripting please help me on this issue with your ideas.
Thanks For the reply Turbocapitalist, main issue is that there are many files on this location and i have to only delete folder 1,2,3,4 which are 30 days oldest and keep a copy from it.
This much is correct find /jenkins/build/job -type d -mtime +30 -delete
But how to put this conditions ?
[root@localhost job]# pwd
/jenkins/build/jo
[root@localhost job]# ls -ltr
total 44
drwxr-xr-x 2 root root 4096 Feb 4 14:33 4
drwxr-xr-x 2 root root 4096 Feb 4 14:33 3
drwxr-xr-x 2 root root 4096 Feb 4 14:33 2
drwxr-xr-x 2 root root 4096 Feb 4 14:33 1
-rw-r--r-- 1 root root 0 Feb 4 14:34 file2
-rw-r--r-- 1 root root 0 Feb 4 14:34 file1
-rw-r--r-- 1 root root 0 Feb 4 14:34 conf
[root@localhost job]#
find /jenkins/build/job -type d -name 1 -mtime +30 -delete
Thanks For the reply Turbocapitalist, main issue is that there are many files on this location and i have to only delete folder 1,2,3,4 which are 30 days oldest and keep a copy from it. This much is correct find /jenkins/build/job -type d -mtime +30 -delete But how to put this conditions ?
find /jenkins/build/job -type d -name 1 -mtime +30 -delete
Again, have you read the man page on the find command??? And can you post what YOU have done/tried/written on your own to solve your problem? We're happy to help you, but you have to show effort of your own first. You've been here several years now; you've read the "Question Guidelines" right?
@Turbocapitalist, find ... -exec rm -rf {} \; is safe regarding special filenames, and quotes are not needed. There is no shell in between, and filenames are prefixed with a path.
However, the recursive rm is a problem because it happily deletes the whole tree. Generally, because find is recursive, you should not have another recursive command in it.
--
A find ... -delete does partial deletes; where I understand the o/p wants all or nothing.
Maybe the solution here is to find the young files first, and if none was found do the rm -rf
Last edited by MadeInGermany; 02-05-2017 at 01:44 AM.
The page you linked to https://www.dwheeler.com/essays/filenames-in-shell.html also doesn't give examples which make assume find in general wouldn't be able to handle such problems. The wrong usage of find is which does it, and those examples are given there.
The page you linked to https://www.dwheeler.com/essays/filenames-in-shell.html also doesn't give examples which make assume find in general wouldn't be able to handle such problems. The wrong usage of find is which does it, and those examples are given there.
It's not intended as a tutorial. It's there to point out the existing pitfalls and to call for changes in implementation.
I need something like this were i will search all folders (1,2,3,4) and will delete the folder which is 30 days old.
But if all of the four are 30 days old then latest among them, this my condition
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.