If you work on the Linux command line long enough, you will eventually delete a file by accident. Unlike using a GUI, there is no Trash folder (Recycle Bin) in a terminal. Your file is gone. Sure, you may be able to restore the file from disk, but it is not trivial or fast. Enter Trash-CLI, the command line interface to the Linux trash can. In this Linux quick tip we will show you how to install and use this nifty little utility.
Installing Trash-CLI
Some distributions will have trash-cli in their default repositories.
Install Trash-CLI on Fedora
You can use the DNF package manager on your Fedora system for installation like so:
sudo dnf install trash-cli
Install Trash-CLI on Ubuntu
Using APT on Ubuntu like so:
sudo apt install trash-cli
Install Using the PIP Package Manager for Python
If the package does not exist in your repos, you can easily install it using the PIP Python Package Manager. First, ensure you have PIP installed, then use the pip3
command to install the trash-cli package. Here is an example using yum.
sudo yum install python3-pip -y; pip3 install trash-cli
Using the Trash-CLI Command Line Trash Can
The trash-cli package provide several different commands.
- trash / trash-put - Used to send a specific file or directory to the trash
- trash-list - Used to list the files in the trash
- trash-empty - Used to empty your trash
- trash-restore - Used to restore files from the trash back to the filesystem
- trash-rm - Used to permanently delete a file or files from the trash
Sending a File to the Trashcan From Terminal
You can send a file to the trash by passing it's name to the trash
command.
[savona@putor ~]$ trash floyd.txt
You can use the -v
for verbose output to see what is being done.
[savona@putor ~]$ trash -v floyd.txt
trash: Volume of file: /home
trash: Trash-dir: /home/savona/.local/share/Trash from volume: /home
trash: 'floyd.txt' trashed in ~/.local/share/Trash
List Contents of the Trash from Terminal
Use the trash-list
command to see a listing of files in the trashcan. It will also show you the date and time the file was sent to the trash.
[savona@putor ~]$ trash-list
2020-03-04 23:18:42 /home/savona/floyd.txt
Restoring Trashed Files from the Command Line
To restore files from the trash can you can simply call the trash-restore
command. You will be presented with a numbered list of files in the trash. Simply select the number of the file you want to restore.
[savona@putor ~]$ trash-restore
0 2020-03-04 23:25:11 /home/savona/test.txt
1 2020-03-04 23:25:08 /home/savona/mysongs
2 2020-03-04 23:25:02 /home/savona/rock.csv
3 2020-03-04 23:22:12 /home/savona/floyd.txt
4 2020-03-04 23:25:14 /home/savona/index.html
5 2020-03-04 23:25:50 /home/savona/songs.csv
What file to restore [0..5]: 3
NOTE: The trash-restore
command will only show you files below the current directory.
[savona@putor Documents]$ trash-list
2020-03-04 23:29:24 /home/savona/Documents/test
2020-03-04 23:25:11 /home/savona/test.txt
2020-03-04 23:25:08 /home/savona/mysongs
2020-03-04 23:25:02 /home/savona/rock.csv
2020-03-04 23:27:31 /home/savona/floyd.txt
2020-03-04 23:25:14 /home/savona/index.html
2020-03-04 23:25:50 /home/savona/songs.csv
[savona@putor Documents]$ trash-restore
0 2020-03-04 23:29:24 /home/savona/Documents/test
What file to restore [0..0]:
In the above output we are in the /home/savona/Documents
directory. Therefore the trash-restore
command only shows us files from trash in or below to that directory. If we move up a directory to /home/savona
it will show all files in our home (or files below the current directory).
[savona@putor Documents]$ cd ..
[savona@putor ~]$ trash-restore
0 2020-03-04 23:29:24 /home/savona/Documents/test
1 2020-03-04 23:25:11 /home/savona/test.txt
2 2020-03-04 23:25:08 /home/savona/mysongs
3 2020-03-04 23:25:02 /home/savona/rock.csv
4 2020-03-04 23:27:31 /home/savona/floyd.txt
5 2020-03-04 23:25:14 /home/savona/index.html
6 2020-03-04 23:25:50 /home/savona/songs.csv
What file to restore [0..6]:
You can bypass this behavior by specifying the directory of the file you want to restore.
[savona@putor Documents]$ trash-restore /home/savona/
0 2020-03-04 23:29:24 /home/savona/Documents/test
1 2020-03-04 23:25:11 /home/savona/test.txt
2 2020-03-04 23:25:08 /home/savona/mysongs
3 2020-03-04 23:25:02 /home/savona/rock.csv
4 2020-03-04 23:27:31 /home/savona/floyd.txt
5 2020-03-04 23:25:14 /home/savona/index.html
6 2020-03-04 23:25:50 /home/savona/songs.csv
What file to restore [0..6]:
Permanently Delete a File or Files from Trash
The trash-rm
command allows you to permanently delete files from the trash. It accepts globbing patterns that allows you to delete multiple files. In this example we use the asterisks wildcard to delete all files that start with the word test.
[savona@putor ~]$ trash-list
2020-03-04 23:29:24 /home/savona/Documents/test
2020-03-04 23:25:11 /home/savona/test.txt
2020-03-04 23:25:08 /home/savona/mysongs
2020-03-04 23:25:02 /home/savona/rock.csv
2020-03-04 23:27:31 /home/savona/floyd.txt
2020-03-04 23:25:14 /home/savona/index.html
2020-03-04 23:25:50 /home/savona/songs.csv
[savona@putor ~]$ trash-rm test*
[savona@putor ~]$ trash-list
2020-03-04 23:25:08 /home/savona/mysongs
2020-03-04 23:25:02 /home/savona/rock.csv
2020-03-04 23:27:31 /home/savona/floyd.txt
2020-03-04 23:25:14 /home/savona/index.html
2020-03-04 23:25:50 /home/savona/songs.csv
Empty Trashcan from the Command Line
You can empty the trash by using the trash-empty
command.
[savona@putor ~]$ trash-empty
[savona@putor ~]$ trash-list
[savona@putor ~]$
Optionally, you can pass a number to trash-empty
to only remove files that have been placed in the trash x number of days ago.
Example of removing files from the trash that are 30 days old:
[savona@putor ~]$ trash-empty 30
Create an Alias for Trash
My first instinct was to alias rm to trash so every time I delete a file it is placed in the trash. However, the developer of the package thinks it's a bad idea.
Although the interface of trash-put seems to be compatible with rm, it has different semantics which will cause you problems. For example, while rm requires -R for deleting directories trash-put does not.
- Andrea Francia
Conclusion
The Trash-CLI utility adds a functionality to the command line that I really enjoy. I have installed this on my workstation and I am sure it will save my behind sooner or later. I have been using Linux for two decades and I still occasionally delete a file and instantly regret it.
Resources and Links
Leave a Reply Cancel reply
This site uses Akismet to reduce spam. Learn how your comment data is processed.
1 Comment
Join Our Newsletter
Categories
- Bash Scripting (17)
- Basic Commands (50)
- Featured (7)
- Just for Fun (5)
- Linux Quick Tips (98)
- Linux Tutorials (65)
- Miscellaneous (15)
- Network Tools (6)
- Reviews (2)
- Security (32)
- Uncategorized (1)
I can see how this would be really useful. I am going to give it a go