Skip to content

[Linux] Use “trash” Command Instead Of “rm” Command To Avoid Deleting Files By Mistake

First, I want to explain, the rm command is a useful but danger command. On the one hand, the command is very brief, on the other hand, when we use the system, it is inevitable to release space and delete unnecessary old files.

If you use rm to delete files, if you accidentally delete the files by mistake, you may only be able to quickly restore them with Inode, BUT THIS IS OFTEN NOTE SAVED.

What I want to introduce today is a command trash that can be instead of rm.

What is trash?

Simply to put, almost all of system have a specific rule that can store the files we delete into a temporary space.

Such as Windows, there is a trash we can restore the files we deleting.

Generally, we use the del key to delete files in the desktop environment, and the files will automatically move to the bottom of Trash. When we are sure that we don’t really want this file, we enter Trash to delete it completely.

The operation flow of the trash command is the same. Files deleted using this command will not disappear completely, but temporarily moved to the bottom of Trash, which can be restored or deleted at any time. In this way, the problem of rm deleting files by mistake is avoided.


Several commands of trash

Before we using trash command, we need to install it first.

sudo apt install trash-cli




1. Use trash-list to check whether there are any deleted files in Trash

trash-list

But at the beginning, if there is no file, no information will be displayed here.



2. Use trash to delete files

Suppose I have two files that I want to delete, called 01.txt and 02.txt.

trash 01.txt 02.txt


Use a space to separate the two files, you can see that these two files are no longer under the current folder. At this time, use trash-list to confirm that the file is under Trash, and you should see information like this:

2020-08-04 20:38:40 /home/clay/02.txt
2020-08-04 20:38:40 /home/clay/01.txt

Record the time of deletion and the file path before deletion.



3. Use restore-trash to restore files

If you want to restore the files in Trash, you can enter:

restore-trash

Output:

restore-trash
0 2020-08-04 20:38:40 /home/clay/02.txt
1 2020-08-04 20:38:40 /home/clay/01.txt
What file to restore [0..1]:

Suppose you want to restore 02.txt, then you have to enter its number 0.

This is a little troublesome when restoring a large number of files… In fact, it can be copied directly from the path of ~/.local/share/Trash/files/ with the cp command. It is recommended not to use the mv command to move directly, or to cause confusion in the subsequent trash command.


4. Fourth, use trash-empty to empty all files in Trash

It’s very simple, use directly:

trash-empty

All files under Trash will be deleted. However, just like the rm command, you must first make sure that there are all unnecessary files.

In the end, I think even if there are more insurance steps, the possibility of accidentally deleting the mistake will always be there. The most important thing is that users must carefully confirm the instructions they have given. This is the way to cure the root cause.


References


Read More

Tags:

Leave a Reply