Home >Operation and Maintenance >Linux Operation and Maintenance >How to delete files using Linux command line? (code example)

How to delete files using Linux command line? (code example)

青灯夜游
青灯夜游Original
2019-03-02 15:30:554449browse

There are many methods to delete files in Linux. This article will introduce two methods to delete files: the rm command and the trash-cli tool. I hope it will be helpful to you. [Video tutorial recommendation: Linux tutorial]

How to delete files using Linux command line? (code example)

##rm command

rm The command is a command that most people often use when deleting files. It is used to delete a file or directory.

The syntax of the rm command is as follows:

rm /path/to/file

You can also delete all files in the folder and subfolders as follows:

rm -R /path/to/folder

Files Once deleted via the rm command, it cannot be restored, so this command must be used with extreme caution. However, we can prevent accidental file deletion to some extent by using various methods. For example, if you want to delete multiple files, you can be prompted before each file is deleted to ensure that the correct file is deleted.

rm -i /path/to/file

Whenever you run the above command, a message will be displayed asking if you are sure you want to delete the file.

But if when deleting dozens of files, each file receives a prompt, and we need to press "y" repeatedly to confirm, this will be very troublesome.

We can use the following command, which only prompts when more than 3 files need to be deleted or deleted recursively.

rm -i /path/to/file

trash-cli tool

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

By default, trash-cli is usually not installed on Linux, we need to install it before it can be used.

Installation (using package manager)

If you are using a 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 a centos-based system, you need to use the yum command:

sudo yum install -y trash-cli

How to move files to the Recycle Bin?

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

trash /path/to/file

The file is not completely deleted, but is moved to the Recycle Bin in the same way as the Windows Recycle Bin middle.

If you provide a folder name to the trash command, it will send the folder and all files in the folder to the recycle bin.

How to list files in the Recycle Bin?

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

trash-list

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

How to recover files from Recycle Bin?

The man page for the trash command states that to recover files you should use the following command:

trash-restore

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

An alternative to file restoration is to use the restore-trash command, as shown below:

restore-trash

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

How to empty the Recycle 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 in the Recycle Bin, you can 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, just specify that number using the trash-empty command. Example:

trash-empty 7

Delete all files that have existed in the Recycle Bin for 7 days.

The above is the entire content of this article, I hope it will be helpful to everyone's study. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of How to delete files using Linux command line? (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn