PDA

View Full Version : scripted rm fears



surfdoc
July 6th, 2008, 05:40 PM
Hi,

I'm wanting to add some automatic file deletion in to a bash script. All the files and directories I want to delete begin with $ or #. I was wondering if its as simple as:

rm -rf $*
rm -rf #*

however I wasn't sure whether these were special characters. I'm afraid I might delete more than I bargained for, so I haven't wanted to try my usual trial and "error" approach (The potential error around a line of script that looks rather like rm -rf * gives me the hebejebies). Can anyone give me some pointers on this?

Ta

CptPicard
July 6th, 2008, 05:47 PM
$* means "all command line parameters" so yeah, that's a huge hole :)

nvteighen
July 6th, 2008, 05:55 PM
(...) I'm afraid I might delete more than I bargained for, so I haven't wanted to try my usual trial and "error" approach (The potential error around a line of script that looks rather like rm -rf * gives me the hebejebies). (...)

For your tests, I think you could create a bunch of files inside of a directory without write permissions. If the script works, you'll get a "Permission denied" error, if it doesn't find the files, you'll get "File not found" instead. This way, you'll be safe from deleting something wrong... I guess... :p

LinuX-M@n1@k
July 6th, 2008, 07:44 PM
I believe "\$*" and "\#*" would do the trick, but for more safety use nvteighen's advice.

Both $ and # are special characters:
$ - indicates the value the variable holds
# - comment
Correct me if I'm wrong.

http://tldp.org/LDP/abs/html/special-chars.html

surfdoc
July 6th, 2008, 08:19 PM
Thanks guys - escaping the characters does seem like the obvious option. Still a little nervous tho. I suppose I could remove the -f and then it would prompt for each file ?

henchman
July 6th, 2008, 08:20 PM
right-o :)

Can+~
July 7th, 2008, 12:07 AM
Use the -i flag so it will prompt for every removal, thus, making sure you're not deleting anything important.