LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 02-04-2017, 05:17 AM   #1
prathamesh7478
Member
 
Registered: Feb 2013
Location: Pune
Distribution: Unix,Solaris,Ubuntu.
Posts: 35

Rep: Reputation: Disabled
Red face 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
 
Old 02-04-2017, 05:29 AM   #2
jmgibson1981
Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 678

Rep: Reputation: Disabled
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.
 
Old 02-04-2017, 06:08 AM   #3
prathamesh7478
Member
 
Registered: Feb 2013
Location: Pune
Distribution: Unix,Solaris,Ubuntu.
Posts: 35

Original Poster
Rep: Reputation: Disabled
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
 
Old 02-04-2017, 07:46 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,676
Blog Entries: 3

Rep: Reputation: 2908Reputation: 2908Reputation: 2908Reputation: 2908Reputation: 2908Reputation: 2908Reputation: 2908Reputation: 2908Reputation: 2908Reputation: 2908Reputation: 2908
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.
 
Old 02-04-2017, 08:11 AM   #5
prathamesh7478
Member
 
Registered: Feb 2013
Location: Pune
Distribution: Unix,Solaris,Ubuntu.
Posts: 35

Original Poster
Rep: Reputation: Disabled
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
 
Old 02-04-2017, 09:32 AM   #6
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 23,944

Rep: Reputation: 7028Reputation: 7028Reputation: 7028Reputation: 7028Reputation: 7028Reputation: 7028Reputation: 7028Reputation: 7028Reputation: 7028Reputation: 7028Reputation: 7028
Quote:
Originally Posted by prathamesh7478 View Post
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?
 
Old 02-05-2017, 01:43 AM   #7
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,768

Rep: Reputation: 797Reputation: 797Reputation: 797Reputation: 797Reputation: 797Reputation: 797Reputation: 797
@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.
Old 02-05-2017, 02:34 AM   #8
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,676
Blog Entries: 3

Rep: Reputation: 2908Reputation: 2908Reputation: 2908Reputation: 2908Reputation: 2908Reputation: 2908Reputation: 2908Reputation: 2908Reputation: 2908Reputation: 2908Reputation: 2908
Quote:
Originally Posted by MadeInGermany View Post
@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
 
Old 02-05-2017, 03:48 AM   #9
nodir
Member
 
Registered: May 2016
Posts: 222

Rep: Reputation: Disabled
Quote:
it looks like
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.
 
Old 02-05-2017, 04:03 AM   #10
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,676
Blog Entries: 3

Rep: Reputation: 2908Reputation: 2908Reputation: 2908Reputation: 2908Reputation: 2908Reputation: 2908Reputation: 2908Reputation: 2908Reputation: 2908Reputation: 2908Reputation: 2908
Quote:
Originally Posted by nodir View Post
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.
 
Old 02-05-2017, 05:34 AM   #11
nodir
Member
 
Registered: May 2016
Posts: 222

Rep: Reputation: Disabled
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.
 
Old 02-06-2017, 12:35 AM   #12
prathamesh7478
Member
 
Registered: Feb 2013
Location: Pune
Distribution: Unix,Solaris,Ubuntu.
Posts: 35

Original Poster
Rep: Reputation: Disabled
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
 
Old 02-06-2017, 01:18 AM   #13
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,622

Rep: Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610
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.
Old 02-06-2017, 03:28 AM   #14
prathamesh7478
Member
 
Registered: Feb 2013
Location: Pune
Distribution: Unix,Solaris,Ubuntu.
Posts: 35

Original Poster
Rep: Reputation: Disabled
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"
 
Old 02-06-2017, 03:32 AM   #15
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,622

Rep: Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script for deleting the oldest directory within a directory kauuttt Linux - Newbie 6 07-14-2013 05:11 AM
Shell script to delete folders and files dynamically and recursively rjbaca Linux - General 1 06-21-2010 11:26 AM
Shell script/cmd to remove old folders in a directory? exxoid Linux - Newbie 1 03-29-2009 01:38 PM
shell script: delete all directories named directory.# except directory.N brian0918 Programming 3 07-13-2005 06:54 PM
Auto Delete oldest files @ratex when directory is 98% full jmanjohn61 Linux - Software 1 04-05-2005 03:44 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration