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

Detailed explanation of the use of linux rm command

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

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

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:

[root@localhost test1]# ll

total of 4

-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

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:

rm -f log1.log

Output:

[root@localhost test1]# ll

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

Command:

rm -i *.log

Output:

[root@localhost test1]# ll

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

Command:

rm -r test1

Output:

The 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: Enter directory "test1"? y

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

rm: 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 the test2 subdirectory and all files in the subdirectory without confirming them one by one.

Command:

rm -rf test2

Output:

The 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 starting with-f

Command:

rm -- -f

Output:

The 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

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

ls: -f: 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 general empty file "./- f"? y

[root@localhost test]#

Example 7: Custom Recycle Bin Function

Command:

myrm(){ D=/tmp/$(date +%Y%m%d%H%M%S); mkdir -p $D; mv "$@" $D && echo "moved to $D ok"; }

Output:

The 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 1.log 2.log 3.log

[root@localhost test]# ll

total of 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 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.

At this point, I believe that we have a deeper understanding of the "linux rm command use detailed explanation," 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

Servers

Wechat

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

12
Report