https://www.lifewire.com/delete-files-using-linux-command-line-4119814
alias rm=trash
Introduction
This guide will show you how to safely delete files from your system.
Now you might be thinking that the whole point of deleting files is to get rid of them so how safe can you be. Imagine you executed a command intended to remove all files from a particular folder and instead of deleting just those files it deleted all the files in the sub-folders as well.
Which Command Should You Use To Delete Files
There are a number of ways you can use to delete files within Linux and in this guide I will show you two of them:
- rm
- trash-cli
The rm Command
Most people tend to use the rm command when deleting files and out of the two explained here, this is the most brutal command. If you delete a file using the rm command it is very difficult (although not necessarily impossible) to recover that file.
The syntax of the rm command is as follows:
rm /path/to/file
You can also delete all the files in a folder and sub folders as follows:
rm -R /path/to/folder
As mentioned previously the rm command is pretty much final. You can protect yourself to some extent however by using various switches.
For instance if you are deleting multiple files you can get a prompt before each file is deleted so that you can be sure you are deleting the correct files.
rm -i /path/to/file
Whenever you run the above command a message will appear asking you whether you are sure you want to delete the file.
If you are deleting dozens of files receiving a prompt for each one can get tedious and you might just press “y” repeatedly and still end up accidentally deleting the wrong file.
You can use the following command which only prompts when you are deleting more than 3 files or you are deleting recursively.
rm -I /path/to/file
The rm command is possibly the one you want to use least if you wish to be careful.
Introducing trash-cli
The trash-cli application provides a command line trash can. It isn’t usually installed by default with Linux so you will have to install it from the repositories of your distribution.
sudo apt-get install trash-cli
sudo yum install trash-cli
If you are using openSUSE use the zypper command:
sudo zypper -i trash-cli
Finally if you are using an Arch based distribution use the pacman command:
sudo pacman -S trash-cli
How To Send A File To The Trash Can
To send a file to the trash can use the following command:
trash /path/to/file
The file is not fully deleted but instead sent to a trash can in the same way as the Windows recycle bin.
If you supply the trash command to a folder name it will send the folder and all of the files in the folder to the recycle bin.
How To List The Files In The Trash Can
To list the files in the trash you can run the following command:
trash-list
The results returned include the original path to the file and the date and time the files were sent to the trash can.
How To Restore Files From The Trash Can
The manual page for the trash command states that to restore a file you should use the following command:
trash-restore
You may however receive a command not found error if you run this command.
The alternative to trash-restore is restore-trash as follows:
restore-trash
The restore-trash command will list all of the files in the trash with a number next to each one. To restore a file simply enter the number next to the file.
How To Empty The Trash Can
The main issue with the trash can approach is that the files still take up valuable drive space. If you are satisfied that everything in the trash can is no longer required you can run the following command to empty the trash.
trash-empty
If you want to delete all the files that have been in the trash for a certain number of days simply specify that number with the trash-empty command.
trash-empty 7
Summary
Most graphical desktop environments provide a trash can or recycle bin, but when you are using the command line you are left to your own wit and cunning.
To be safe I recommend using the trash-cli program.
赞呀,好人好文章,不顶不行呀