codea.live
Search…
Bash - Trash Command
Make a trash command in bash, which sends files to a recyle bin, instead of deleting them.
For the solution, we will separate each task into separate commands, and then combine then into one trash command.

Move Files Into Trash Folder

Make Trash Folder

In the default directory, we will make a directory for storing trash, then a directory for storing logs.
1
$ mkdir ~/trash ~/logs
Copied!

Move Items to Trash

This will move a file to trash. We will make this configurable later.
1
$ mv file_name.txt ~/trash
Copied!

Log Actions in a File

Add a Timestamp

This will return a timestamp in the format: HH:MM:SS.
1
$ date | cut --delimiter " " --fields 4
2
13:37:13
Copied!

Log Files

1
$ date 1>> timestamp
Copied!

[Bonus] Make It a Script

Add Script to PATH

1
$
2
$ mkdir ~/bin
Copied!
~/bin/trash
1
$FILE_NAME=""
2
​
Copied!
How to add a directory to the PATH?
Ask Ubuntu
Last modified 9mo ago