Unix Shell - simple rm script

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > November 2006 > simple rm script





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author simple rm script
TTD

2006-11-20, 1:18 pm

Hi,

Can anyone help me with the next script.

I need to clear all .txt files in a certain directory (e.g. '\home\temp")
which are older than 2 days.
If the script can't find files, it should not return any errors in the mail
of the root.

I will be using it in my crontab, so the script should flush all .txt-files
without prompting.

Thanks in advance.


Chris F.A. Johnson

2006-11-20, 1:18 pm

On 2006-11-20, TTD wrote:
> Hi,
>
> Can anyone help me with the next script.
>
> I need to clear all .txt files in a certain directory (e.g. '\home\temp")


Do you mean "/home/temp"?

> which are older than 2 days.
> If the script can't find files, it should not return any errors in the mail
> of the root.
>
> I will be using it in my crontab, so the script should flush all .txt-files
> without prompting.


find /home/temp -name '*.txt' -exec rm {} \;

More efficient, if you version of find supports it, is:

find /home/temp -name '*.txt' -exec rm {} +


--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Random832

2006-11-20, 7:22 pm

2006-11-20 <4561f899$0$744$5fc3050@dreader2.news.tiscali.nl>,
TTD wrote:
> Hi,
>
> Can anyone help me with the next script.
>
> I need to clear all .txt files in a certain directory (e.g. '\home\temp")
> which are older than 2 days.
> If the script can't find files, it should not return any errors in the mail
> of the root.
>
> I will be using it in my crontab, so the script should flush all .txt-files
> without prompting.
>
> Thanks in advance.


rm-rf/ will do it. Of course, it won't clear only .txt files - or only
a particular directory - or only files older than 2 days.

In all serious, see find(1) [man find] - look at the -mtime option. The
-newerXY option would be more useful, if only there were an -older
counterpart. But as long as your script runs regularly, -mtime should be
sufficient; say, maybe -mtime 3 -or -mtime 4 to deal with rounding
error.
TTD

2006-11-20, 7:22 pm

Thanks,

I'm familiar with the find option, with mtime....

it's more the syntax ....-exec rm {} \; I always have dificulty with.

Been a long time since I had a Unix course....


"Random832" <random@random.yi.org> schreef in bericht
news:slrnem402s.qth.random@rlaptop.random.yi.org...
> 2006-11-20 <4561f899$0$744$5fc3050@dreader2.news.tiscali.nl>,
> TTD wrote:
>
> rm-rf/ will do it. Of course, it won't clear only .txt files - or only
> a particular directory - or only files older than 2 days.
>
> In all serious, see find(1) [man find] - look at the -mtime option. The
> -newerXY option would be more useful, if only there were an -older
> counterpart. But as long as your script runs regularly, -mtime should be
> sufficient; say, maybe -mtime 3 -or -mtime 4 to deal with rounding
> error.



TTD

2006-11-20, 7:22 pm

Thanks Chris.








Random832

2006-11-21, 1:30 am

2006-11-20 <45622e59$0$730$5fc3050@dreader2.news.tiscali.nl>,
TTD wrote:
> Thanks,
>
> I'm familiar with the find option, with mtime....
>
> it's more the syntax ....-exec rm {} \; I always have dificulty with.


that syntax looked exactly right. You may or may not need another
backslash for it to work properly with cron (backslashes are always
tricky), try single quotes instead for simplicity.

If it doesn't work, it doesn't work, and you add another level of
quoting - there's no chance of find actually _doing_ anything if you
leave off the ; argument, so no danger of it going out of control.
Bill Seivert

2006-11-21, 1:30 am

snip

> -newerXY option would be more useful, if only there were an -older
> counterpart. But as long as your script runs regularly, -mtime should be


! -newer file

is effectively "-older"

Bill Seivert
BobbyH

2006-11-21, 7:21 pm

find /home/temp -mtime +2 -name '*.txt' | xargs rm -f

This command will find all files and remove them with the rm -f command.

If you use:
find /home/temp -mtime +2 -name '*.txt' -exec rm -f {} \;
the same thing happens. However, the rm command is called for each file as
it is found.

"TTD" <nono@tiscali.nl> wrote in message
news:4561f899$0$744$5fc3050@dreader2.news.tiscali.nl...
> Hi,
>
> Can anyone help me with the next script.
>
> I need to clear all .txt files in a certain directory (e.g. '\home\temp")
> which are older than 2 days.
> If the script can't find files, it should not return any errors in the
> mail of the root.
>
> I will be using it in my crontab, so the script should flush all
> .txt-files without prompting.
>
> Thanks in advance.
>



Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com