Indiana University
University Information Technology Services
  
What are archived documents?
Login>>
Login

Login is for authorized groups (e.g., UITS, OVPIT, and TCC) that need access to specialized Knowledge Base documents. Otherwise, simply use the Knowledge Base without logging in.

Close

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, who can recover files and email accidentally 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:

#junk file(s) to the trashcan: alias can 'mv\!* ~/.trashcan' #irretrievably empty trash: alias mtcan 'rm -rf ~/.trashcan/ ;mkdir ~/.trashcan' #ensure trashcan exists: if ( ! -d ~/.trashcan ) mkdir ~/.trashcan

To automatically empty the trash when you log out, you can put the following in the .logout file in your home directory:

rm -rf ~/.trashcan/ ; mkdir ~/.trashcan/

Optionally, you can create shell scripts for the can and mtcan commands. For can, the shell script should consist of the following:

#!/bin/ksh if [ ! -d ~/.trashcan ] #ensure trashcan exists then mkdir ~/.trashcan fi mv $* ~/.trashcan/ #junk file(s) to the trashcan

For the mtcan command, create a shell script that consists of the following:

#!/bin/ksh rm -rf ~/.trashcan #irretrievably empty trash mkdir ~/.trashcan #recreate trashcan directory

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?

This is document abeh in domain all.
Last modified on August 22, 2008.

Comments/Questions/Corrections

Use this form to offer suggestions, corrections, and additions to the Knowledge Base. We welcome your input!

If you are affiliated with Indiana University and would like assistance with a specific computing problem, please use the Ask a Consultant form, or contact your campus Support Center.

Contact Information

Note: We will reply to your comment at this address. If your message concerns a problem receiving email, please enter an alternate email address.