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 |
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. |
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 |
Looks like didn't work for me :(
Its not throwing any error but also didnt files/folders which are older than 2 days. |
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 |
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) |
You asked for:
Quote:
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. |
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. |
Earlier you asked to delete Folders - now you want files. To change to files, use -type f instead of -type d.
|
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. |
My command works for me.
Code:
$ ll |
your problem is that you are running echo xargs /bin/rm -rf whereas you should be running xargs echo /bin/rm -rf
|
Quote:
Isays find . -type f -name < > -mtime +2 print | xargs echo /bin/rm -rf You typed echo xargs /bin/rm -rf |
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..... |
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 |
All times are GMT -5. The time now is 12:58 AM. |