1753798 Members
8124 Online
108805 Solutions
New Discussion юеВ

Re: simple rm script

 
SOLVED
Go to solution
Tamera Street
New Member

simple rm script

I need to rm all txt files in a certain dir every night. Can you give me some suggestions on how to do this?
9 REPLIES 9
David Bellamy
Respected Contributor

Re: simple rm script

rm *.txt
James R. Ferguson
Acclaimed Contributor

Re: simple rm script

Hi Tamera:

Please define what you mean by "txt" --- an extension to a filename? --- an ASCII file as opposed to a binary one?

Do you want to recursively descend a directory?

Do you need help with the process of selecting and removing? Do you need help setting up a 'crontab'?

Regards!

...JRF...
Calandrello
Trusted Contributor

Re: simple rm script

Friend

Script follows one that and safer than to execute rm *.txt :

for G in `ll |grep .sh |awk '{print $9}'`
do
rm $G
done



spex
Honored Contributor

Re: simple rm script

Hi Tamera,

In this example, a cron job, which will be run as root, is scheduled. The job deletes all .txt files in /yourdir and its subdirectories at 11:00pm every day.

# grep -q root /var/adm/cron/cron.allow || echo root >> /var/adm/cron/cron.allow
# crontab -l root > /tmp/crontab.root
# echo "00 23 * * * find /yourdir -name '*.txt' -type f -exec rm -f {} 1> /dev/null \;" >> /tmp/crontab.root
# crontab < /tmp/crontab.root
# rm -f /tmp/crontab.root

For obvious reasons, care must be taken with a job such as this.

PCS
Dennis Handly
Acclaimed Contributor

Re: simple rm script

>Calandrello Script follows one that and safer than to execute rm *.txt

I'm not sure how this is safer or how it is "txt"? Also did you want to escape the "."?

But you can improve your script:
for G in $(ls | fgrep .sh); do rm $G; done
But this is the same as: rm *.sh.
Unless you really wanted to remove *.sh* files?

>spex
00 23 * * * find /yourdir -name '*.txt' -type f -exec rm -f {} 1> /dev/null \;

Any reason you want to redirect stdout from the rm command? You don't get anything, only to stderr. Did you mean "2>"?
Tamera Street
New Member

Re: simple rm script

The txt is a extension - data.txt I know how to do this by command line everyday but my scripting is limited. I would like to put this in a crontab to run everynight.
Bill Hassell
Honored Contributor
Solution

Re: simple rm script

Scripting is no different than typing the command(s) interactively. If you type one command such as:

rm /var/tmp/specialdir/*.txt

then you simply add that to crontab and pick a time at night to run this command. If there are multiple commands needed, put them into a file, add an interpreter line at the top and make it executable. Then add the full path to the script to crontab. Something like this:

#!/usr/bin/sh
rm /var/tmp/specialdir1/*.txt
rm /var/tmp/specialdir2/*.txt
rm /var/tmp/specialdir3/*.txt


Bill Hassell, sysadmin
Frank de Vries
Respected Contributor

Re: simple rm script

Tamara,
For me rm is rather silent.

I would prefer to embedd the rm command inside a find and log this. This would
improve security and visability.

The crontab entry would look like this:
(10 minutes before midnight, every day of the week)

50 23 * * * /root/adm/bin/text_clean.sh > /root/adm/logs/text_clean.log 2>&1

The script would look like this:
vi text_clean.sh

#!/sbin/sh
# Script to clean txt files by Tamara

find /path/textfiles -name "*txt" -type f -print -exec rm {} \;

#end of script

Now you could at a later stage , put a time
criteria in your find. For example to delete only the *.txt files older then 1 day.
The find command would look like this:

find /path/textfiles -name "*txt" -type f -mtime +1 -print -exec rm {} \;

You can also put multiple find commands in the script. So your housekeeping collection
can grow over time as the need arises.



Look before you leap
Arturo Galbiati
Esteemed Contributor

Re: simple rm script

Hi,
simply add in your crontab the command:

cd && /usr/bin/rm *.txt

at more conveninet time for you.

HTH,
Art