Yesterday I learned the command to create files and directories mkdir, today to learn Linux delete files and directories command: RM command. RM is a commonly used command that deletes one or more files or directories in a directory, and it can delete a directory and all of its files and subdirectories. For linked files, only the link is deleted and the original file remains unchanged.
RM is a dangerous command to be particularly careful when used, especially for beginners, otherwise the entire system will be destroyed in this command (for example, under/(root directory) to perform RM *-RF). So, before we do RM, we'd better make sure which directory you want to delete, and maintain a high level of sanity in the operation.
1. Command format:
RM [Options] File ...
2. Command function:
Deletes one or more files or directories in one 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.
3. Command parameters:
-F,--force ignores files that do not exist and never gives a hint.
-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
--HELP displays this help information and exits
--version output version information and exit
4. Command instance:
Instance one: Delete file files, and the system will ask if you want to delete them first.
Command
RM file Name
Output:
[Root@localhost test1]# LL
Total 4
-rw-r--r--1 root 10-26 14:31 log.log
Root@localhost test1]# RM log.log
RM: Do you want to delete the generic file "Log.log"? Y
Root@localhost test1]# LL
Total 0 [root@localhost test1]#
Description
After you enter the RM log.log command, the system asks if you want to delete it, and then you delete the file after you enter Y, and you do not delete the data n.
Example two: Forcibly delete file, the system no longer prompts.
Command:
Rm-f Log1.log
Output:
[Root@localhost test1]# LL
Total 4
-rw-r--r--1 root 10-26 14:40 log1.log
[Root@localhost test1]# rm-f Log1.log
[Root@localhost test1]# LL
Total 0 [root@localhost test1]#
Example three: Delete any. log file, ask for confirmation before deleting
Command:
Rm-i *.log
Output:
[Root@localhost test1]# LL
Total 8
-rw-r--r--1 root 10-26 14:45 log1.log
-rw-r--r--1 root 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 four: Delete all files in the Test1 subdirectory and subdirectories
Command:
Rm-r test1
Output:
[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 the 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]#