How to Send Files to Trash from the Linux Command Line

Using trash-cli to put files into the trash bin from the Linux command line.  (Image credit: Tom's Hardware)

Deleting files in Linux 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 Linux 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.

Even if your Linux desktop 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 your Linux desktop’s trash directory by navigating to ~/.local/share/trash. It comprises three directories: files, expunged (sometimes) 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. Expunged is often empty and you don’t really need to go in there.

(Image credit: Tom's Hardware)

Deleting Files Safely with Trash-Cli 

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 so, you can install it by typing: 

sudo apt-get install trash-cli -y

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

(Image credit: Tom's Hardware)

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 trash-put 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. 

Restoring files from the trashcan using the restore-trash command.  (Image credit: Tom's Hardware)

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/forgm/memo of appearance.odt
1 2020-02-13 14:02:34 /home/linuxlala/Documents/forgm/temp
What file to restore [0..1]:

So the second time we run restore-trash, the utility presents a list of files deleted from the current directory, and asks you to specify the file you wish to restore. 

Prune the Trash with Trash-Cli

You can delete files or directories from the /trash with the trash-rm <pattern> or the trash-rm <filename> commands. The latter command expects the complete name of the file, and you can’t employ tab-complete with this command.  

Removing files from trash using the trash-rm Linux command.  (Image credit: Tom's Hardware)

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 corresponding file, nor will it report any errors.  Alternatively, 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 directories from trash. The <days> 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. 

Using Rm-Protection to Safeguard Important Files 

You must answer a question correctly before deleting a file with rm-p.  (Image credit: Tom's Hardware)

You can use the rm-protection utility to force users to answer security questions before they can delete important files. Run this command to install rm-protection: 

sudo pip3 install rm-protection

If you get an error message saying that pip is not a known command, you must install pip first by typing: 

sudo apt-get install python3-pip

The rm-protection utility 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. 

Using rm-protect to force users to answer a question before deleting a file in Linux.  (Image credit: Tom's Hardware)

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” so that users who type rm really get 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 by the possibility of restoring deleted files, you may want to skip the rm-p command. Like rm, it bypasses the /trash directory. 

Write-Protecting Files with Chattr 

Using Chattr to write-protect files in Linux

Typing chattr +i [filename] write protects a file while typing chattr -i [filename] unprotects it.  (Image credit: Tom's Hardware)

If you have accidentally lost precious data because of hasty Shift+Del, we have a quick solution to prevent further mishaps. You can use chattr to change the associated attributes for a file. The default syntax is chattr [operator] [switch] [filename].

Refer to the man page of chattr for a list of all available attributes and their purpose. For now, we’re only interested in the immutable attribute, which can be used to make sure a file cannot be deleted by any command, even by a root user.

Run the sudo chattr +i [filename] command to set the immutable attribute. You’ll find that you can no longer delete the file, and you can’t edit it either. Run lsattr filename to view the attributes of the specified file. sudo chattr -R +i <directoryname> can also be used to recursively change the attribute of all files in the specified directory.

If you wish to be able to edit the file, but only wish to protect it from being deleted, you can use the append attribute instead of immutable: sudo chattr +a filename . Replace the + with an – operator to remove the attribute from a file. For instance, running the sudo chattr -i filename command will once again grant you the ability to edit or delete the file. 

Shashank Sharma
Freelance Writer

Shashank Sharma is a freelance writer for Tom’s Hardware US, where he writes about his triumphs and joys of working with the Linux CLI.

  • unis_torvalds
    Thank you Toms for this practical article!

    However is important to mention that the commands shown are specific to Debian (and derivatives like Ubuntu).

    Otherwise you risk confusing beginners who have a different package manager with their distro.
    Reply
  • juliancardich
    The recommended way to send files to the trash from the shell is
    gio trash myfileThere is nothing to install.
    Reply
  • bit_user
    It's also worth mentioning that people can enable periodic or manual snapshots on filesystems like BTRFS and ZFS that support them. If you use snapshots, then it protects you not only from unintended deletion but also accidental modification and corruption.

    As for specific instruction or guidance on snapshot usage, that's worth an article all by itself. I'm sure anyone interested can already find a couple, out there.
    Reply