Here are several ways to protect your files from accidental loss.
- Backup your files to another place, e.g. the hard drive on another computer. See File transfer.
- Make backup copies of files and directories in a compressed tar file. For example, to create a compressed tar file (.tgz) of all files under directory “myproject”:
tar -czf myproject.tgz myproject/
man tarat the shell prompt for more information.
- Modify your personal Linux environment to change the cp (copy), mv (move), and rm (remove) commands so that you are prompted for confirmation before any existing file is deleted or overwritten.
bash shell: Add the following commands to your $HOME/.bashrc file:
alias cp='cp -i'
alias mv='mv -i'
alias rm='rm -i'
tcsh shell: Add the following commands to your $HOME/.cshrc file:
alias cp 'cp -i'
alias mv 'mv -i'
alias rm 'rm -i'
- Modify your personal Linux environment to prevent any existing file from being overwritten by the output redirection (>) symbol.
bash shell: Add the following command to your $HOME/.bashrc file:
set -o noclobber
tcsh shell: Add the following command to your $HOME/.cshrc file:
- Use the chmod command to remove your own write access to files you intend to not change or delete. Example:
chmod -w myfile
You will be unable to accidentally modify such a file in the future. If you try to delete a file for which you have removed your own write access without specifying the -f (force) flag on the rm command, you will be prompted and have to reply affirmatively before the file will be removed. Enter
man chmodat the shell prompt for more information.