Home  >  Article  >  Operation and Maintenance  >  How to use the rm command under linux

How to use the rm command under linux

WBOY
WBOYforward
2023-05-13 18:07:152212browse

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 link is 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 (for example, execute rm * -rf in / (root directory) ). Therefore, before we execute rm, it is best to confirm which directory we are in and what we want to delete, and keep a clear mind during the operation.

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 restore the file to its original state.

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

4. Command example:

Example 1: To delete the file file, the system will first ask whether to delete it.
Command:
rm file name

Copy code The code is as follows:

[root@localhost test1]# ll总计 4-rw-r--r-- 1 root root 56 10-26 14:31 log.logtest1]# rm log.logrm:是否删除 一般文件 “log.log”? ytest1]# ll总计 0[root@localhost test1]#
说明:输入rm log.log命令后,系统会询问是否删除,输入y后就会删除文件,不想删除则数据n。
实例二:强行删除file,系统不再提示。命令:rm -f log1.log

Copy code The code is as follows:

[root@localhost test1]# ll
总计 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
总计 0[root@localhost test1]#

Example 3: Delete any. log files; ask for confirmation one by one before deleting
Command:

rm -i *.log

Copy code The code is as follows:

[root@localhost test1]# ll
总计 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:是否删除 一般文件 “log1.log”? y
rm:是否删除 一般文件 “log2.log”? y
[root@localhost test1]# ll
总计 0[root@localhost test1]#

Example 4: Delete the test1 subdirectory and all files in the subdirectory
Command:

rm -r test1

Copy code The code is as follows:

[root@localhost test]# ll
总计 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:是否进入目录 “test1”? y
rm:是否删除 一般文件 “test1/log3.log”? y
rm:是否删除 目录 “test1”? y
[root@localhost test]# ll
总计 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 will delete the test2 subdirectory and all files in the subdirectory without a single Once confirmed
Command:

rm -rf  test2

Copy code The code is as follows:

[root@localhost test]# rm -rf test2
[root@localhost test]# ll
总计 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

Copy code The code is as follows:

[root@localhost test]# touch -- -f
[root@localhost test]# ls -- -f
-f[root@localhost test]# rm -- -f
rm:是否删除 一般空文件 “-f”? y
[root@localhost test]# ls -- -f
ls: -f: 没有那个文件或目录
[root@localhost test]#
也可以使用下面的操作步骤:
[root@localhost test]# touch ./-f
[root@localhost test]# ls ./-f
./-f[root@localhost test]# rm ./-f
rm:是否删除 一般空文件 “./-f”? y
[root@localhost test]#

Example 7: Customize the recycle bin function
Command:

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

Copy code 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
总计 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
总计 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 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.

The above is the detailed content of How to use the rm command under linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete