How to keep yourself from losing important files on a Unix system

There are a number of tricks that you can implement on a Unix system to lessen the chance that you will accidentally erase important files. The two that we'll examine will 1) help protect files in an important directory 2) set up a trash can to hold files for a period of time before they are actually deleted and 3) good old Unix security smarts.

Protecting a Directory

To protect files in an important directory from being inadvertently deleted (e.g., when you forget where you are in the file system when you type the rm command), touch a file named -i in the directory. You will have to type "touch -- -i" to keep your shell from interpreting your -i as a command option. The -i file will ensure that, should you type "rm *" in one of these protected directories, your rm command will expand into "rm -i" followed by the names of the other files. This then puts your rm command into confirmation mode and it will ask you to confirm each file deletion before it removes the file. If you don't use rm -i routinely, this will get your attention.

Creating a Trash Can

For anyone who spends most of their time working in a GUI, whether Windows or Unix/Linux, the idea of a trash can that holds files prior to their actual deletion is familiar and natural. When it comes to working on the command line, however, the safety net of having an extra chance to think before you delete a file just isn't there. And recovering files on Unix systems, aside from depending on backups, is just about impossible. But you can implement a command line trash can without a lot of work.

The first step is to create the trash can. Set up a directory called .trash, .trashcan, or .recyclebin or something else that works for you.

mkdir .trash

The next step is to create a function to dump files into the trash. Add lines like these to the end of your .bashrc or .bash_profile file. Once you log in again or source your file (e.g., . ~/.bashrc), you can use commands like trashit junk* to toss unwanted files into your .trash directory and leave them there until you're confident you don't need them anymore.

function trashit () {

mv $@ .trash


Alternately, you can call your function rm and not end up having to remember to use trashit in lieu of rm when you want to delete a file. The only drawback here is that the normal rm command will be unavailable to you as long as your function is set up -- unless, of course, you type /bin/rm!

A good complement to trashit is to have a reminder that tells you when you have files in your trash can -- especially if the trash files are piling up or getting old. You might put a reminder like this in your .bash_profile so that you're reminded every time you log in:

echo Reminder: You have `ls ~/.trash | wc -l` files in your trash can

Another option is to set up an alias -- or even a cron job -- that automatically dumps your trash when files have been sitting in it for more than a week. An alias to do this might look like this:

alias dumptrash='find ~/.trash -type f -mtime +7 -exec rm {} \;'

A similar script might look like this:


oldfiles=`find ~/.trash -type f -mtime +7 -print | wc -l`

find ~/.trash -type f -mtime +7 -exec rm {} \;

echo $oldfiles removed from .trash

Using Good Unix Security Smarts

Good Unix security practice suggests that you use file permissions to protect files. If you have files you want to be sure won't get deleted by mistake, you might be able to remove write permission to them. Then, when you go to remove them, you will get a warning that says "remove write-protected regular file `goodstuff'?" That should get your attention. And, of course, if you're not logging in as root, but using a normal user account and switching to root only when needed, you might find that you just can't remove files you're not supposed to remove.

Everybody makes mistakes now and then (at least everyone I know does), but you can make it less likely that those mistakes will bite you or maybe those bites won't break the skin. Happy Unixing!

This article is published as part of the IDG Contributor Network. Want to Join?

To express your thoughts on Computerworld content, visit Computerworld's Facebook page, LinkedIn page and Twitter stream.
10 super-user tricks to boost Windows 10 productivity
Shop Tech Products at Amazon