display | more...

A perfect illustration of the fundamental difference between Unix-like operating systems and Windows.

When you delete a file in Windows, the operating system by default asks you if you're sure if you want to delete the file, then stores it temporarily in the recycle bin, where you can retrieve it if you change your mind.

Unix, on the other hand, just deletes the file. You'd better be sure you want to delete it, because there's no getting it back once you hit enter.

### rm -rf

Remove, real fast.

If you're like me, you already have rm aliassed to rm -i, so it still asks annoying questions. yes | rm -rf ... or \rm -rf ... to skip those, too, and hose your directory in less time than it takes to type ^C.

### rm -i

A way to make Unix behave like Windows*. Used by sissies and people who aren't sensible enough to back up their stuff (and will be sorry they weren't, eventually, after a hardware failure).

No, really. I know it's better safe than sorry, but in the five years I've been using Unix (and quite intensively) I've inadvertedly deleted stuff perhaps a dozen times, and not once files that I couldn't recover or recreate in a matter of minutes.

Of course, there's one relatively useful application of this command: if you have a lot of files and want to delete many, but not all of them, and there's no name pattern to distinguish between the two, then it's quicker to rm -i and say yes or no for each file, instead of typing the names of all those you want to delete. But I don't think that occurs very often...

* It asks confirmation for every file it deletes with this command.

Kids! Say no to rm -rf!

rm -rf is a bad habit, and it almost all cases an inappropriate command to use. The time saved not having to answer a few prompts is outweighed by the risk of accidentally deleting something important.

rm is the unix command to remove a file. When rm is told to delete a directory, it ignores it; -r instructs rm to instead delete everything in it, and then delete the directory itself. When rm encounters a file that is not writable (an important system file, for example), it prompts the user to make sure he really wants to delete it; -f instructs rm to instead delete the file without asking.

rm -r is the normal way to delete a directory (the equivalent of deltree in windows)

rm -f is to delete single files without being prompted. This is useful when being called from a script, for instance. It can be used to delete files matching a wildcard, but a safer way is to use chmod on the files that are actually supposed to be deleted (to make them writable) and then use rm <wildcard> to delete them.

rm -rf should be used never. To delete a whole directory with 'important' files in it, either use rm -r, and answer 'yes' to the files you really want to delete, or chmod these files beforehand. The prompt is there to protect you from doing something you didn't really mean to do; disable it at your peril.

As well as the obvious risk of deleting your unix operating system, there are some additional side-effects of unix's 'everything is a file' philosophy to consider:

When used on hardlinks to files, symbolic links to directories, and symbolic links to files, rm -r will just delete the link, not the actual file or directory. But, when used on a hardlink to a directory, it will descend and clean the directory out. rm -r will also traverse to mounted filesystems, including NFS and SMB network shares, giving you the potential to accidentally delete all the files on a data partition, or even a completely different computer. It is especially risky to use rm -r on a system with more than one account (or user) that has permissions to make hard linked directories, or mount drives - even if there was nothing risky last time you checked, another user may have hardlinked a directory or mounted a drive before rm is called.

The risks of this feature can be reduced by using rm -r instead of rm -rf, as rm -r will prompt before deleting important files. Marking files you value (MP3s, programs, documents, etc.) as read-only will protect them from rm -r, as will mounting foreign filesystems (FAT32, NTFS, SMB network drives, etc.) with read-only file permissions. Only read-only media can reliably protect your files from rm -rf.

Another good practice is to use the -v switch to make rm, mv, chmod, etc. tell you what they're doing, so that if they're not doing what you told them to do, you have the opportunity to stop them, and a log of what they've done.

It's far better to stop using rm -rf before you delete the entire filesystem of one or more computers than after.

Log in or register to write something here or to contact authors.