RM is a dangerous command, especially when used, especially for novices, or the entire system will be destroyed in this command (e.g., RM *-rf under/(root directory)). So, before we execute RM, it's a good idea to check in which directory, exactly what to delete, and keep a high level of sanity in 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 the-r option is not used. If you use RM to delete a file, you can usually still restore the file to its original state.
-F,--force ignores nonexistent files and never gives hints
-I,--interactive for interactive deletion
-R,-R,--recursive instructs RM to recursively delete all directories and subdirectories listed in the parameters
-V,--verbose detailed display of the steps performed
--HELP Display this help message and exit
--version output version information and exit
Instance one: Delete file, the system will first ask whether to delete.
Command:
RM file Name
Output:
[email protected] test1]# LL
Total 4
-rw-r--r--1 root root 10-26 14:31 log.log
[Email protected] test1]# RM log.log
RM: Do you want to delete the generic file "Log.log"? Y
[email protected] test1]# LL
Total 0[[email protected] test1]#
Description
After entering the RM log.log command, the system asks whether to delete the file after entering Y, and does not want to delete the data n.
Example two: Forcibly delete file, the system no longer prompt.
Command:
Rm-f Log1.log
Output:
[email protected] test1]# LL
Total 4
-rw-r--r--1 root root 10-26 14:40 Log1.log
[Email protected] test1]# rm-f Log1.log
[email protected] test1]# LL
Total 0[[email protected] test1]#
Example three: Delete any. log file, ask for confirmation before deleting
Command:
Rm-i *.log
Output:
[email protected] test1]# LL
Total 8
-rw-r--r--1 root root one 10-26 14:45 log1.log
-rw-r--r--1 root root 10-26 14:45 Log2.log
[Email protected] 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
[email protected] test1]# LL
Total 0[[email protected] test1]#
Example four: Delete all the files in the Test1 subdirectory and subdirectories
Command:
Rm-r test1
Output:
[email protected] 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
[Email protected] 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 the directory "Test1"? Y
[email protected] 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
[Email protected] test]#
Example five: The RM-RF test2 command will delete all files in the Test2 subdirectory and subdirectories without one by one confirmation
Command:
RM-RF test2
Output:
[Email protected] test]# RM-RF test2
[email protected] 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
[Email protected] test]#
Example six: Delete a file that begins with-F
Command:
RM---F
Output:
[[email protected] test]# touch---F
[[email protected] test]# ls---f
-f[[email protected] test]# RM---F
RM: Do you want to delete the generic empty file "-F"? Y
[[email protected] test]# ls---f
LS:-F: No file or directory
[Email protected] test]#
You can also use the following procedure:
[[email protected] test]# touch./-f
[[email protected] test]# ls./-f
./-f[[email protected] test]# rm./-f
RM: Delete the generic empty file "./-f"? Y
[Email protected] test]#
Linux common commands (4) RM command