How to Manage the Recycling Bin using 'trash-cli' (with examples)
- Linux
- December 17, 2024
The trash-cli
command is an essential tool for users looking for a more powerful way to manage their trashcan or recycling bin directly from the command line. It allows you to move files to the trash, list them, restore them, and even delete them permanently under certain criteria, providing a convenient alternative to the traditional methods of file management on desktop systems.
Use case 1: Sending a File to the Trash
Code:
trash path/to/file
Motivation:
Sometimes you may need to remove files quickly without permanently deleting them, ensuring you can restore them later if needed. The trash
command provides a safer way to delete files because it moves them to the trash rather than permanently erasing them from the filesystem.
Explanation:
trash
: The command to initiate the trash action.path/to/file
: This is the path to the file you want to move to the trash. Substitute this with the actual file path you wish to remove temporarily.
Example Output:
No output is typically displayed when you successfully move a file to trash; the command completes quietly. However, you can confirm the file is in the trash by using trash-list
to check the contents.
Use case 2: Listing All Files in the Trash
Code:
trash-list
Motivation:
Checking the contents of your trash can be crucial, especially if you’re looking to recover a file or manage the volume of files you’ve discarded. Knowing what’s in your trash helps you keep track and make informed decisions about permanent deletion.
Explanation:
trash-list
: This command lists all the files currently residing in your trash. It provides an overview, displaying information such as the file name, original location, and deletion date.
Example Output:
1. filename.txt - /original/location/ - 2023-11-01
2. image.png - /images/ - 2023-11-02
Use case 3: Interactively Restoring a File from the Trash
Code:
trash-restore
Motivation:
Sometimes you accidentally move important files to the trash and must restore them to their original locations. The trash-restore
command provides an interactive way to review and recover those files.
Explanation:
trash-restore
: Launches an interactive session to sift through deleted files and decide which ones you’d like to restore.
Example Output:
Which file would you like to restore?
1. filename.txt
2. image.png
Enter the number of the file you wish to restore:
Use case 4: Emptying the Trash
Code:
trash-empty
Motivation:
Periodically, it’s essential to clear out accumulated files in your trash to free up system storage. After reviewing the contents, you can use trash-empty
to discard all files permanently and reclaim valuable space.
Explanation:
trash-empty
: This command deletes all files in the trash permanently, making the space these files occupied available again.
Example Output:
The trash has been emptied.
Use case 5: Permanently Deleting Files Older Than 10 Days
Code:
trash-empty 10
Motivation:
This command is particularly useful for automated cleanup, setting a criteria-based approach to file deletion. It ensures that only files which have been in the trash for a specific period are removed, helping to manage trash contents more intelligently without accidentally losing recent items.
Explanation:
trash-empty
: Initiates the process of emptying the trash.10
: Limits the deletion process to files that are older than 10 days, minimizing data loss risk from recent deletions.
Example Output:
Files older than 10 days have been deleted.
Use case 6: Removing Files Matching a Pattern
Code:
trash-rm "*.o"
Motivation:
There are scenarios where you need to selectively delete files based on patterns, such as temporary or object files generated during the build process. This selective deletion helps maintain a cleaner and more organized set of files without manually sifting through the trash.
Explanation:
trash-rm
: This command removes files from the trash."*.o"
: A pattern matching all files with a.o
extension, ensuring only those files matching the criteria are removed. Adjust the pattern based on your requirements.
Example Output:
Removing file1.o
Removing file2.o
Use case 7: Removing Files with a Specific Original Location
Code:
trash-rm /path/to/file_or_directory
Motivation:
Managing files based on their original location is useful for recovering storage space or organizing your trash by removing unnecessary files from known paths. This is particularly handy when you want to target files from a previous directory cleanup operation.
Explanation:
trash-rm
: Similar to earlier, this command facilitates the removal of files from the trash./path/to/file_or_directory
: The specific original path of the files to be removed, allowing focused cleaning efforts.
Example Output:
Removing specific_file from /path/to/
Conclusion:
The trash-cli
tool offers powerful file management functionalities beyond what typical GUI interfaces provide. By leveraging these command-line actions you can efficiently manage, restore, and delete files from your system’s trash, ensuring a more secure and organized approach to file management.