A Trash-Bin for the Ubuntu Command Line

Ubuntu Commandline Trash-bin

As a Linux user, you may need to delete files from your system from time to time. We tend to be cautious while deleting files, especially when deleting them permanently, as we do not want to mistakenly lose useful information forever. Unfortunately, there are instances when we delete something by mistake and there is no way to recover it. Or, it happens that we intend to delete only files from a folder but mistakenly delete files from the subfolders as well. In the graphical interface, things are pretty much visible and there is also the Trashcan utility to recover deleted files. However, file deletion is especially a sensitive operation in the Linux command line.

In this article, we will explain a few ways to safely remove files through the Ubuntu command line and also install a Trash CLI so that files can be recovered if we have deleted them by mistake.

We have run the commands and procedures mentioned in this article on an Ubuntu 22.04 LTS system.

Since we are using the Ubuntu command line application, the Terminal, you can open it either through the system Dash or the Ctrl+Alt+T shortcut.

The rm command - The brutal way of deletion

The rm command in Linux is used to remove files and folders from the system. Although it is the most widely used command for this purpose, it is not the optimal way to do so. It is because when you delete files and folders through this command, they are extremely hard to recover. Let us go through the syntax that we usually follow in order delete files and folders:

Delete a file with rm command

The following command will remove the specified file permanently from your system:

$ rm [/file-location/file-name]

Example:

$ rm /home/textfile.txt

Delete Files in all folders and sub-folders

The following command will remove the specified folder, including its files and all the files in its subfolders permanently from your system:

$ rm -R [/folder-location]

Example:

$ rm -R /home/samplefolder

Making safe deletion through the rm command

The above mentioned commands remove the files from the system almost permanently; so whats gone is pretty much gone. The system does not even prompt you for confirmation before deleting any file. How about use a switch that asks you for confirmation before deleting a file from your system?

Delete files by first getting a confirmation prompt

When you use the -i switch, you will get a confirmation prompt before the system deletes the file.

$ rm -i [/file-location/file-name]

Example:

$ rm -i /home/textfile.txt

The system will only delete the file if you enter Y as the answer to the confirmation prompt.

Getting a confirmation prompt when deleting more than 3 files

When we want to delete multiple files at once, it becomes a little troublesome to be asked every time before the deletion of each file. The -I switch will only give you a confirmation prompt if you select to delete more than 3 files at once or you are deleting recursively in folders and sub-folders.

$ rm -I [/file-location/file-name]

The command line Trashcan - The recoverable way of deletion

The -i and -I switches mentioned above might be a careful way of deleting files but the safest route is to have an option to recover files even when you have deleted them. The Trash Can command line interface provides exactly what the Trash or Recycle bin does in the UI.

Installing the Trash CLI

Run the following command as root in your Terminal as only an authorized user can install software on Ubuntu:

$ sudo apt-get install trash-cli

Trash-cli installtion

You may be required to enter the password for sudo. Once the installation is complete, you can verify the installation and also check the version number of the trash utility through the following command:

$ trash --version

The trash command can be used in multiple ways. You can view what can be done with it, through the various switches by checking the trash help through the following command:

$ trash --version

The Ubuntu trash command

Using the Trash CLI

You can perform the following operations through the Trash CLI:

Delete a file by sending it to the Trash Can

Use the following command to send a file to the Trash can:

$ trash [/file-location/file-name]

List files in the Trash Can

Use the following command in order to list all the files currently residing in the Trash can:

$ trash-list

Empty the Trash Can

Use the following command in order empty the Trash can; this will remove the files permanently from the system:

$ trash-empty

Restore Files from the Trash Can

Use one of the following commands in order to restore the files to the location from where they were deleted:

$ trash-restore

Or

$ restore-trash

When you run this command, the Trash utility will list all the files from the Trash can with a unique number assigned to each. Enter the number against a file in order to restore it.

After reading this article, you are better equipped with safely deleting files through the Ubuntu command line. You can use the mentioned switches with the rm command to get a prompt before deletion or use the Trashcan CLI in order to perform all those operations that you could otherwise do with the graphical Ubuntu Trash utility.