ARCHIVED: In Unix, how do I undelete a file?

This content has been archived, and is no longer maintained by Indiana University. Information here may no longer be accurate, and links may no longer be available or reliable.

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 ARCHIVED: 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:

  #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.

At Indiana University, for personal or departmental Linux or Unix systems support, see Get help for Linux or Unix at IU.

This is document abeh in the Knowledge Base.
Last modified on 2018-01-18 08:58:58.