Home directories almost always end up cluttered with loose files: random downloads from the internet, to-do lists, and working directories for projects long since abandoned.
Desktop users can send all of their garbage to the trashcan—a temporary holding space where files are kept before deletion—but users who prefer to carry out their maintenance in the terminal don’t have this option. Here’s how you can change that.
What Is a Trashcan, and Why Would You Use It?
A trashcan (known as a recycle bin on Windows) is temporary storage for files you have decided to delete, but haven’t yet gotten around to deleting. It’s like the bin in your kitchen or outside your house: you can throw your junk in there, but until the trash collectors drive down your street and take it to the incinerator, you can still pull it out clean it off, and put it back where it’s supposed to be.
This is useful when you’ve accidentally thrown away your spouse’s old love letters (which look like rubbish to you but are priceless mementos to them). It’s also useful when you delete an entire directory of what appears to be unloved garbage from your drive, but later remember that deep in its subdirectories it contains the only backup of your password file, your wedding photos, and ultrasound scans of your unborn child.
Most GUI-driven desktops have had some variant of the trash system since the launch of Apple’s Lisa in 1983 (it was called a wastebasket back then), and GNOME, MATE, KDE, and XFCE desktops ship with a trashcan as standard.
Trash is so deeply embedded into the desktop environment, that more often than not, selecting a file and then hitting the Delete key, won’t actually delete the file, and will, instead, move it to the trash. If you’re coming from Windows, the OS will even ask you if you’re sure you want to send the file to the recycle bin.
Trash for Your Linux Terminal!
Trashcans, recycle bins, and wastebaskets are skeuomorphic objects. They’re software objects which graphically mimic their real-world counterparts in a way that gives clues to their intended function. A desktop trashcan looks the way it does because of the way it behaves.
While a great place to get things done with minimal fuss, the Linux terminal isn’t a graphically rich environment. Having a picture of a domestic waste receptacle would be pointless, and, even if possible, would distract from the terminal experience and consume unnecessary resources.
As standard behavior, the terminal allows you to delete items with the rm command. For instance:
rm this.file that.file another.file
The rm command accepts arguments that will recursively remove directories, empty directories, prompt before every one or three removals, or delete items even if it’s an astonishingly bad idea to do so.
The rm command gives power to the terminal user, but if you mess up and accidentally delete your entire project, then you have no way of getting them back without using a utility such as TestDisk.
trash-cli changes that by allowing you to use your Linux desktop’s Trash system from the terminal.
What Is trash-cli?
If you have a DE such as KDE, GNOME, or XFCE, trash-cli will use your system’s trashcan to junk files, while keeping the original path, deletion date, and permissions. This means you can inspect the files before permanently wiping them from your disk.
You can empty the trash in one go, or you can choose to throw individual files and folders into the virtual dumpster.
This won’t work out-of-the-box on desktops that don’t have a trash system built in, so if you’re using a window manager, such as i3, Ratpoison, or dwm, you are sadly out of luck and may need to consider an alternative solution.
Installing trash-cli on Linux
Installing trash-cli is simple, with the only prerequisite being that you have Python 2.7 or Python 3 already installed. If you don’t have Python, you can install it on Ubuntu with:
sudo apt install python3-pip
On Arch-based distros:
sudo pacman -S python-pip
On Red Hat-related distros:
sudo yum install python3 python3-wheel
…and on Fedora:
sudo dnf install python3-pip python3-wheel
Now you can install trash-cli with:
pip install trash-cli
The default package installation location will be ~/.local/bin, so if this isn’t in your PATH, add it with:
echo 'export PATH="$PATH":~/.local/bin' >> ~/.bashrc
Then reload .bashrc:
source ~/.bashrc
Using trash-cli to Manage Your Trash From the Terminal
Just as with its desktop and real-world counterparts, trash-cli is simple to use in the terminal, and using it is pretty uncomplicated. There are only four commands you need to remember.
Of course, you don’t want junk sitting in your trash indefinitely. It attracts rodents and insects, and besides, as long as it’s on your disk, you’re not saving any space. While you can use trash-empty to empty the trash whenever you remember to do it, it’s easier to set up a cronjob to do it for you.
crontab -l ; echo "@daily $(which trash-empty) 30") | crontab -
… adds a cronjob that will, every day, empty your trashcan of any items over 30 days old.
Using a Command-Line Trashcan Can Save Your Files!
It’s easy to think that when you delete something, it’s because you want it permanently removed from your system. But unless you’re very careful, it’s almost inevitable that you’ll accidentally delete something you really want to keep.
If you’re a terminal user, trash-cli gives you the same safeguards that desktop dwellers are used to, while keeping the speed and efficiency of the command line.
To avoid accidentally deleting files in the first place, consider taking steps to better manage and organize your files.