Home > Hosting > Server

How to use the rm command in Linux

2023-02-18 21:53:12

<p style="text-align: left;">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!</p><p style="text-align: left;">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.</p><p style="text-align: left;">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&#39;d better confirm which directory we are in, what exactly we want to delete, and keep a clear mind when operating.</p><p style="text-align: left;">1. Command format:</p><p style="text-align: left;">rm [option] file...</p><p style="text-align: left;">2. Command function:</p><p style="text-align: left;">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.</p><p style="text-align: left;">3. Command parameters:</p><p style="text-align: left;">-f, --force Ignore files that do not exist, never give a prompt. <br/>-i, --interactive delete interactively<br/>-r, -r, --recursive instructs rm to delete all directories and subdirectories listed in the parameters recursively. <br/>-v, --verbose Display the steps in detail<br/>--help Display this help information and exit<br/>--version Output version information and exit</p><p style="text-align: left;">4. Example of the command:</p><p style="text-align: left;"><strong>Example 1: delete the file file, the system will first ask whether to delete it. </strong></p><p style="text-align: left;">Command:</p><p style="text-align: left;">rm filename</p><p style="text-align: left;">Output:</p><p style="text-align: left;">Copy code The code is as follows:</p><p style="text-align: left;">[root@localhost test1]# ll<br/></p><p style="text-align: left;">Total 4</p><p style="text-align: left;">Copy code The code is as follows:</p><p style="text-align: left;">-rw-r--r-- 1 root root 56 10-26 14:31 log.log<br/>root@localhost test1]# rm log.log<br/></p><p style="text-align: left;">rm: Do you want to delete the general file log.log? y</p><p style="text-align: left;">Copy code The code is as follows:</p><p style="text-align: left;">root@localhost test1]# ll<br/></p><p style="text-align: left;">Total 0[root@localhost test1]#</p><p style="text-align: left;">&lt; p &gt;Explanation:</p><p style="text-align: left;">After entering the rm log.log command, the system will ask whether to delete the file. After entering y, the file will be deleted.</p><p style="text-align: left;"><strong>Example 2: Forcibly delete the file, the system will no longer prompt. </strong></p><p style="text-align: left;">Command:</p><p style="text-align: left;">Copy the code The code is as follows:</p><p style="text-align: left;">rm -f log1.log<br/></p><p style="text-align: left;">Output :</p><p style="text-align: left;">Copy code The code is as follows:</p><p style="text-align: left;">[root@localhost test1]# ll<br/></p><p style="text-align: left;">Total 4</p><p style="text-align: left;">Copy code The code is as follows:</p><p style="text-align: left;">-rw-r--r-- 1 root root 23 10-26 14:40 log1.log<br/>[root@localhost test1]# rm -f log1.log&lt; br/&gt;[root@localhost test1]# ll<br/></p><p style="text-align: left;">Total 0[root@localhost test1]#</p><p style="text-align: left;"><strong>Example 3: delete any .log file; Ask for confirmation one by one before deleting</strong></p><p style="text-align: left;">Command:</p><p style="text-align: left;">rm -i *.log</p><p style="text-align: left;">Output:</p><p style="text-align: left;">Copy Code Code As follows:</p><p style="text-align: left;">[root@localhost test1]# ll<br/></p><p style="text-align: left;">Total 8</p><p style="text-align: left;">Copy code The code is as follows:</p><p style="text-align: left;">- rw-r--r-- 1 root root 11 10-26 14:45 log1.log<br/>-rw-r--r-- 1 root root 24 10-26 14:45 log2.log<br/>[root@localhost test1]# rm -i *.log<br/></p><p style="text-align: left;">rm: whether to delete the general file log1.log? y</p><p style="text-align: left;">rm: whether to delete the general file log2 .log? y</p><p style="text-align: left;">[root@localhost test1]# ll</p><p style="text-align: left;">Total 0[root@localhost test1]#</p><p style="text-align: left;"><strong>ExampleFour: Delete the test1 subdirectory and all files in the subdirectory</strong></p><p style="text-align: left;">Command:</p><p style="text-align: left;">Copy code The code is as follows:</p><p style="text-align: left;">rm -r test1&lt; br/&gt;</p><p style="text-align: left;">Output:</p><p style="text-align: left;">Copy the code The code is as follows:</p><p style="text-align: left;">[root@localhost test]# ll<br/></p><p style="text-align: left;">Total 24drwxr-xr-x 7 root root 4096 10-25 18:07 scf</p><p style="text-align: left;">Copy code The code is as follows:</p><p style="text-align: left;">drwxr-xr-x 2 root root 4096 10-26 14: 51 test1<br/>drwxr-xr-x 3 root root 4096 10-25 17:44 test2<br/>drwxrwxrwx 2 root root 4096 10-25 17:46 test3<br/>drwxr-xr-x 2 root root 4096 10-25 17:56 test4<br/>drwxr-xr-x 3 root root 4096 10-25 17:56 test5<br/>[root@localhost test]# rm -r test1<br/></p><p style="text-align: left;">rm: Do you want to enter the directory test1? y</p><p style="text-align: left;">rm: Do you delete the general file test1/log3.log? y</p><p style="text-align: left;">rm: Do you want to delete the directory test1? y</p><p style="text-align: left;">Copy code The code is as follows:</p><p style="text-align: left;">[root@localhost test]# ll<br/></p><p style="text-align: left;">Total 20drwxr-xr-x 7 root root 4096 10-25 18: 07 scf</p><p style="text-align: left;">Copy code The code is as follows:</p><p style="text-align: left;">drwxr-xr-x 3 root root 4096 10-25 17:44 test2<br/>drwxrwxrwx 2 root root 4096 10-25 17 :46 test3<br/>drwxr-xr-x 2 root root 4096 10-25 17:56 test4<br/>drwxr-xr-x 3 root root 4096 10-25 17:56 test5<br/>[root@ localhost test]#<br/></p><p style="text-align: left;"><strong>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</strong></p><p style="text-align: left;">Command:</p><p style="text-align: left;">Copy code The code is as follows:</p><p style="text-align: left;">p&gt;</p><p style="text-align: left;">rm -rf test2<br/></p><p style="text-align: left;">Output:</p><p style="text-align: left;">Copy the code The code is as follows:</p><p style="text-align: left;">[root@localhost test]# rm - rf test2<br/>[root@localhost test]# ll<br/></p><p style="text-align: left;">total 16drwxr-xr-x 7 root root 4096 10-25 18:07 scf<br/></p><p style="text-align: left;">Copy code The code is as follows:</p><p style="text-align: left;">drwxrwxrwx 2 root root 4096 10-25 17:46 test3<br/>drwxr-xr-x 2 root root 4096 10-25 17:56 test4<br/>drwxr-xr-x 3 root root 4096 10-25 17:56 test5<br/>[root@localhost test]#<br/></p><p style="text-align: left;"><strong>Example 6: Delete starting with -f </strong></p><p style="text-align: left;">Command:</p><p style="text-align: left;">rm -- -f</p><p style="text-align: left;">Output:</p><p style="text-align: left;">Copy code The code is as follows:</p><p style="text-align: left;">p&gt;</p><p style="text-align: left;">[root@localhost test]# touch -- -f<br/>[root@localhost test]# ls -- -f<br/>-f[root@localhost test]# rm -- - f<br/></p><p style="text-align: left;">rm: Do you want to delete the general empty file -f? y</p><p style="text-align: left;">Copy the code The code is as follows:</p><p style="text-align: left;">[root@localhost test]# ls -- -f<br/></p><p style="text-align: left;">ls: -f: No such file or directory</p><p style="text-align: left;">Copy the code The code is as follows:</p><p style="text-align: left;">[root@localhost test] #<br/></p><p style="text-align: left;">You can also use the following steps:</p><p style="text-align: left;">Copy the code The code is as follows:</p><p style="text-align: left;">[root@localhost test]# touch ./- f<br/>[root@localhost test]# ls ./-f<br/>./-f[root@localhost test]# rm ./-f<br/></p><p style="text-align: left;">rm: whether to delete general empty files./-f? y</p><p style="text-align: left;">Copy code The code is as follows:</p><p style="text-align: left;">&lt; p &gt;[root@localhost test]#<br/></p><p style="text-align: left;"><strong>Example 7: Custom recycle bin function</strong></p><p style="text-align: left;">Command:</p><p style="text-align: left;">Copy code The code is as follows:</p><p style="text-align: left;">myrm(){ d=/tmp/$(date +%y%m%d%h%m%s); mkdir -p $d; mv &quot;$@ &quot; $d &amp;&amp; echo &quot;moved to $d ok&quot;; }<br/></p><p style="text-align: left;">Output:</p><p style="text-align: left;">Copy code The code is as follows:</p><p style="text-align: left;">[root@localhost test]# myrm(){ d=/tmp/$(date +%y%m%d%h%m%s); mkdir -p $d; mv &quot;$@&quot; $d &amp;&amp; echo &quot;moved to $d ok&quot;; }<br/>[root@localhost test]# alias rm=&#39;myrm&#39;<br/>[root@localhost test]# touch .log .log .log<br/>[root@localhost test]# ll<br/>Total<br/>-rw-r--r-- root root - : .log<br/>-rw-r--r-- root root - : .log<br/>-rw -r--r-- root root - : .log<br/>drwxr-xr-x root root - : scf<br/>drwxrwxrwx root root - : test<br/>drwxr-xr-x root root - : test<br/>drwxr-xr-x root root - : test<br/>[root@localhost test]# rm [].log<br/>moved to /tmp/ ok<br/>[root@localhost test ]# ll<br/>Total drwxr-xr-x root root - : scf<br/>drwxrwxrwx root root - : test<br/>drwxr-xr-x root root - : test<br/>drwxr-xr- x rootroot - : test<br/>[root@localhost test]# ls /tmp//<br/>.log .log .log<br/>[root@localhost test]#<br/></p><p style="text-align: left;">&lt; p &gt;Explanation:</p><p style="text-align: left;">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.</p><p style="text-align: left;">Let’s give you a detailed introduction to the name: rm command</p><p style="text-align: left;">Usage authority: any user<br/></p><p style="text-align: left;">Usage method: rm [options] name ...<br/></p><p style="text-align: left;">Description: Delete files and directories. <br/></p><p style="text-align: left;"><strong>Parameters: <br/></strong></p><p style="text-align: left;">-i Ask for confirmation one by one before deleting. <br/>-f Even if the original file attribute is set to read-only, it will be deleted directly without confirmation one by one. <br/>-r deletes the directory and the following files one by one. <br/></p><p style="text-align: left;"><strong>Example:</strong><br/></p><p style="text-align: left;">Delete any c language program files; ask for confirmation one by one before deleting:<br/></p><p style="text-align: left;">rm -i *.c<br/></p><p style="text-align: left;">Delete the finished subdirectory and any files in the subdirectory:<br/></p><p style="text-align: left;">rm -r finished&lt; br/&gt;</p><p style="text-align: left;">Function description: delete files or directories. <br/></p><p style="text-align: left;">Syntax: rm [-dfirv][--help][--version][document or directory...]<br/></p><p style="text-align: left;">Additional instructions: Execute the rm command to delete files or directories. If you want to delete a directory, you must add the parameter &quot;-r&quot;, otherwise only files will be deleted by default.</p><p style="text-align: left;"><strong>Parameters:<br/></strong></p><p style="text-align: left;">-d or --directory directly delete the hard link data of the directory to be deleted to 0, delete the directory . <br/></p><p style="text-align: left;">-f or --force Forcefully delete files or directories. <br/></p><p style="text-align: left;">-i or --interactive Ask the user before deleting an existing file or directory. <br/></p><p style="text-align: left;">-r or -r or --recursive Recursive processing, processing any documents and subdirectories under the specified directory together. <br/></p><p style="text-align: left;">-v or --verbose Display the execution process of the instruction. <br/></p><p style="text-align: left;">--help Online help. <br/></p><p style="text-align: left;">--version Display version information</p><p><br/></p>


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