LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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
 
LinkBack Search this Thread
Old 07-09-2008, 11:38 PM   #1
siddhartha_ece2004
LQ Newbie
 
Registered: Jul 2008
Posts: 14

Rep: Reputation: 0
Automated script to DELETE files older than 2 days in a Particular folder


Hi,

I am a newbie on Linux environment and trying to learn shell scripting to implement some automated job.

I have a server which runs on DEBIAN linux.
On that server some nightly builds (folder) are getting downloaded everyday in a particular folder.
I want to DELETE builds which are older than 2 days.
Also on this particular folder there are many other files which I really dont want to delete.
So, I need to do some conditional deletion where the script will delete ONLY those folders which are named with 'QA_Builds_XXX'. This XXX will be the variable tag no. as per daily builds come.

So the CONDITION is:

Automated Deletion of Folders which are named as 'QA_Builds_<any tag no>' and older than 2 days.

Hope my description of the prolem makes sense.

Any suggession APPRICIATED....

Thanks
 
Old 07-09-2008, 11:46 PM   #2
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
find PATH -type d -name QA_Builds_'*' -mtime +2 -print | xargs echo /bin/rm -rf

replace PATH with the correct path to the base of your builds.
remove echo after you have verified it looks like it is doing the right thing. It will then delete.
 
Old 07-10-2008, 06:43 AM   #3
siddhartha_ece2004
LQ Newbie
 
Registered: Jul 2008
Posts: 14

Original Poster
Rep: Reputation: 0
Well ...thanks a lot for your suggestion. Two things i want to know:

1. What is the purpose of 'echo' on that mentioned command?
2. Why do I need to remove it?

Thanks
 
Old 07-10-2008, 07:59 AM   #4
siddhartha_ece2004
LQ Newbie
 
Registered: Jul 2008
Posts: 14

Original Poster
Rep: Reputation: 0
Looks like didn't work for me
Its not throwing any error but also didnt files/folders which are older than 2 days.
 
Old 07-10-2008, 12:23 PM   #5
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
I told you what the echo did. It prevents the removal of the files, so that you can verify that it would operate correctly *after* removal of echo.

If it didn't work for you, show the command you ran, and its output (exactly as they are), and show the directory contents.

The command works fine:

Code:
$ ll
total 12K
drwx------ 2 MrC users 4.0K Jul  1 00:00 QA_Builds_1012/
drwx------ 2 MrC users 4.0K Jul  1 00:00 QA_Builds_99/
-rw------- 1 MrC users   55 Jul  9 21:49 in
-rw------- 1 MrC users    0 Jul  9 21:54 newfilename

$ find . -type d -name QA_Builds_'*' -mtime +2 -print | xargs echo /bin/rm -rf
/bin/rm -rf ./QA_Builds_99 ./QA_Builds_1012

$ ll
total 12K
drwx------ 2 MrC users 4.0K Jul  1 00:00 QA_Builds_1012/
drwx------ 2 MrC users 4.0K Jul  1 00:00 QA_Builds_99/
-rw------- 1 MrC users   55 Jul  9 21:49 in
-rw------- 1 MrC users    0 Jul  9 21:54 newfilename

$ find . -type d -name QA_Builds_'*' -mtime +2 -print | xargs  /bin/rm -rf
$ ll
total 4.0K
-rw------- 1 MrC users 55 Jul  9 21:49 in
-rw------- 1 MrC users  0 Jul  9 21:54 newfilename
If it doesn't work in your case, there is either something wrong with what you did, or there are details you've left out.
 
Old 07-10-2008, 11:31 PM   #6
siddhartha_ece2004
LQ Newbie
 
Registered: Jul 2008
Posts: 14

Original Poster
Rep: Reputation: 0
Well...but its really didnt work for me. See this:

qabng29a:/tmp# ls -la
total 28
drwxrwxrwt 7 root root 4096 2008-07-11 09:47 .
drwxr-xr-x 24 root root 4096 2008-06-17 01:48 ..
drwxrwxrwt 2 root root 4096 2008-06-16 18:17 .ICE-unix
drwxr-xr-x 2 root root 4096 2008-07-11 09:47 sir_1001
drwxr-xr-x 2 root root 4096 2008-07-11 09:47 sir_1002
drwxr-xr-x 2 root root 4096 2008-07-11 09:47 sir_1003
drwxrwxrwt 2 root root 4096 2008-06-16 18:17 .X11-unix
qabng29a:/tmp# find . -type d -name sir_'*' -mtime +2 -print | echo xargs /bin/rm -rf
xargs /bin/rm -rf --> it returns this output.
qabng29a:/tmp#

It never show the folders which need to be deleted.

Note: I named those folder as sir_'*'

Also my understanding is the syntax -mtime +2 deletes those folders which are older than 2 days from today's date. Then how can you delete files/folders created on the same day? (I mean to say the example you have shown)
 
Old 07-11-2008, 12:22 AM   #7
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
You asked for:

Quote:
Automated Deletion of Folders which are named as 'QA_Builds_<any tag no>' and older than 2 days.
The command I gave you accomplishes that. If you want to change the criteria, that's your business. But you can't expect the command to read your mind.

The directories you show are only a few hours old. You asked for > 2 days. Change the 2 into a 1 if you want 1 day old. Change it to a 7 if you want 7 days old. Frankly, I'm not sure what you want.
 
Old 07-11-2008, 02:29 AM   #8
siddhartha_ece2004
LQ Newbie
 
Registered: Jul 2008
Posts: 14

Original Poster
Rep: Reputation: 0
Well....... I hope my this example will make sense to you. Please have a look:

qabng29a:/localdisk# ls -lrt
total 40
drwxrwsr-x 4 root software 4096 2006-06-07 20:25 buildcache
-rw-r--r-- 1 root software 20 2008-07-02 15:55 usage_report.2008-07-02.gz
-rw-r--r-- 1 root software 20 2008-07-03 15:55 usage_report.2008-07-03.gz
-rw-r--r-- 1 root software 20 2008-07-04 15:55 usage_report.2008-07-04.gz
-rw-r--r-- 1 root software 20 2008-07-05 15:55 usage_report.2008-07-05.gz
-rw-r--r-- 1 root software 20 2008-07-06 15:55 usage_report.2008-07-06.gz
-rw-r--r-- 1 root software 20 2008-07-07 15:55 usage_report.2008-07-07.gz
-rw-r--r-- 1 root software 20 2008-07-08 15:55 usage_report.2008-07-08.gz
-rw-r--r-- 1 root software 20 2008-07-09 15:55 usage_report.2008-07-09.gz
lrwxrwxrwx 1 root software 26 2008-07-10 15:55 usage_report.gz -> usage_report.2008-07-10.gz
-rw-r--r-- 1 root software 20 2008-07-10 15:55 usage_report.2008-07-10.gz
qabng29a:/localdisk# find . -type d -name usage_'*' -mtime +2 -print | echo xargs /bin/rm -rf
xargs /bin/rm -rf
qabng29a:/localdisk#

Here its just returns xargs /bin/rm -rf nothing else. In this example those files are created in older dates but still ur script couldnt able to DELETE it.

Please let me know if it still doesnt make sense to you....

Thanks for your help and patience.
 
Old 07-11-2008, 02:45 AM   #9
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
Earlier you asked to delete Folders - now you want files. To change to files, use -type f instead of -type d.
 
Old 07-11-2008, 03:59 AM   #10
siddhartha_ece2004
LQ Newbie
 
Registered: Jul 2008
Posts: 14

Original Poster
Rep: Reputation: 0
NO GO....

qabng29a:/localdisk# ls -lrt
total 40
drwxrwsr-x 4 root software 4096 2006-06-07 20:25 buildcache
-rw-r--r-- 1 root software 20 2008-07-02 15:55 usage_report.2008-07-02.gz
-rw-r--r-- 1 root software 20 2008-07-03 15:55 usage_report.2008-07-03.gz
-rw-r--r-- 1 root software 20 2008-07-04 15:55 usage_report.2008-07-04.gz
-rw-r--r-- 1 root software 20 2008-07-05 15:55 usage_report.2008-07-05.gz
-rw-r--r-- 1 root software 20 2008-07-06 15:55 usage_report.2008-07-06.gz
-rw-r--r-- 1 root software 20 2008-07-07 15:55 usage_report.2008-07-07.gz
-rw-r--r-- 1 root software 20 2008-07-08 15:55 usage_report.2008-07-08.gz
-rw-r--r-- 1 root software 20 2008-07-09 15:55 usage_report.2008-07-09.gz
lrwxrwxrwx 1 root software 26 2008-07-10 15:55 usage_report.gz -> usage_report.2008-07-10.gz
-rw-r--r-- 1 root software 20 2008-07-10 15:55 usage_report.2008-07-10.gz
qabng29a:/localdisk# find . -type f -name usage_'*' -mtime +2 -print | echo xargs /bin/rm -rf
xargs /bin/rm -rf
qabng29a:/localdisk#

Note: I am using DEBIAN Linux. But, I guess it doesn't matter.
 
Old 07-11-2008, 04:12 AM   #11
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
My command works for me.

Code:
$ ll
total 0
-rw------- 1 mrc users 0 Jul  1 00:00 usage_report.2008-07-01.gz
-rw------- 1 mrc users 0 Jul  2 00:00 usage_report.2008-07-02.gz
-rw------- 1 mrc users 0 Jul  3 00:00 usage_report.2008-07-03.gz
-rw------- 1 mrc users 0 Jul  4 00:00 usage_report.2008-07-04.gz
-rw------- 1 mrc users 0 Jul 10 00:00 usage_report.2008-07-10.gz
-rw------- 1 mrc users 0 Jul 11 00:00 usage_report.2008-07-11.gz
find . -type f -name usage_'*' -mtime +2 -print |  xargs echo /bin/rm -rf
/bin/rm -rf ./usage_report.2008-07-02.gz ./usage_report.2008-07-01.gz \
  ./usage_report.2008-07-03.gz ./usage_report.2008-07-04.gz
So why doesn't it work for you? Compare your line with my line VERY carefully.

Last edited by Mr. C.; 07-11-2008 at 04:14 AM.
 
Old 07-11-2008, 04:14 AM   #12
a_m0d
Member
 
Registered: Apr 2007
Location: Perth, WA, Australia
Distribution: Fedora 8, Fedora 10
Posts: 49

Rep: Reputation: 15
your problem is that you are running echo xargs /bin/rm -rf whereas you should be running xargs echo /bin/rm -rf
 
Old 07-11-2008, 04:21 AM   #13
linuxlover.chaitanya
Senior Member
 
Registered: Apr 2008
Location: Nagpur, India
Distribution: Cent OS 5/6, Ubuntu Server 10.04
Posts: 4,615

Rep: Reputation: Disabled
Quote:
Originally Posted by siddhartha_ece2004 View Post
NO GO....


qabng29a:/localdisk# find . -type f -name usage_'*' -mtime +2 -print | echo xargs /bin/rm -rf
xargs /bin/rm -rf
qabng29a:/localdisk#

.
Look at the command Mr.C gave you.
Isays

find . -type f -name < > -mtime +2 print | xargs echo /bin/rm -rf

You typed echo xargs /bin/rm -rf
 
Old 07-11-2008, 04:26 AM   #14
siddhartha_ece2004
LQ Newbie
 
Registered: Jul 2008
Posts: 14

Original Poster
Rep: Reputation: 0
ooops.... I got the point.

I was typing find . -type f -name usage_'*' -mtime +2 -print | echo xargs /bin/rm -rf

instead of

find . -type f -name usage_'*' -mtime +2 -print | xargs echo /bin/rm -rf

I am extremely sorry for this confusion and I apologize for this trouble.
Thanks a ton for all of your help.....

I will bug you again if I stuck again with something.....
 
Old 07-11-2008, 05:46 AM   #15
siddhartha_ece2004
LQ Newbie
 
Registered: Jul 2008
Posts: 14

Original Poster
Rep: Reputation: 0
I have tested in Live environment and it worked exactly what i was looking for....

Thanks all of you for providing the right support.

Cheers,
Sid
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Script help - delete files older than 45 days but exclude the system files jojothedogboy Linux - Software 3 06-13-2008 03:43 PM
bash: delete files older than 7 days in a folder and subfoldes lupomanaro Programming 1 05-07-2008 10:22 AM
Delete files older then 30 days stefaandk *BSD 1 01-07-2008 08:31 PM
script to auto delete files older than X days nocnoc Programming 17 12-06-2006 08:30 AM
delete files older than 30 days using cronjob latheesan Linux - Newbie 5 06-14-2005 02:40 PM


All times are GMT -5. The time now is 11:03 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration