How to use the rm command in Linux

02-18-2023

This article mainly introduces how to use the rm command in Linux. In daily operations, I believe that many people have doubts about the usage of the rm command in Linux. The easy-to-use method of operation, I hope it will help you to answer your doubts about how to use the rm command in Linux! Next, please follow the editor to learn together!

rm is a commonly used command. The function of this command is to delete one or more files or directories in a directory. It can also delete a directory and all files and subdirectories under it. For linked files, only the links are deleted, and the original files remain unchanged.

rm is a dangerous command. Be careful when using it, especially for novices, otherwise the entire system will be destroyed by this command (such as executing rm * -rf in / (root directory)). Therefore, before executing rm, we'd better confirm which directory we are in, what exactly we want to delete, and keep a clear mind when operating.

1. Command format:

rm [option] file...

2. Command function:

Delete one or more files or directories in a directory. If the -r option is not used, rm will not delete the directory. If you use rm to delete a file, you can usually still get the file back.

3. Command parameters:

-f, --force Ignore files that do not exist, never give a prompt.
-i, --interactive delete interactively
-r, -r, --recursive instructs rm to delete all directories and subdirectories listed in the parameters recursively.
-v, --verbose Display the steps in detail
--help Display this help information and exit
--version Output version information and exit

4. Example of the command:

Example 1: delete the file file, the system will first ask whether to delete it.

Command:

rm filename

Output:

Copy code The code is as follows:

[[email protected] test1]# ll

Total 4

Copy code The code is as follows:

-rw-r--r-- 1 root root 56 10-26 14:31 log.log
[email protected] test1]# rm log.log

rm: Do you want to delete the general file log.log? y

Copy code The code is as follows:

[email protected] test1]# ll

Total 0[[email protected] test1]#

< p >Explanation:

After entering the rm log.log command, the system will ask whether to delete the file. After entering y, the file will be deleted.

Example 2: Forcibly delete the file, the system will no longer prompt.

Command:

Copy the code The code is as follows:

rm -f log1.log

Output :

Copy code The code is as follows:

[[email protected] test1]# ll

Total 4

Copy code The code is as follows:

-rw-r--r-- 1 root root 23 10-26 14:40 log1.log
[[email protected] test1]# rm -f log1.log< br/>[[email protected] test1]# ll

Total 0[[email protected] test1]#

Example 3: delete any .log file; Ask for confirmation one by one before deleting

Command:

rm -i *.log

Output:

Copy Code Code As follows:

[[email protected] test1]# ll

Total 8

Copy code The code is as follows:

- rw-r--r-- 1 root root 11 10-26 14:45 log1.log
-rw-r--r-- 1 root root 24 10-26 14:45 log2.log
[[email protected] test1]# rm -i *.log

rm: whether to delete the general file log1.log? y

rm: whether to delete the general file log2 .log? y

[[email protected] test1]# ll

Total 0[[email protected] test1]#

ExampleFour: Delete the test1 subdirectory and all files in the subdirectory

Command:

Copy code The code is as follows:

rm -r test1< br/>

Output:

Copy the code The code is as follows:

[[email protected] test]# ll

Total 24drwxr-xr-x 7 root root 4096 10-25 18:07 scf

Copy code The code is as follows:

drwxr-xr-x 2 root root 4096 10-26 14: 51 test1
drwxr-xr-x 3 root root 4096 10-25 17:44 test2
drwxrwxrwx 2 root root 4096 10-25 17:46 test3
drwxr-xr-x 2 root root 4096 10-25 17:56 test4
drwxr-xr-x 3 root root 4096 10-25 17:56 test5
[[email protected] test]# rm -r test1

rm: Do you want to enter the directory test1? y

rm: Do you delete the general file test1/log3.log? y

rm: Do you want to delete the directory test1? y

Copy code The code is as follows:

[[email protected] test]# ll

Total 20drwxr-xr-x 7 root root 4096 10-25 18: 07 scf

Copy code The code is as follows:

drwxr-xr-x 3 root root 4096 10-25 17:44 test2
drwxrwxrwx 2 root root 4096 10-25 17 :46 test3
drwxr-xr-x 2 root root 4096 10-25 17:56 test4
drwxr-xr-x 3 root root 4096 10-25 17:56 test5
[[email protected] localhost test]#

Example 5: rThe m -rf test2 command will delete the test2 subdirectory and all files in the subdirectory, and there is no need to confirm one by one

Command:

Copy code The code is as follows:

p>

rm -rf test2

Output:

Copy the code The code is as follows:

[[email protected] test]# rm - rf test2
[[email protected] test]# ll

total 16drwxr-xr-x 7 root root 4096 10-25 18:07 scf

Copy code The code is as follows:

drwxrwxrwx 2 root root 4096 10-25 17:46 test3
drwxr-xr-x 2 root root 4096 10-25 17:56 test4
drwxr-xr-x 3 root root 4096 10-25 17:56 test5
[[email protected] test]#

Example 6: Delete starting with -f

Command:

rm -- -f

Output:

Copy code The code is as follows:

p>

[[email protected] test]# touch -- -f
[[email protected] test]# ls -- -f
-f[[email protected] test]# rm -- - f

rm: Do you want to delete the general empty file -f? y

Copy the code The code is as follows:

[[email protected] test]# ls -- -f

ls: -f: No such file or directory

Copy the code The code is as follows:

[[email protected] test] #

You can also use the following steps:

Copy the code The code is as follows:

[[email protected] test]# touch ./- f
[[email protected] test]# ls ./-f
./-f[[email protected] test]# rm ./-f

rm: whether to delete general empty files./-f? y

Copy code The code is as follows:

< p >[[email protected] test]#

Example 7: Custom recycle bin function

Command:

Copy code The code is as follows:

myrm(){ d=/tmp/$(date +%y%m%d%h%m%s); mkdir -p $d; mv "[email protected] " $d && echo "moved to $d ok"; }

Output:

Copy code The code is as follows:

[[email protected] test]# myrm(){ d=/tmp/$(date +%y%m%d%h%m%s); mkdir -p $d; mv "[email protected]" $d && echo "moved to $d ok"; }
[[email protected] test]# alias rm='myrm'
[[email protected] test]# touch .log .log .log
[[email protected] test]# ll
Total
-rw-r--r-- root root - : .log
-rw-r--r-- root root - : .log
-rw -r--r-- root root - : .log
drwxr-xr-x root root - : scf
drwxrwxrwx root root - : test
drwxr-xr-x root root - : test
drwxr-xr-x root root - : test
[[email protected] test]# rm [].log
moved to /tmp/ ok
[[email protected] test ]# ll
Total drwxr-xr-x root root - : scf
drwxrwxrwx root root - : test
drwxr-xr-x root root - : test
drwxr-xr- x rootroot - : test
[[email protected] test]# ls /tmp//
.log .log .log
[[email protected] test]#

< p >Explanation:

The above operation process simulates the effect of the recycle bin, that is, when deleting a file, it just puts the file in a temporary directory, so that it can be restored when needed.

Let’s give you a detailed introduction to the name: rm command

Usage authority: any user

Usage method: rm [options] name ...

Description: Delete files and directories.

Parameters:

-i Ask for confirmation one by one before deleting.
-f Even if the original file attribute is set to read-only, it will be deleted directly without confirmation one by one.
-r deletes the directory and the following files one by one.

Example:

Delete any c language program files; ask for confirmation one by one before deleting:

rm -i *.c

Delete the finished subdirectory and any files in the subdirectory:

rm -r finished< br/>

Function description: delete files or directories.

Syntax: rm [-dfirv][--help][--version][document or directory...]

Additional instructions: Execute the rm command to delete files or directories. If you want to delete a directory, you must add the parameter "-r", otherwise only files will be deleted by default.

Parameters:

-d or --directory directly delete the hard link data of the directory to be deleted to 0, delete the directory .

-f or --force Forcefully delete files or directories.

-i or --interactive Ask the user before deleting an existing file or directory.

-r or -r or --recursive Recursive processing, processing any documents and subdirectories under the specified directory together.

-v or --verbose Display the execution process of the instruction.

--help Online help.

--version Display version information


Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us