APC Australia

The smarter way to delete files

If you’re worried about accidental­ly wiping out important files, Shashank Sharma knows of a few techniques to protect yourself.

-

Deleting files is a fairly straightfo­rward 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 immediatel­y 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 permanentl­y delete files using the graphical environmen­t, you must press the Shift-Del key combo. You’ll be prompted with an alert seeking confirmati­on before the selected files or directorie­s are deleted. This is in stark contrast to the command line, where running the rm command will delete the specified files without any confirmati­on prompts.

In this tutorial, we’ll discuss two command-line utilities, trash-cli and rm-protection, which you can

use to add these functional­ities to the terminal.

TRASH FOR BASH

Even if your graphical environmen­t 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 directorie­s: files, expunged and info. The info directory keeps a record of the permission­s, path and deletion date of each of the files. This is relevant and useful if you decide to restore deleted files or directorie­s. 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 repositori­es of many popular desktop distributi­ons. If it’s not available for your distributi­on, you can follow these steps to install it from Git:

$ git clone https://github. com/andreafran­cia/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 command option with the last command so that the trash-cli utility isn’t installed system-wide and is only available for the specified 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 permanentl­y delete files using the graphical environmen­t, you must press the Shift-Del key combo”.

directorie­s, view the contents of the trash, restore files and more:

trash-put – Delete specified files or directorie­s.

trash-list – Displays the contents of the trash.

trash-rm – Delete individual files or directorie­s from the trash.

trash-empty – Delete all files and directorie­s 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 directorie­s unless you use the -R command option, trash-put has no such reservatio­ns 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 environmen­t, 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 correspond­ing to the file you wish to restore. The downside to this approach is that you can’t simultaneo­usly 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 restoretra­sh , 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 directorie­s from the /trash with the trash-rm or the trash-rm command. The latter command expects the complete name of the file, and you can’t employ tab-complete with this command. So if you wish to delete a file named memo of appearance from the /trash, you will have to type the complete file name. If you only provide one part of the name to trash-rm it won’t delete the correspond­ing file, nor will it report any errors. Alternativ­ely, you can run the trash-rm memo* command to delete the file and all other files that start with the word memo.

The trash-empty command can similarly be used to delete all the files and directorie­s from trash. The command option can be used to only delete files that have been in the trash for longer than a specified number of days. For instance, if you wish to delete all the files that were deleted more than a month ago, you can run the trash-empty 30 command. The command trash-empty 1 will similarly delete files that have been in /trash for longer than a day.

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 accidental­ly.

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 possibilit­y 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.

 ??  ?? While the project hasn’t seen a new release in quite some time, it works exactly as advertised, even with large files and directorie­s.
While the project hasn’t seen a new release in quite some time, it works exactly as advertised, even with large files and directorie­s.
 ??  ?? You can run the protect filename command to overwrite the existing question/answer for a protected file.
You can run the protect filename command to overwrite the existing question/answer for a protected file.

Newspapers in English

Newspapers from Australia