How to use the command trash (with examples)
- Linux
- December 25, 2023
The trash
command is a Linux utility that allows users to manage the trashcan or recycling bin. It provides a safer and more convenient way of deleting files, as they are sent to the trash instead of being permanently deleted immediately. The trash
command also supports various operations such as listing the files in the trash, restoring files from the trash, and emptying the trash.
Use case 1: Delete a file and send it to the trash
Code:
trash path/to/file
Motivation: This use case is helpful when you want to delete a file, but still have the ability to recover it if needed. Instead of using the rm
command to delete the file permanently, using trash
sends the file to the trashcan/recycling bin, allowing you to easily restore it if necessary.
Explanation: trash
is the main command, followed by the path to the file you want to delete. The file will be moved to the trashcan/recycling bin instead of being permanently deleted.
Example output: The file path/to/file
is deleted and moved to the trash.
Use case 2: List all files in the trash
Code:
trash-list
Motivation: When you want to see what files you have sent to the trashcan/recycling bin, using the trash-list
command provides a convenient way to view the contents of the trash.
Explanation: The trash-list
command lists all the files that are currently in the trash.
Example output:
1. /path/to/file1
2. /path/to/file2
3. /path/to/file3
Use case 3: Interactively restore a file from the trash
Code:
trash-restore
Motivation: This use case is useful when you want to restore a file that you have sent to the trash by mistake or because you need to use it again.
Explanation: The trash-restore
command opens an interactive menu that allows you to select a file from the trashcan/recycling bin and restore it to its original location.
Example output:
1. /path/to/file1
2. /path/to/file2
3. /path/to/file3
Select the file you want to restore (1-3):
Use case 4: Empty the trash
Code:
trash-empty
Motivation: When the trashcan/recycling bin becomes cluttered with unnecessary files, using the trash-empty
command allows you to permanently delete all the files in the trash, freeing up storage space.
Explanation: The trash-empty
command empties the trashcan/recycling bin by permanently deleting all the files in it.
Example output: The trashcan/recycling bin is emptied, and all files are permanently deleted.
Use case 5: Permanently delete all files in the trash which are older than 10 days
Code:
trash-empty 10
Motivation: If you want to automatically remove files from the trashcan/recycling bin that are older than a certain number of days, this use case is helpful. It allows you to keep the trashcan/recycling bin clean and avoid accumulating unnecessary files.
Explanation: The trash-empty
command can be used with an argument specifying the number of days. In this example, 10
is the argument, which indicates that files older than 10 days should be permanently deleted.
Example output: All files in the trash that are older than 10 days are permanently deleted.
Use case 6: Remove all files in the trash, which match a specific blob pattern
Code:
trash-rm "*.o"
Motivation: When you want to remove files from the trashcan/recycling bin that match a specific pattern, using trash-rm
with a blob pattern is convenient. It helps you remove multiple files at once based on their filename extension or other file attributes.
Explanation: The trash-rm
command is used with a blob pattern argument, *.o
in this case, which matches all files with the .o
extension. It removes all files in the trashcan/recycling bin that match the pattern.
Example output: All files in the trash that have the .o
extension are permanently deleted.
Use case 7: Remove all files with a specific original location
Code:
trash-rm /path/to/file_or_directory
Motivation: In situations where you want to delete all files in the trashcan/recycling bin that originally belonged to a specific location, using trash-rm
with the path argument allows you to remove all those files at once.
Explanation: The trash-rm
command is used with the path to the original file or directory that you want to remove from the trash. It deletes all files in the trashcan/recycling bin that were originally located at the specified path.
Example output: All files in the trash that were originally located at /path/to/file_or_directory
are permanently deleted.
Conclusion:
The trash
command provides an efficient way to manage the trashcan/recycling bin on Linux systems. By using the various options, such as deleting files to the trash, listing the files in the trash, restoring files, emptying the trash, or removing specific files based on patterns or original locations, users have control over their deleted files and can recover them when needed, with the added benefit of freeing up disk space by permanently deleting unnecessary files.