In Unix, how do I undelete a file?
Someday, you are going to accidentally type something like
rm *.foo , and find you just deleted
* instead of *.foo .
Consider it a rite of passage.
Of course, your system administrator should be doing regular backups.
Check with your sysadmin (usually username root) to see
if a recent backup copy of your file is available. For details, see
the Knowledge Base document At IU, how do I recover files or email deleted from UITS central systems? If the file is not
available, read on.
For all intents and purposes, when you delete a file with the
rm command, it is gone; the system totally forgets which
blocks scattered around the disk comprised your file. Even
worse, the blocks from the file you just deleted are going to be the
first ones taken and scribbled upon when the system needs more disk
space. However, it is theoretically possible (but quite difficult),
if you shut down the system immediately after you used
rm , to recover portions of the data.
Note: Under no condition will UITS honor requests to shut down any of the central systems to retrieve lost files.
Your first reaction when you've used the rm command by
mistake may be to make a shell alias or a procedure which
changes rm to move files into a trash bin rather than
delete them. That way you can recover them if you make a mistake, and
periodically clean out your trash bin. This, however, is generally
accepted as a bad idea. You will become dependent upon this behavior
of rm, and you will find yourself someday on a normal
system where it won't work.
Also, you will eventually find that dealing with the disk space and
time involved in maintaining the trash bin is a hassle. It might be
easier just to be a bit more careful with the rm command.
For starters, you should look up the -i
option to the rm command in your manual.
If you are still undaunted, then here is a possible simple answer.
You can make yourself a can command, which moves files
into a trash-can directory. In csh and tcsh,
you can place the following commands in the .cshrc file
in your home directory:
To automatically empty the trash when you log out, you can put the
following in the .logout file in your home directory:
Optionally, you can create shell scripts for the can and
mtcan commands. For can, the shell script
should consist of the following:
For the mtcan command, create a shell script that
consists of the following:
Also, if you use tcsh, you can place set
rmstar in your .cshrc or .tcshrc
file. This will cause the shell to prompt you for confirmation if you
enter rm * .
Note: This information comes from the Unix
FAQ, which is posted regularly to the Usenet
newsgroups comp.unix.questions and
comp.unix.shell. You can obtain it by FTP from
rtfm.mit.edu in the /pub/usenet directory,
and on the web at:
http://www.faqs.org/faqs/unix-faq/faq/
At Indiana University, for personal or departmental Linux or Unix systems support, see At IU, how do I get support for Linux or Unix?
Last modified on February 19, 2013.







