Soft Delete Files from the Terminal on Linux



Introduction

When working with files in the terminal on Linux, there may be times when you want to delete a file but also keep a copy of it just in case. This is where the concept of "soft delete" comes into play. A soft delete allows you to move a file to a designated recycle bin or trash, instead of permanently deleting it. That way, if you accidentally delete a file, you can easily get it back.

In this article, we will explore how to delete files from the terminal on Linux using the "trash-put" command. We will also discuss how to recover deleted files, list files in trash and empty the trash.

Commands for temporary or soft deletion of files

The "trash-put" command is used to move a file to the trash or trash. The basic syntax is as follows ?

$ trash-put [file]

For example, to soft-delete a file named "example.txt", the command would be ?

$ trash-put example.txt

You can also use wildcards to delete multiple files at once. For example, to delete all files with the ".txt" extension, the command would be ?

$ trash-put *.txt

Recover deleted files

To recover a soft-deleted file, you can use the "trash-restore" command. The basic syntax is as follows ?

$ trash-restore [file]

For example, to restore a file named "example.txt", the command would be ?

$ trash-restore example.txt

You can also use wildcards to restore multiple files at once. For example, to restore all files with the ".txt" extension, the command would be ?

$ trash-restore *.txt

Emptying the Trash Bin

To empty the trash, you can use the "trash-empty" command. This command will permanently delete all files in the trash, so use it with caution. The basic syntax is as follows ?

$ trash-empty

It is important to note that emptying the trash bin permanently deletes all of the files and directories stored in it, and they cannot be restored.

You can also specify a number of days to empty the trash bin. For example, to empty all files that have been in the trash bin for more than 3 days, use the following command ?

$ trash-empty 3

This command will empty all files and directories older than three days from the trash bin.

Listing trash content

To list the contents of the trash, you can use the "trash-list" command. This command will display the files and directories in the trash, along with their original path, date deleted, and size. The basic syntax is as follows ?

$ trash-list

For example, the output of the command ?

/home/user/example.txt 	Wed Jan 27 14:21:11 2022	597
/home/user/example2.txt	Mon Jan 28 14:00:00 2022	9192

Customizing trash location

By default, the trash is located at "$HOME/.local/share/Trash", but you can customize the location of the trash. You can set the XDG_DATA_HOME and XDG_CONFIG_HOME environment variables to specify a custom location for the trash.

For example, to set the trash location to /mnt/trash

$ export XDG_DATA_HOME=/mnt/trash
$ export XDG_CONFIG_HOME=/mnt/trash

You can then use the trash-put, trash-list, trash-empty, trash-restore commands as usual.

Permanently delete files

If you want to permanently delete a file from the trash and skip the recovery process, you can use the "trash-rm" command. This command will remove a file from the trash without moving it to a recycle bin. The basic syntax is as follows ?

$ trash-rm [file]

For example, to permanently delete a file named "example.txt" from the trash, the command would be ?

$ trash-rm example.txt

You can also use wildcards to delete multiple files at once. For example, to permanently delete all files with the ".txt" extension, the command would be ?

$ trash-rm *.txt

Be careful when using this command, as it will permanently delete the specified files and you will not be able to restore them.

Conclusion

Overall, the trash command is a powerful tool for dealing with deleted files on Linux. It allows for easy recovery of accidentally deleted files, as well as the ability to permanently delete files. Using the trash-put command, files can be moved to the trash, the trash-list command can be used to list the contents of the trash, the trash-restore command can be used to restore a file from the trash, the trash command -empty can be used to empty the trash and the trash-size command can be used to control the size of the trash. Also, the trash-rm command can be used to permanently delete files from the trash. With these commands, managing deleted files in Linux becomes very easy. Remember, always be careful when using the trash-rm command, as it will permanently delete the specified files and you will not be able to restore them.

Updated on: 2023-02-13T17:29:02+05:30

563 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements