Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the rm command to delete files in linux

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "how to delete files using the rm command in linux". The editor shows you the operation process through an actual case. The method of operation is simple and fast, and it is practical. I hope that this article "how to use the rm command to delete files in linux" can help you solve the problem.

Rm is a dangerous command, and be especially careful when using it, especially for beginners, otherwise the entire system will be destroyed by this command (such as executing rm *-rf under / (root)). Therefore, before executing rm, we'd better confirm which directory you are in, what you want to delete, and keep a high level of sanity during the operation.

1. Command format:

Rm [options] file...

2. Command function:

Delete one or more files or directories in a directory, and rm does not delete the directory if you do not use the-r option. If you use rm to delete a file, you can usually restore the file to its original state.

3. Command parameters:

-f,-- force ignores files that do not exist and never gives prompts.

-I,-- interactive for interactive deletion

-r,-r,-- recursive instructs rm to recursively delete all directories and subdirectories listed in the parameter.

-v,-- verbose shows the steps in detail

-- help displays this help and exits

-- version outputs version information and exits

4. Command example:

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

Command:

Rm file name

Output:

The copy code is as follows:

[root@localhost test1] # ll

Total 4

-rw-r--r-- 1 root root 56 10-26 14:31 log.log

Test1] # rm log.log

Rm: do you want to delete the generic file "log.log"? Y

Test1] # ll

Total 0 [root@localhost test1] #

Description:

After you enter the rm log.log command, you will be asked if you want to delete it. If you enter y, you will delete the file. If you do not want to delete it, the data n.

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

Command:

Rm-f log1.log

Output:

The copy code is as follows:

[root@localhost test1] # ll

Total 4

-rw-r--r-- 1 root root 23 10-26 14:40 log1.log

[root@localhost test1] # rm-f log1.log

[root@localhost test1] # ll

Total 0 [root@localhost test1] #

Example 3: delete any .log files; ask for confirmation one by one before deletion

Command:

Rm-I * .log

Output:

The copy code is as follows:

[root@localhost test1] # ll

Total 8

-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

[root@localhost test1] # rm-I * .log

Rm: do you want to delete the generic file "log1.log"? Y

Rm: do you want to delete the generic file "log2.log"? Y

[root@localhost test1] # ll

Total 0 [root@localhost test1] #

Example 4: delete all files in the test1 subdirectory and subdirectories

Command:

Rm-r test1

Output:

The copy code is as follows:

[root@localhost test] # ll

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

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

[root@localhost test] # rm-r test1

Rm: do you want to enter the directory "test1"? Y

Rm: do you want to delete the generic file "test1/log3.log"? Y

Rm: do you want to delete directory "test1"? Y

[root@localhost test] # ll

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

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

[root@localhost test] #

Example 5: the rm-rf test2 command deletes all files in the test2 subdirectory and subdirectories without confirming them one by one

Command:

Rm-rf test2

Output:

The copy code is as follows:

[root@localhost test] # rm-rf test2

[root@localhost test] # ll

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

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

[root@localhost test] #

Example 6: delete files that start with-f

Command:

Rm-f

Output:

The copy code is as follows:

[root@localhost test] # touch-f

[root@localhost test] # ls-f

-f [root@localhost test] # rm-f

Rm: do you want to delete the general empty file "- f"? Y

[root@localhost test] # ls-f

Ls:-f: there is no such file or directory

[root@localhost test] #

You can also use the following steps:

[root@localhost test] # touch. /-f

[root@localhost test] # ls. /-f

. /-f [root@localhost test] # rm. /-f

Rm: delete the general empty file ". /-f"? Y

[root@localhost test] #

Example 7: custom Recycle Bin function

Command:

Myrm () {moved to ok $(date +% y%m%d%h%m%s); mkdir-p $d; mv "$@" $d & & echo "TMP $d";}

Output:

The copy code is as follows:

[root@localhost test] # myrm () {dink echo TMP ok $(date +% y%m%d%h%m%s); mkdir-p $d; mv "$@" $d & & echo "TMP $d";}

[root@localhost test] # alias rm='myrm'

[root@localhost test] # touch 1.log 2.log 3.log

[root@localhost test] # ll

Total 16

-rw-r--r-- 1 root root 0 10-26 15:08 1.log

-rw-r--r-- 1 root root 0 10-26 15:08 2.log

-rw-r--r-- 1 root root 0 10-26 15:08 3.log

Drwxr-xr-x 7 root root 4096 10-25 18:07 scf

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

[root@localhost test] # rm [123] .log

Moved to / tmp/20121026150901 ok

[root@localhost test] # ll

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

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

[root@localhost test] # ls / tmp/20121026150901/

1.log 2.log 3.log

[root@localhost test] #

Description:

The above procedure simulates the effect of the Recycle Bin, that is, when you delete a file, you just put the file in a temporary directory so that it can be recovered when needed.

This is the end of the introduction on "how to use the rm command to delete files in linux". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report