14,000 hints and counting!

A safe script alternative to 'rm'
I found this alternative to rm some time ago, and it has saved me from a few potential deleting disasters. It's simply a shell script, which I've put in the /bin directory and named it "del". I also created an alias in my .cshrc file, such as:
  alias rm /bin/del
Don't forget to 'source .cshrc' after your edit.

This script simply moves the file you're wanting to delete to /tmp, and/or names duplicate files as /tmp/filename+datestamp. That way, if you realize that you've deleted a file that you need after all, you can retrieve it safely from /tmp.

#!/bin/tcsh# del Moves the argument to directory /tmp instead of removing it.# Bumps old version in tmp to /tmp/$FILE+$stamp if it exists.setenv stamp date | awk '{print $2"_"$3"_"$4}'echo "Moving files(s) to the trash ..."foreach FILE ($*)  if ( ! -f $FILE ) then # if not file or directory if (! -d$FILE ) then      echo "$FILE not found" exit endif endifif (-f /tmp/$FILE) then  echo " * File /tmp/$FILE exists. " echo " * Bump it to /tmp/$FILE+$stamp." mv /tmp/$FILE /tmp/$FILE+$stampelse if ( -d /tmp/$FILE) then echo " * Directory /tmp/$FILE exists. "  echo " * Bump it to /tmp/$FILE+$stamp."  cp -r /tmp/$FILE /tmp/$FILE+$stamp rm -r /tmp/$FILEendifmv $FILE /tmpecho "$FILE moved to /tmp ..."end
[Editor's note: Save the file, make it executable (chmod 755 filename), type 'rehash' to force the shell to find it, and you're good to go. Remember to create an alias for 'rm' if you want this new command to override the old, and I'd recommend keeping it in ~/bin or /usr/local/bin, just to protect it from future upgrades.]
•
• Currently 4.00 / 5
• 1
• 2
• 3
• 4
• 5
(1 vote cast)

[6,870 views]

## Hint Options

A safe script alternative to 'rm' | 15 comments | Create New Account
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Safe deleting
Authored by: wayneyoung on Oct 18, '02 10:33:55AM

Seems like a good idea, but I think moving the file to ~/.trash might make a bit more since. Of course you should NEVER delete anything you might possibly want again, but accidents do happen :^)

Safe deleting
Authored by: Tiresias on Oct 21, '02 06:12:34PM

I've tried that but .trash is a slippery beast. It only exists if there is something in it. That would be optimum of course so you didn't have to clean out /tmp, you could just empty the trash.

Maintaining /tmp
Authored by: Lizard_King on Oct 18, '02 11:09:23AM

So then how do you perform regular maintenance on the /tmp directory? In other words, when the /tmp directory starts growing past your acceptable limits, how do you delete old files (that you've verified are OK to go of course)? Do you temporarily disable the alias? is there a better way?

Maintaining /tmp
Authored by: mervTormel on Oct 18, '02 11:12:34AM

% \\rm /path/to/filename

the backslash will disable command line alias expansion

Maintaining /tmp
Authored by: mervTormel on Oct 18, '02 11:14:00AM

one backslash. dang geeklog.

MvMac
Authored by: CaptDeuce on Oct 18, '02 11:30:19AM

I haven't tested it ... I take that back, I just did using the command:

/Developer/Tools/MvMac [filelist] ~/.Trash

I Don't know the syntax off hand but when made into an alias, "rm" will work exactly like dragging files to the Trash. Is good, eh?

Of course you'd need to install the Developer Tools ... but if you're mucking with Terminal, chances are you already have. ;-)

CaptDeuce

MvMac
Authored by: bradrf on Oct 18, '02 11:40:22AM

This is an excellent idea. However, take note that MvMac will not work across volumes or filesystems (as noted in the manpage).

MvMac
Authored by: mervTormel on Oct 18, '02 03:19:19PM

beware! mvmac and cpmac have produced undesirable results in the past and there's no indication that these tools have been updated. you should use ditto.

csh scripts are icky
Authored by: sjk on Oct 18, '02 04:13:17PM
Anyone writing/contributing (t)csh scripts might want to read Tom Christiansen's seven-year-old classic Csh Programming Considered Harmful article.
there's a much simpler way...
Authored by: Jay D on Oct 18, '02 07:37:26PM
this is way complicated when alias rm /bin/rm -i will ask you for confirmation.
there's a much simpler way...
Authored by: cubixe on Oct 19, '02 02:53:41AM

I currently have rm aliased to rm -i, but its SOOOO annoying because most of the time I AM deleting the correct thing. with the script, I don't have to confirm everything.... if I make a mistake, I just go fetch it out of the /tmp directory (or wherever I'm putting it).

Great idea!

Beware...
Authored by: patashnik on Oct 19, '02 05:05:08AM

Have you ever tried removing files with embedded whitespace in their names with this script? AFAICS, it will bomb on them.

Do Not Alias this to rm
Authored by: kerbaugh on Oct 21, '02 12:57:16AM

makefiles regularly use the rm command to clean up and this script will cause the make to fail. I've written a script like this but it faithfully emulates the rm command. It uses CpMac to move files to ~/.Trash, supports recursive removal, handles spaces in quoted filenames, (which this script does not), and appends " copy" to existing files. (like the finder) It can safely be used as rm but despite what my website says, I currently name the script rmm on my Mac. The reason is that installations can still put an incredible amount of junk in the Trash. Those interested in my script can find it at its webpage:

http://www.cs.ecu.edu/~kerbaugh/rm/rm.html

This is what I use
Authored by: Paul Burney on Nov 13, '02 09:47:10PM

This is the script I wrote for the same effect. It creats a folder called .trash_can in your home directory and moves all files on the command line to the directory. It could use the .Trash directory, but I'd prefer not to mess with the Apple supplied folders.

The script uses the Bourne Shell and should work on platforms other than MacOSX. (I use it on RedHat Linux at work.) It works with spaces in file names and uses ditto rather than mv on darwin systems.

#!/bin/sh
#
# trash - This file moves folders and files to a .trash_can directory in
# a user's home directory instead of deleting them.
#

# Make sure there is at least one file listed
if [ "$#" -eq 0 ] then echo "Usage: trash file-list"; exit 1; fi # Check for existance of a trash_can directory and create if not if [ ! -d${HOME}/.trash_can ]
then
echo "Creating Trash Can...";
mkdir ${HOME}/.trash_can; chmod 700${HOME}/.trash_can;
fi

# Initialize a file counter
j=0;

# For each directory/file, see if it exists, if so, append a number
# and check again. Then move the files to the trash can
for this_file in "$@" do i=0; if [ -s$this_file ]
then
new_file=echo $this_file | tr '/' '_'; while [ -s${HOME}/.trash_can/"${new_file}" ] do i=expr$i + 1;
new_temp=echo $this_file | tr '/' '_'; new_file="${new_temp}_${i}"; done if [$OSTYPE = 'darwin' ]
then
ditto -rsrcFork "$this_file"${HOME}/.trash_can/"${new_file}"; rm -rf "$this_file";
else
mv "$this_file"${HOME}/.trash_can/"${new_file}"; fi j=expr$j + 1;
fi
done

echo "\$j files moved to the trash can.";