1752780 Members
6220 Online
108789 Solutions
New Discussion юеВ

Re: Deletion of files

 
SOLVED
Go to solution
rhansen
Frequent Advisor

Deletion of files

Hello,
The task is to delete any files under directory /home/hansen/download which are older than 210 days.

Can someone help me with a script for automating this?

Thanks in advance.
4 REPLIES 4
Fabian Brise├▒o
Esteemed Contributor
Solution

Re: Deletion of files

Hello hansen.
Test the following command out in some unimportant folder first.

Hope it helps.

find /home/hansen/download/* -mtime +210 -exec rm {} \;
Knowledge is power.
rhansen
Frequent Advisor

Re: Deletion of files

Thanks.
James R. Ferguson
Acclaimed Contributor

Re: Deletion of files

Hi:

You can improve the performance of your removal by using this syntax:

# find /home/hansen/download -type f -mtime +210 -exec rm {} +

...instead of:

# find /home/hansen/download/* -mtime +210 -exec rm {} \;

Notice that the path to find() does not include the wildcard. You don't need the shell expanding this list. Next, files are specifically selected with '-type f'. Lastly, the terminator to '-exec' is a "+" character. This causes multiple arguments to be assembled for each 'rm' command spawned. Your processor and will love you :-)

Regards!

...JRF...
Fabian Brise├▒o
Esteemed Contributor

Re: Deletion of files

Thanks for improving/refining my script
Mr. Ferguson.

I had no idea I was putting extra unneeded work on my processor.

Live and learn.
Knowledge is power.