Simpler Navigation for Servers and Operating Systems
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
cancel
Showing results for 
Search instead for 
Did you mean: 

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