Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Soft-Delete Files from the Terminal on Linux
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-cli utility and its trash-put command. We will also discuss how to recover deleted files, list files in trash, and empty the trash.
Installing trash-cli
Before using soft delete functionality, you need to install the trash-cli package. On most Linux distributions, you can install it using the package manager
# Ubuntu/Debian sudo apt install trash-cli # CentOS/RHEL/Fedora sudo dnf install trash-cli # Arch Linux sudo pacman -S trash-cli
Commands for Soft Deletion of Files
The trash-put command is used to move a file to the 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
Listing Trash Contents
To list the contents of the trash, you can use the trash-list command. This command displays the files and directories in the trash, along with their original path, date deleted, and size
trash-list
Example output
/home/user/example.txt 2022-01-27 14:21:11 597 /home/user/example2.txt 2022-01-28 14:00:00 9192
Recovering Deleted Files
To recover a soft-deleted file, you can use the trash-restore command. When run without arguments, it presents an interactive menu
trash-restore
This will show a numbered list of deleted files, and you can select which ones to restore. You can also specify a file pattern
trash-restore example.txt
Emptying the Trash
To empty the trash, use the trash-empty command. This permanently deletes all files in the trash, so use it with caution
trash-empty
You can also specify a number of days to empty files older than that period. For example, to empty all files that have been in the trash for more than 30 days
trash-empty 30
Permanently Deleting from Trash
If you want to permanently delete specific files from the trash without emptying everything, you can use the trash-rm command
trash-rm example.txt
You can also use wildcards to delete multiple files at once
trash-rm *.txt
Warning: Be careful when using this command, as it permanently deletes the specified files and they cannot be restored.
Customizing Trash Location
By default, the trash is located at $HOME/.local/share/Trash, but you can customize this location by setting environment variables
export XDG_DATA_HOME=/custom/path/data export XDG_CONFIG_HOME=/custom/path/config
The trash will then be located at $XDG_DATA_HOME/Trash. You can add these exports to your shell profile (~/.bashrc or ~/.zshrc) to make them permanent.
Comparison with Standard rm Command
| Command | Action | Recovery Possible | Safety Level |
|---|---|---|---|
rm file.txt |
Permanent deletion | No (without special tools) | Dangerous |
trash-put file.txt |
Move to trash | Yes (with trash-restore) | Safe |
rm -rf directory/ |
Force recursive deletion | No | Very dangerous |
trash-put directory/ |
Move directory to trash | Yes | Safe |
Conclusion
The trash-cli utility provides a safer alternative to the standard rm command by implementing soft delete functionality on Linux. It allows for easy recovery of accidentally deleted files through trash-put, trash-list, and trash-restore commands. Always exercise caution when using trash-empty or trash-rm, as these operations are irreversible.
