LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Automated script to DELETE files older than 2 days in a Particular folder (https://www.linuxquestions.org/questions/linux-newbie-8/automated-script-to-delete-files-older-than-2-days-in-a-particular-folder-654754/)

siddhartha_ece2004 07-09-2008 11:38 PM

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

Mr. C. 07-09-2008 11:46 PM

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.

siddhartha_ece2004 07-10-2008 06:43 AM

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

siddhartha_ece2004 07-10-2008 07:59 AM

Looks like didn't work for me :(
Its not throwing any error but also didnt files/folders which are older than 2 days.

Mr. C. 07-10-2008 12:23 PM

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.

siddhartha_ece2004 07-10-2008 11:31 PM

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)

Mr. C. 07-11-2008 12:22 AM

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.

siddhartha_ece2004 07-11-2008 02:29 AM

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.

Mr. C. 07-11-2008 02:45 AM

Earlier you asked to delete Folders - now you want files. To change to files, use -type f instead of -type d.

siddhartha_ece2004 07-11-2008 03:59 AM

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.

Mr. C. 07-11-2008 04:12 AM

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.

a_m0d 07-11-2008 04:14 AM

your problem is that you are running echo xargs /bin/rm -rf whereas you should be running xargs echo /bin/rm -rf

linuxlover.chaitanya 07-11-2008 04:21 AM

Quote:

Originally Posted by siddhartha_ece2004 (Post 3210970)
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

siddhartha_ece2004 07-11-2008 04:26 AM

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.....

siddhartha_ece2004 07-11-2008 05:46 AM

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 02:44 PM.