Command Line Recycle-Bin For Linux System
Tags: linux, ubuntu, beginners, computerscience
Published: 2020-05-26 13:36:23

Recently, we looked at how to <a href = "https://dev.to/yashsugandh/deleting-files-and-directories-in-linux-system-4hbf"> delete files and directories in the Linux system</a> 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](https://dev-to-uploads.s3.amazonaws.com/i/sawet5gzbib2i8nl2nc0.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/3ii2a2b68wiifetg9eyr.png)

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

![trash-cli pip-install](https://dev-to-uploads.s3.amazonaws.com/i/9cic8x7j7icupgxsmcs0.png)

If even this method is not possible for any reason

- From the source

**System-Wide Installation**

![trash-install-soure](https://dev-to-uploads.s3.amazonaws.com/i/yjwyjmudnd5ovgdksmai.png)

**For a single user**

![trash-install-user](https://dev-to-uploads.s3.amazonaws.com/i/4ii7m91c5p5whlgfdemm.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/c8k8bg8omcu31kgva9rl.png)

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

![trash single file](https://dev-to-uploads.s3.amazonaws.com/i/2j1afq3nk3dg3jrd44v5.png)

- Delete multiple files

![trash-multiple-files](https://dev-to-uploads.s3.amazonaws.com/i/a91cfbvlfsvf290ly3a9.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/5ptktii13b6lnfsoj7m4.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/qb0ilm57nco2svqm7gki.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/epi2avsopip3fa2ca9k4.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/1io880r23v7oo4ebe2nx.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/ebdy7pfurnll4rq09kyl.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/1y1yufcut16dpw5gql0v.png)

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.