Re: Deletion of files (152 Views)
Reply
Frequent Advisor
rhansen
Posts: 47
Registered: ‎09-10-2009
Message 1 of 5 (152 Views)
Accepted Solution

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.
Esteemed Contributor
Fabian Briseño
Posts: 645
Registered: ‎03-09-2003
Message 2 of 5 (152 Views)

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.
Frequent Advisor
rhansen
Posts: 47
Registered: ‎09-10-2009
Message 3 of 5 (152 Views)

Re: Deletion of files

Thanks.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 4 of 5 (152 Views)

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...
Esteemed Contributor
Fabian Briseño
Posts: 645
Registered: ‎03-09-2003
Message 5 of 5 (152 Views)

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.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.