Linux and Unix Trash: List trashed files, Restore trashed files, Empty trash

The Linux and Unix Trash is the equivalent of the Window’s Recicle Been. Every user has it’s own trash in Linux and Unix Systems.

By default, the trash is located in the user’s ~/.local/share/Trash. The ~/.local/share/Trash directory contains two folders, files and info.

The ~/.local/share/Trash/files folder contains all the files and folders inside the Trash.

Example:

$ ls -l files ~/.local/share/Trash/files
-rw-rw-r-- 1 razvan razvan    0 2012-08-08 14:22 23
-rwxrwxrwx 1 razvan razvan  220 2012-08-15 14:23 a
drwxrwxrwx 2 razvan razvan 4096 2012-07-25 02:18 abc
drwxrwxrwx 2 razvan razvan 4096 2012-08-10 00:26 abcd
-rwxrwxrwx 1 razvan razvan   14 2012-07-04 01:28 b
-rwxrwxrwx 1 razvan razvan    7 2012-07-04 01:28 b2

The ~/.local/share/Trash/info folder contains some text files having the files or folder’s inside the trash names and the .trashinfo extension.

Example:

$ ls -l ~/.local/share/Trash/info
-rw------- 1 root root 67 2012-08-15 15:38 23.trashinfo
-rw------- 1 root root 69 2012-08-15 15:38 abcd.trashinfo
-rw------- 1 root root 68 2012-08-15 15:38 abc.trashinfo
-rw------- 1 root root 66 2012-08-15 15:38 a.trashinfo
-rw------- 1 root root 67 2012-08-15 15:38 b2.trashinfo
-rw------- 1 root root 66 2012-08-15 15:38 b.trashinfo

Each *.trashinfo file contains the path and the deletion date of the file in the trash, representing.

Example:

$ cat ~/.local/share/Trash/info/23.trashinfo
[Trash Info]
Path=/home/razvan/23
DeletionDate=2012-08-15T15:38:05

Install the trash-cli package

Install trash-cli on Debian, Ubuntu, Linux Mint, Knoppix: sudo apt-get install trash-cli

Install trash-cli on Fedora and CentOS: sudo yum install trash-cli

Install trash-cli on openSUSE: sudo zypper install trash-cli

Now that we have installed the trash-cli package, let’s start using it.

Find out the trash size:

$ du -hs ~/.local/share/Trash

View the files inside the trash:

You can easily find out the files and folders inside the trash, with list-trash:

$ list-trash
2012-08-15 15:10:27 /home/razvan/one
2012-08-15 15:09:59 /home/razvan/foo
2012-08-15 15:26:42 /home/razvan/1

Send files and folders to trash:

To send files and folders to trash, use the trash command: trash /path/to/file_or_folder.

$ trash -v /home/razvan/myfifo
`/home/razvan/myfifo' trashed in ~/.local/share/Trash

How to empty the trash:

Emptying the trash is simple. Use empty-trash.

To remove files that have been to trash for X days, use empty trash X, where X is the number of days.

$ empty-trash 7

You can also empty the trash by hand: sudo rm -rf ~/.local/share/Trash/*.

Restoring files from trash:

To restore files from trash, use restore-trash and type the number representing the file you want to restore.

$ restore-trash
0 2012-08-15 15:38:54 /home/razvan/b
1 2012-08-15 15:38:05 /home/razvan/23
2 2012-08-15 15:38:54 /home/razvan/abc
3 2012-08-15 15:38:54 /home/razvan/a
4 2012-08-15 15:38:54 /home/razvan/b2
5 2012-08-15 15:38:54 /home/razvan/abcd
What file to restore [0..5]:

If I have more than one file to restore, I cd to the ~/.local/share/Trash/files directory, and move the files I need to restore by hand.

I have this cron job configured for emptying the trash after every reboot: @reboot empty-trash > /dev/null

To cron an empty trash job at reboot, type crontab -e in your terminal and paste this:

@reboot empty-trash > /dev/null

Tagged with: , , , , , ,
Posted in The Linux and Unix Articles!
One comment on “Linux and Unix Trash: List trashed files, Restore trashed files, Empty trash
  1. maxmoon says:

    The trash-cli doesn’t contain the commands empty-trash and list-trash. All commands start with trash-*, like trash-list, trash-empty, trash-put, etc.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Subscribe

  

Subscribe to get the latest Linux news and how to guides directly on your e-mail!