Introduction
Deleting files is a basic function of using a computer. And on Linux, there is more than one way to delete things.
One of the tools, rm, comes standard but has some drawbacks.
Another tool, trash, does not come standard on most distributions, but provides some helpful protections.
We’ll discuss them both and take a brief look at how to use the trash tool.
Dangers of rm
The command to delete files or directories that comes standard with nearly all Linux distributions is rm. But the rm command can be dangerous since it is possible to accidentally use the command to delete the wrong things. And when using rm it can be difficult or impossible to recover those files.
As the Ubuntu command line tutorial cautions:
Unlike graphical interfaces, rm doesn’t move files to a folder called “trash” or similar. Instead it deletes them totally, utterly and irrevocably. You need to be ultra careful with the parameters you use with rm to make sure you’re only deleting the file(s) you intend to. You should take particular care when using wildcards, as it’s easy to accidentally delete more files than you intended.
The rm command can delete files, groups of files, directories, or complete directory trees. That’s why it must be used with caution. Using rm isn’t difficult, but the penalty for failure is high.
When a file is deleted with rm , it is gone. It isn’t moved to the trash. It is obliterated immediately. That doesn’t mean you should avoid using rm . But to use it safely, you need to be aware of what it can do, and ensure you’re using it properly.
Some tools are more dangerous than others, and far less forgiving of mistakes. That’s why there’s never been a movie called The Texas Wrench Massacre. rm isn’t a wrench, it’s definitely a chainsaw.
People use chainsaws all day everyday, and as long as they use one responsibly and mindfully, they’re fine. It’s the same deal with rm . When you pull rm out of your tool bag, you ought to slow down and check, then double-check, your command line.
We think this is a good analogy and helpfully illustrates the power of rm.
However, we respectfully disagree with the assertion that users shouldn’t avoid using rm.
We think you should avoid using rm unless it’s exactly what you need.
We think you should avoid using rm in the same way that you should avoid using a chainsaw for most small households tasks. You don’t use a chainsaw to slice bread. You don’t use a chainsaw to open boxes. You don’t use a chainsaw to cut paper. It’s too dangerous and powerful of a tool for most tasks, the cost of a mistake is too high, and there is a completely suitable alternative for most tasks. Yes, people use chainsaws everyday. And every day some people are hurt badly by chainsaws. It would be senseless to use such a dangerous tool without a need to use it. And typically that just isn’t the case with either chainsaws or rm. It is important to know about the rm command, but we don’t recommend making it your day-to-day tool for deleting files. Leave the chainsaw in the workshop on the shelf for most daily tasks, and use the safer and simpler tools when they work just as well. Bring out the chainsaw only when that is what you need.
Furthermore, even though the rm command may be helpful with some tasks and might be good to learn eventually, we don’t believe it is the tool that new users should focus on.
Instead, we recommend learning and using the trash
command for most situations.
trash command
The trash command removes files and sends them to the trash can.
The trash command operates in line with contemporary expectations that files are simply moved to a temporary location where they can be recovered instead of instantly deleted forever. This gives you a chance to see (and potentially restore) deleted files.
It does not come standard on most Linux distributions, so you likely will need to install it.
Installing the trash command
To install trash on Ubuntu:
Command:
sudo apt install trash-cli
Using the trash command
There are four basic functions of the trash command:
- Removing a file to the trash can.
- Restoring a file from the trash can.
- Listing all files in the trash can.
- Deleting files from the trash can.
Removing a file to the trash can
Command form:
trash {{file}}
Command example:
To delete a file called shopping-list.txt in the present working directory use the following.
trash shopping-list.txt
Restoring a file from the trash can
Let’s say we changed our mind about deleting that file.
If we had used the rm command, we’d have a difficult time restoring it, if it was even possible.
But with trash, we can get it back easily.
Command:
trash-restore
Example output:
To restore a file called shopping-list.txt in the present working directory use the above command and you should see something like the following interactive output.
user@computer:~/Documents$ trash-restore
0 2023-09-23 17:15:29 /home/Documents/notes.txt
1 2023-09-23 17:02:36 /home/Documents/shopping-list.txt
What file to restore [0..1]: 1
Entering 1
and pressing Enter
will restore the file.
Listing all files in the trash can
To see all files and directories currently in the trash can use the following command.
Command:
trash-list
Deleting files from the trash can
One thing to note with the trash command is that since it doesn’t permanently delete files, they remain on your system taking up space until you delete them permanently.
If you want to remove those files, you have several options:
- Permanently delete all files that match a specific pattern.
- Permanently delete all files with a specific original location.
- Empty the trash can of files older than a certain number of days and permenantly delete them.
- Empty the trash can and permanently delete all files stored there.
A note of caution: All of the following commands will permanently delete files. Be sure of what you are removing before proceeding.
Permanently deleting all files from trash can that match a pattern
Command form:
trash-rm {{pattern}}
Command example:
To permanently delete all files that end in _FINAL.pdf use the following.
trash-rm *_FINAL.pdf
Permanently deleting all files from trash can from a specific original location
Command form:
trash-rm {{full path to directory}}
Command example:
To permanently delete all files that had an original location of /home/USER/Documents/testdirectory use the following.
trash-rm /home/USER/Documents/testdirectory
Permanently deleting files from trash can older than N days
Command form:
trash-empty {{number of days since moved to trash can}}
Command example:
To permanently delete all files that have been in the trash longer than 120 days.
trash-empty 120
Permanently deleting all files from trash can
Command form:
trash-empty
rm command
Because the rm command still has it’s uses and you are very likely to encounter tutorials telling you to use it, there are a few things that are helpful to know.
The rm command removes files or directories immediately and permanently and comes standard with nearly all Linux distributions, but, as discussed above, it can be dangerous because it’s easy to delete the wrong thing and it can be difficult or impossible to recover files.
Caution when using the rm command
If you are going to use the rm command, it usually makes sense to use the i
(interactive) option to see prompts before files are deleted and the v
(verbose) option so that you are made aware of what the command is doing.
Caution when aliasing the rm command
While the rm command itself can be dangerous, some suggested strategies for aliasing rm are also dangerous. If you want to avoid accidental use of rm, alias to a different name than rm so you don’t create a dangerous habit, or consider aliasing to an explanatory echo command.
Conclusion
The trash command is often the right tool to remove files from your system.
It is a helpful and safer alternative to the rm command.