Linux command line delete file operation method

  • 2021-01-06 00:49:19
  • OfStack

rm command

The rm command is the one most people use when deleting files. It is used to delete 1 file or directory.

The syntax of the rm command is as follows:

[

rm /path/to/file

]

You can also delete all files in folders and subfolders, as shown below:

[

rm -R /path/to/folder

]

Once a file is deleted by the rm command, it cannot be recovered, so this command must be used with great care. However, we can prevent accidental deletion of files to some extent by using various methods. For example, if you are deleting multiple files, you can be prompted before each file is deleted to ensure that the correct files are deleted.

[

rm -i /path/to/file

]

Each time you run the above command, a message is displayed asking if you are sure you want to delete the file.

But if each file is prompted when deleting several 10 files, we need to press "y" repeatedly to confirm, which can be troublesome.

We can use the following command, which is prompted only if we need to delete more than three files or recursive deletion.

[

rm -I /path/to/file

]

trash - cli tools

ES46en - ES47en is a Recycle Bin command line utility that provides a command line Recycle Bin. We can move files to the recycle bin or restore deleted files by using the trash-cli tool.

By default, trash-cli is not normally installed on Linux, we need to install it before we can use it.

Installation (using the package manager)

If you are using an Debian based system, such as Ubuntu or Mint, you need to use the apt-get command:

[

sudo apt-get install trash-cli

]

If you are using an centos-based system, you need to use the yum command:

[

sudo yum install -y trash-cli

]

How do I move a file to the Recycle Bin?

To move a file to the Recycle Bin, use the following command:

[

trash /path/to/file

]

This file is not completely deleted, but is moved to the Recycle Bin in the same manner as the Windows Recycle Bin.

If you supply the folder name to the trash command, it sends the folder and all the files in the folder to the Recycle Bin.

How do I list the files in the Recycle Bin?

To list the files in the Recycle Bin, run the following command:

[

trash-list

]

The results returned include: the original path of the file, the date and time the file was moved to the recycle bin.

How do I recover a file from the Recycle Bin?

The man page of the trash command states that to restore a file, you should use the following command:

[

trash-restore

]

However, if you run this command, you may receive a "Command not found" error.

An alternative method of restoring files is to use the restore-trash command, as follows:

[

restore-trash

]

The restore-trash command will list all the files in the Recycle Bin, with 1 number next to each file. To restore the file, simply enter the number next to the file.

How do I empty the recycling bin?

The main problem with the Recycle Bin is that files still take up valuable drive space. If you really don't need all the contents of the recycle bin, run the following command to empty the recycle bin.

[

trash-empty

]

If you want to delete all files that have been in the Recycle Bin for a specific number of days, simply specify that number using the trash-empty command. Ex. :

[

trash-empty 7

]

Delete any files that have been in the Recycle Bin for 7 days.


Related articles: