Where Do Files Go When the rm Command Is Executed?

The data itself is not destroyed or shredded; it is only unlinked from the filesystem that references the data and indicates the block as free when you remove it using the rm command.

In this scenario, recovering the files becomes too complex, and it’s a good choice not to rewrite something on your system (ex: copying, moving, installing), which overwrites the unlinked block.

Delete your files wisely! It’s not like deleting something in Windows and hoping to recover it from the Recycle Bin; once you rm a file or directory, there is no easy way to undo it.

Suppose you delete any files or directories from the desktop environment file manager (Nautilus, Dolphins, etc). In that case, you can easily recover the file from the trash.

The location of trash when files and directories are trashed is at the ~/.local/share/Trash/files/ path. You can try to delete a file using a file manager and check this directory’s content.

How Files are Deleted in the Desktop Environment File Manager

To perform the demonstration, I will first create a new file in the current location using the touch command, as shown below.

Creating a new file in the current directory
Creating a new file in the current directory

Once the file is created, I will remove it using the nautilus file manager, as shown below.

Removing file using the nautilus file manager
Removing files using the nautilus file manager

After the file is deleted, you can directly navigate to the trash section, but I will show you the content of the trash from the terminal app by listing the content of ~/.local/share/Trash/files/ path, as shown below.

$ ls ~/.local/share/Trash/files/ 

Below is the output of the above command.

Reading the content of trash path
Reading the content of the trash path

Note: The current trash path only lists the files deleted by the logged user. You can’t read the deleted content of other users only if you have root access or sudo privileges.

As you can see, the trash path listed the file we deleted a few steps back. But what if you want to remove the files and directories from your terminal and want them to move into the trash directory instead of being unlinked.

Removing Files and Directories and Moving them into the Trash Path from the Command-Line

You can use the gio command to move files or directories into the trash via command-line, which is installed by default in Ubuntu. This tool was renamed before it was called gvfs-trash from the gvfs-bin package, which is now deprecated.

Execute the below command to move files and directories into the trash.

$ gio trash file.txt
$ ls ~/.local/share/Trash/files/

Below is the output of the above command.

Removing Files and Directories and Moving them into Trash Path from the Command-Line
Removing Files and Directories and Moving them into the Trash Path from the Command-Line

If you have multiple files and directories inside the directory, you can forcefully remove them without prompting using the -f or --force option as shown below.

$ gio trash -f /tmp/*

You can even empty the trash with the --empty option, as shown below.

$ ls ~/.local/share/Trash/files/
$ gio trash --empty
$ ls ~/.local/share/Trash/files/

Below is the output of the above command.

Empty the Trash
Empty the Trash

Wrap Up

Today, you have learned how files are removed and unlinked in the Linux system from the rm command and desktop environment file manager and how you can use the gio trash command to move the file into the trash instead of unlinking it.

Please let us know in the comment section if you have any queries regarding this topic.

Leave a Reply