Linux - Newbie This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
|
|
02-04-2017, 05:17 AM
|
#1
|
Member
Registered: Feb 2013
Location: Pune
Distribution: Unix,Solaris,Ubuntu.
Posts: 35
Rep:
|
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.
Can you guys help me on this
|
|
|
02-04-2017, 05:29 AM
|
#2
|
Senior Member
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 1,185
|
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.
|
|
|
02-04-2017, 06:08 AM
|
#3
|
Member
Registered: Feb 2013
Location: Pune
Distribution: Unix,Solaris,Ubuntu.
Posts: 35
Original Poster
Rep:
|
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.
find /jenkins/build/job -type d -name 1 -mtime +30 -exec rm -rf {} \;
Example of the issue
[root@prathamfrontend job]# pwd
/jenkins/build/job
[root@prathamfrontend job]# ls -ltr
total 20
drwxr-xr-x 2 root root 4096 Feb 3 06:38 setup
drwxr-xr-x 2 root root 4096 Feb 3 06:38 4
drwxr-xr-x 2 root root 4096 Feb 3 06:38 3
drwxr-xr-x 2 root root 4096 Feb 3 06:38 2
drwxr-xr-x 2 root root 4096 Feb 4 03:58 1
[root@prathamfrontend job]#
Just did a -1 to check if its deleting all folders
[root@prathamfrontend job]# find /jenkins/build/job -type d -mtime -1 -exec rm -rf {} \;
find: `/jenkins/build/job': No such file or directory
[root@prathamfrontend job]# ll
total 0
[root@prathamfrontend job]#
Thanks,
Prathamesh
|
|
|
02-04-2017, 07:46 AM
|
#4
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,506
|
Code:
find /jenkins/build/job -type d -name 1 -mtime +30 -exec rm -rf {} \;
Several things, read the manual page for rm.
[/code]
Code:
find /jenkins/build/job -type d -name 1 -mtime +30 -exec rm -rf "{}" \;
Even then there are still a lot of things that can go wrong with file names. So I'd recommend using the -delete option where available.
Code:
find /jenkins/build/job -type d -name 1 -mtime +30 -delete
However, I'd use -print first to make sure you are selecting what you want.
Last edited by Turbocapitalist; 02-04-2017 at 07:47 AM.
|
|
|
02-04-2017, 08:11 AM
|
#5
|
Member
Registered: Feb 2013
Location: Pune
Distribution: Unix,Solaris,Ubuntu.
Posts: 35
Original Poster
Rep:
|
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
|
|
|
02-04-2017, 09:32 AM
|
#6
|
LQ Guru
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,145
|
Quote:
Originally Posted by prathamesh7478
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?
|
|
|
02-05-2017, 01:43 AM
|
#7
|
Senior Member
Registered: Dec 2011
Location: Simplicity
Posts: 2,917
|
@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.
|
|
3 members found this post helpful.
|
02-05-2017, 02:34 AM
|
#8
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,506
|
Quote:
Originally Posted by MadeInGermany
@Turbocapitalist,
find ... -exec rm -rf {} \; is safe regarding special filenames, and quotes are not needed
|
Thanks. My misunderstanding on the presence of a shell. I was thinking too much about shell variables.
It looks like you're right on that:
Code:
$ touch "a b"
$ mkdir a b
$ ls
a a b b
$ find . -type f -exec rm -rf {} \;
$ ls
a b
|
|
|
02-05-2017, 03:48 AM
|
#9
|
Member
Registered: May 2016
Posts: 222
Rep:
|
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.
|
|
|
02-05-2017, 04:03 AM
|
#10
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,506
|
Quote:
Originally Posted by nodir
|
It's not intended as a tutorial. It's there to point out the existing pitfalls and to call for changes in implementation.
|
|
|
02-05-2017, 05:34 AM
|
#11
|
Member
Registered: May 2016
Posts: 222
Rep:
|
didn't i say that?
None of the pitfalls there cover the usage of find here, the one you pointed out to contain problems.
Last edited by nodir; 02-05-2017 at 05:36 AM.
|
|
|
02-06-2017, 12:35 AM
|
#12
|
Member
Registered: Feb 2013
Location: Pune
Distribution: Unix,Solaris,Ubuntu.
Posts: 35
Original Poster
Rep:
|
HI jmgibson1981 i have gone through all the find pages and could make some of this commands but they are not helping much
find /home/vk/* -type d -exec stat --format '%Y :%y %n' "{}" \; | sort -n | awk '{print $5}' | head -1 | xargs rm -rf
|
|
|
02-06-2017, 01:18 AM
|
#13
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,702
|
if you have a restriction on filenames (or dirnames) you need to check the -name option of find. Also you may need either the -or or -and operator.
|
|
1 members found this post helpful.
|
02-06-2017, 03:28 AM
|
#14
|
Member
Registered: Feb 2013
Location: Pune
Distribution: Unix,Solaris,Ubuntu.
Posts: 35
Original Poster
Rep:
|
Thank Pan64
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
Guys could you add your views and let me know
find . -type d -mtime +30 -name "1" -o -name "2" -o -name "3" -o -name "4"
|
|
|
02-06-2017, 03:32 AM
|
#15
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,702
|
Quote:
But if all of the four are 30 days old then latest among them, this my condition
|
this cannot be done with a single find command, you need to combine it with the commands in post #12
|
|
|
All times are GMT -5. The time now is 07:46 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|