The smarter way to delete files
If you’re worried about accidentally wiping out important files, Shashank Sharma knows of a few techniques to protect yourself.
Deleting files is a fairly straightforward process, whether you work with a graphical interface or the command line. The former, more often than not, relegates deleted files into the trash directory, giving you the option to access deleted files and even restore them if needed. But this isn’t the case with the popular rm command. Once invoked, rm immediately flushes the specified files, skipping the trash directory altogether. To recover a file deleted from the command line, you must use dedicated software such as PhotoRec, but the process is involved and cumbersome, and not the subject of this tutorial.
If you wish to permanently delete files using the graphical environment, you must press the Shift-Del key combo. You’ll be prompted with an alert seeking confirmation before the selected files or directories are deleted. This is in stark contrast to the command line, where running the rm command will delete the specified files without any confirmation prompts.
In this tutorial, we’ll discuss two command-line utilities, trash-cli and rm-protection, which you can
use to add these functionalities to the terminal.
TRASH FOR BASH
Even if your graphical environment of choice doesn’t place an icon for it on the desktop, your Linux distro already has a trash directory. The advantage of relegating files to a trash directory is that it gives you the option to view all the deleted files and even restore files, empty the trash, etc. You can access this trash directory by
navigating to ~/.local/share/trash. It comprises three directories: files, expunged and info. The info directory keeps a record of the permissions, path and deletion date of each of the files. This is relevant and useful if you decide to restore deleted files or directories. You’ll find all the deleted files and folders in the files directory.
The trash-cli utility adds the ability to move files to the trash from the command line. You’ll find the utility in the software repositories of many popular desktop distributions. If it’s not available for your distribution, you can follow these steps to install it from Git:
$ git clone https://github. com/andreafrancia/trash-cli. git
$ cd trash-cli
$ sudo python setup.py install
The first command creates a directory named trash-cli in the working directory and copies the contents of the trash-cli repository. You can now run the setup.py script to install the utility. You can optionally use the --user
$ sudo python setup.py install --user linuxlala
Once installed, the trash-cli utility provides five unique commands that can be used to delete files or
“If you wish to permanently delete files using the graphical environment, you must press the Shift-Del key combo”.
directories, view the contents of the trash, restore files and more:
trash-put – Delete specified files or directories.
trash-list – Displays the contents of the trash.
trash-rm – Delete individual files or directories from the trash.
trash-empty – Delete all files and directories from trash.
restore-trash – Restore the specified file or directory.
To delete a file, you must use the trash-put command. You can provide a list of space-separated files, and even use pattern-matching to specify the files you wish to delete. Unlike the rm command, which won’t delete non-empty directories unless you use the -R command option, trash-put has no such reservations about deleting them.
As the trash-cli utility only makes use of the already present /trash directory on your system, you can straight away run the trash-list command to view the contents of your /trash directory. That is, files deleted using the graphical environment, or from the command line using trashput command can be accessed with the trash-list command.
When you run the restore-trash command without any additional arguments, the utility will display all the files in the /trash directory and ask you to choose the file you wish to restore. The files are all assigned a unique number, and you must enter the one corresponding to the file you wish to restore. The downside to this approach is that you can’t simultaneously restore multiple files.
The restore-trash command also keeps track of your current working directory. If you invoke it from your /home directory, it will list all the files in the /trash directory. However, if you run it from another directory, it will only list the files in the /trash that were originally stored in that directory:
$ cd ~/Music/
$ restore-trash
No files trashed from current dir (‘/home/linuxlala/
Music’)
$ cd ~/Documents/for-gm $ restore-trash
0 2020-02-13 14:02:31 / home/linuxlala/Documents/ for-gm/memo of appearance.odt
1 2020-02-13 14:02:34 / home/linuxlala/Documents/ for-gm/temp
What file to restore [0..1]: So the second time we run restoretrash , the utility presents a list of files deleted from the current directory, and asks you to specify the file you wish to restore.
PRUNING TRASH
You can delete files or directories from the /trash with the trash-rm
The trash-empty command can similarly be used to delete all the files and directories from trash. The
PROTECTED FILES
You can use the rm-protection utility to provide a question/answer for important files. The specified file will not be deleted unless you provide the correct answer to the prompted question. This ensures that files aren’t deleted accidentally.
Run the sudo pip install rm-protection command to install the utility. It provides two commands – protect and rm-p. The former is used to specify the files you wish to safeguard against accidental deletions, while the latter can be used to delete files so protected. Remember, you can still delete a protected file using the rm or the trash-put command.
So if you decide to employ protect to safeguard your files, you should consider creating an alias for rm-p such as: alias rm=”rm-p”
When you protect a file, the question and answer are stored in a plain text file named .filename.rm-protection. The answers are case-sensitive, so typing “terminator” as an answer in place of “Terminator” will result in an error.
If you’re comforted with the possibility of restoring deleted files, we must take a moment to caution you against using the rm-p command. Like rm , it bypasses the /trash directory.