Practical method of deleting files from Linux command line

Practical method of deleting files from Linux command line

rm Command

The rm 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 a folder and subfolders as follows:

rm -R /path/to/folder

Once a file is deleted using the rm command, it cannot be restored, so you must use this command with extreme caution. 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 deleting each file so that you can ensure that you are deleting the correct files.

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 we delete dozens of files, each file will be prompted and we need to press "y" repeatedly to confirm, which will be very troublesome.

We can use the following command, which will prompt only when more than 3 files need to be deleted or when recursive deletion is required.

rm -I /path/to/file

trash-cli tool

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

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

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 do I 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.

If you provide a folder name to the trash command, it will send the folder and all the 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 when 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 restore files you should use the following command:

trash-restore

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

An alternative to file restoration is to use the restore-trash command as follows:

restore-trash

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

How do I empty the Recycle Bin?

The main problem with the Recycle Bin is that the 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 certain number of days, just specify that number with the trash-empty command. example:

trash-empty 7

Delete all files in the Recycle Bin that are older than 7 days.

You may also be interested in:
  • How to deal with the prompt "Operation not permitted" when deleting files in Linux
  • Linux commands to delete folders and files (forced deletion including non-empty files)
  • How to add/delete file permissions in Linux (Ubuntu)
  • Linux system command to delete folders and files
  • Solution to the problem that FTP account cannot delete folders in Linux
  • Linux bash deletes the line containing "specified content" in the file function example
  • Linux batch delete file command by time (delete files N days ago)
  • Detailed explanation of lsof command to recover deleted files in Linux
  • Linux command to clear the contents of a file without deleting it
  • 4 Ways to Delete Blank Lines in Files in Linux

<<:  Vue custom v-has instruction to implement button permission judgment

>>:  Detailed installation tutorial of Mysql5.7.19 under Centos7

Recommend

Implementation code for using CSS text-emphasis to emphasize text

1. Introduction In the past, if you wanted to emp...

How to use union all in MySQL to get the union sort

Sometimes in a project, due to some irreversible ...

Basic reference types of JavaScript advanced programming

Table of contents 1. Date 2. RegExp 3. Original p...

Detailed explanation of Docker basic network configuration

External Access Randomly map ports Using the -P f...

How to set up Spring Boot using Docker layered packaging

The Spring Boot project uses docker containers, j...

How to support Webdings fonts in Firefox

Firefox, Opera and other browsers do not support W...

How to click on the a tag to pop up the input file upload dialog box

html Copy code The code is as follows: <SPAN cl...

XHTML Tutorial: The Difference Between Transitional and Strict

In fact, XHTML 1.0 is divided into two types (thr...

js dynamically implements table addition and deletion operations

This article example shares the specific code for...

Implementation of Nginx Intranet Standalone Reverse Proxy

Table of contents 1 Nginx Installation 2 Configur...

MySQL 8.0.23 installation super detailed tutorial

Table of contents Preface 1. Download MySQL from ...

HTML implements read-only text box and cannot modify the content

Without further ado, I will post the code for you...

MySQL NULL data conversion method (must read)

When using MySQL to query the database and execut...