How to Send a File/folder to Trash Instead of Permanently Deleting It Using Command-Line

When you want to remove/delete a file/folder using command-line, you have to use rm command. This permanently delete/remove a file/folder without giving any warning. This may cause accidental removal of an important file/folder. So, in case you’re relying on terminal or command-line for your day-to-day work, you have to make sure you’re dealing with the files/folders in a safe hand. You can temporarily send files/folders to trash using command-line as you do while using GUI based file manager e.g. Nautilus, etc. For this use case, you have to create an alias. Follow the procedures below:

  • Open .bashrc using any text editor. I have used here gEdit.
gedit ~/.bashrc
  • .bashrc will open with gEdit. Add the following line at the end of the file,

alias trash=”mv -t ~/.local/share/Trash/files –backup=t”

  • Save this file and close gEdit. Close the terminal window and then open a new terminal.
  • You can test your alias now. Here we have created an alias named trash that would send any file/folder to Trash/files folder.

Say you want to delete filename.pdf and for that purpose use the following command:

trash filename.pdf

This would delete the file filename.pdf. You can delete folders in the same way. Multiple files/folders can also be deleted in the same way.

trash filename1 filename2

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s