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 of Linux correctly

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

Share

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

This article mainly explains "how to use the rm command of Linux correctly", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let Xiaobian take you to learn "how to use Linux rm command correctly"!

rm is a dangerous command and should be used with great care, especially for beginners, or the entire system will be destroyed by this command (e.g. rm * -rf in/(root directory)). So, before we execute rm, it's best to confirm which directory and what to delete, and keep a high level of clarity when doing so.

1. Command Format:

rm [options] file…

2. Command function:

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

3. Command Parameters:

-f, --force Ignore files that don't exist and never give a hint.

-i, --interactive for interactive deletion

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

-v, --verbose Show steps in detail

--help Display this help message and exit

--version Output version information and exit

4. Command example:

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

Command:

rm file name

Output:

The copy code is as follows:

[root@localhost test1]# ll

total of 4

The copy code is as follows:

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

root@localhost test1]# rm log.log

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

The copy code is as follows:

root@localhost test1]# ll

Total 0[root@localhost test1]#

Description:

After entering the rmlog.log command, the system will ask if you want to delete it. After entering y, the file will be deleted. If you don't want to delete it, the data n.

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

Command:

The copy code is as follows:

rm -f log1.log

Output:

The copy code is as follows:

[root@localhost test1]# ll

total of 4

The copy code is as follows:

-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 file; ask for confirmation one by one before deleting

Command:

rm -i *.log

Output:

The copy code is as follows:

[root@localhost test1]# ll

total of 8

The copy 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

[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 the test1 subdirectory and all files in the subdirectory

Command:

The copy code is as follows:

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

The copy 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

[root@localhost test]# rm -r test1

rm: Enter directory "test1"? y

rm: Delete the generic file "test1/log3.log"? y

rm: Delete directory "test1"? y

The copy code is as follows:

[root@localhost test]# ll

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

The copy 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

[root@localhost test]#

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

Command:

The copy code is as follows:

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

The copy 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

[root@localhost test]#

Example 6: Delete files starting 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: Delete the general empty file "-f"? y

The copy code is as follows:

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

ls: -f: No such file or directory

The copy code is as follows:

[root@localhost test]#

You can also use the following steps:

The copy code is as follows:

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

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

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

rm: Delete general empty file "./- f"? y

The copy code is as follows:

[root@localhost test]#

Example 7: Custom Recycle Bin Function

Command:

The copy code is as follows:

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

Output:

The copy code is as follows:

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

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

[root@localhost test]# touch .log .log .log

[root@localhost 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

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

moved to /tmp/ ok

[root@localhost test]# ll

Total drwxr-xr-x root- : scf

drwxrwxrwx root root - : test

drwxr-xr-x root root - : test

drwxr-xr-x root root - : test

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

.log .log .log

[root@localhost test]#

Description:

The above procedure mimics the effect of the Recycle Bin, i.e. deleting files only by placing them in a temporary directory so that they can be restored when needed.

The following is a detailed introduction to the name: rm command

Access: Any user

Use: rm [options] name...

Description: Delete files and directories.

Parameter:

-i Ask for confirmation one by one before deleting.

-f Even if the original file attribute is set to read-only, it is deleted directly without confirmation.

-r Delete the directory and the following files one by one.

Example:

delete any c files; ask for confirmation before deleting them:

rm -i *.c

Delete the finished subdirectory and any files in it:

rm -r finished

Function Description: Delete documents or directories.

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

Supplementary note: execute rm command to delete documents or directories. If you want to delete directories, you must add parameter "-r", otherwise only documents will be deleted by default.

Parameters:

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

-f or--force Forces deletion of documents or directories.

-i or--interactive Ask users before deleting existing documents or directories.

-r or-r or--recursive Recursive, processes any documents and subdirectories in the specified directory together.

-v or--verbose shows instruction execution.

--help Online help.

--version Display version information

At this point, I believe that everyone has a deeper understanding of "how to use the rm command of Linux correctly", so you may wish to actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

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