How to avoid losing files to accidental deletion

Last modified 2019-Oct-14.

  1. Back up regularly

    Here are instructions for making daily automated backups to a Network Attached Storage (NAS) device.
  2. Stay synchronized

    Another way to stay backed up is to synchronize your files with a remote filesystem on another computer. A common instance of this is keeping your home and workplace computers in sync with each other. When you get home, you synchronize your home computer with your workplace computer, updating all the files that you changed at work during the day. At the end of the evening, you upload any subsequent changes from your home computer to your workplace.

    The rsync command is a great tool for this. Here is a general-purpose driver script called rsynchro which synchronizes a given list of files rsynchro.lis between a local and remote machine.

  3. Use a safe deletion command instead of rm

    Create a wastebasket directory, e.g. ~/.wastebasket, and put these definitions in .bashrc:
    alias rm='echo "Use rem for reversible delete, \\rm  for regular delete, wipe or srm for secure delete"'
    
    rem () { 
      IFS=$'\n';   # handle filenames with properly escaped spaces
      echo Removing $* 
      read -p "OK? " rem_resp
      if [ ${rem_resp} == "y" ]; then 
        \mv --backup=numbered -t ~/.wastebasket $*
      else
        echo No action taken
      fi
    }
    
    Now you always type "rem file1 file2 ..." to delete files. If you need the original rm command, you can still get it by typing "\rm". You will quickly get used to typing "rem" instead of "rm", and before long you will thank yourself for making the effort. Just remember to empty your wastebasket directory occasionally.
  4. Make mv and cp safer

    Alias them to safer versions that prompt before overwriting, and create a backup of what they overwrite. Include these lines in .bashrc:
    alias mv='mv -i -b'
    alias cp='cp -i -b'
    

Mark Alford's home page

alford(at)physics.wustl.edu

Valid XHTML 1.0!