DEV Community

Cover image for Command Line Recycle-Bin For Linux System
yash sugandh
yash sugandh

Posted on

Command Line Recycle-Bin For Linux System

Recently, we looked at how to delete files and directories in the Linux system where we learned and used the rm command, but there was a huge problem with the rm command.

Can you remember what it was?

Yeah you guessed it right. The rm command permanently deletes the files and directories.

So what's the big deal, We can still recover the files we mistakenly deleted.

Yeah sure we can recover from the disk but it will cost a lot of time and we were using CLI to save the time in the first place.

So what can we do about this?

What if I tell you that we can use something that will act as our recycle bin and will not delete the files and directories permanently.

Really??

Yes, Today we are going to look at trash-cli.

trash-cli is a command-line interface to the trash-can used by Linux Systems.

The first question that comes to the mind is how do we install it?

Install trash-cli

There are three ways we can install the trash-cli

  • The Easy Way

trash-cli install

The trash-cli is present in repositories of most of the Linux Distributions. The above example is from the Ubuntu OS.

What if it's not present in the repositories?

  • The Python pip package manager

So first we install the pip using the following command :

install pip

After the pip has been installed successfully we use the following command to install the trash-cli :

trash-cli pip-install

If even this method is not possible for any reason

  • From the source

System-Wide Installation

trash-install-soure

For a single user

trash-install-user

Now that we know how to install trash-cli.Let's start exploring how can we use trash-cli.

  • Delete a file

The syntax for trash command

trash delete syntax

To delete a file using trash we only need to use the trash command along with file_name.

trash single file

  • Delete multiple files

trash-multiple-files

In the above example, we use the command trash file1 file2 where

trash represents cli of trash-can
file1 file2 represents files to be deleted

How can we check if the files have been moved to our trash or they have been permanently deleted?

Hmmm, Let's list all the files from our trash

  • List files in the trash

We use the trash-list command to list all the files that are present in our trash.

list-trash-files

In the above example, we used the trash-list command and found all the files that we recently deleted.

So, we have now confirmed that the files are not being deleted permanently.

Now we know how to delete single or multiple files.

Can we use wildcards with the trash command?

Let's try it out -

  • delete all the .txt files

trash-wildcard-star

In the above example, we use the wildcard * and created a command trash *.txt where

trash represents cli of trash-can
.*txt represents all the files that end with ".txt".

Similarly, you can try to use all the other wildcards with the trash command and let me know if you face any challenges.

Till now we have explored

  • How to delete files using the trash command
  • List all the files in our trash

Wait, the main benefit of moving the files to the trash instead of deleting the files permanently is that we can restore files from trash so is that possible using trash cli?

Let's try it out

  • restore file1 from trash-can

trash-restore

In the above example, we use the command restore-trash which presents us with all the files in the trash indexed starting from 0.

Our aim was to restore file1 which was on index 1 so we entered 1.

To verify that the file was restored we checked trash-list to confirm that the file is not present in the trash any more and then used ls command to confirm that the file has been restored.

Note: In some Linux Distributions the command for restore could be trash-restore.So if you try to use restore-trash command and get command not found then you can try trash-restore.

Now, let's have a look at how can we delete files from trash

  • Empty the trash

trash-empty

In the above example,
we first used the command trash-list to list all the files in the trash

Then used the command trash-empty to empty the trash and

To verify that the trash is empty we again used the trash-list command which returned no values as expected.

Similarly, we can use the command trash-empty <days> where days represent the number of days that the files have been in trash-can.

This can be used in cases such as if we want to clear all the files that are at least a week old. We will simply use trash-empty 7.

  • Delete file3 permanently from trash

trash-rm-single

In the above example, we used the command trash-rm file3 where

trash-rm represents the trash remove command
file3 represents the file to be deleted permanently

  • Delete all the ".txt" files from trash

trash-file-wildcard

In the above example, we used the wildcard * with trash-rm command and created command trash-rm *.txt where

trash-rm represents the trash remove command
*.txt represents the all the .txt files to be deleted permanently

Okay, so that’s all we need to know Trash-can in Linux System.

I hope you understood the use of trash in Linux System. Please, let me know if there are any questions.

Top comments (1)

Collapse
 
jingxue profile image
Jing Xue • Edited

One good habit I have gotten into over the years is when rm'ing by a glob pattern, always ls the same pattern first and make sure what shows up is what I intend to delete.