I learned the command to create files and directories mkdir, today I'm going to learn the commands for deleting files and directories in Linux: 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:
Copy Code code as follows:
[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:
Copy Code code as follows:
[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:
Copy Code code as follows:
[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:
Copy Code code 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: 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]#
Instance five: The RM-RF test2 command deletes all files in the Test2 subdirectory and subdirectories without one by one confirmation
Command:
RM-RF test2
Output:
Copy Code code 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 six: Delete a file that starts with-f
Command:
RM---F
Output:
Copy Code code as follows:
[Root@localhost test]# Touch---F
[Root@localhost test]# ls---f
-f[root@localhost test]# RM---F
RM: Do you want to delete the general empty file "-F"? Y
[Root@localhost test]# ls---f
LS: F: no file or directory
[Root@localhost test]#
You can also use the following procedure:
[Root@localhost test]# touch./-f
[root@localhost test]# ls./-f
./-f[root@localhost test]# RM./-f
RM: Do you want to delete the generic empty file "./-f"? Y
[Root@localhost test]#
Example seven: Customizing the Recycle Bin Feature
command:
Myrm () {d=/tmp/$ (date +%y%m%d%h%m%s); Mkdir-p $D mv "$@" $D && echo "moved to $D OK";
Output:
Copy Code code 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 16
-rw-r--r--1 root 0 10-26 15:08 1.log
-rw-r--r--1 root 0 10-26 15:08 2.log
-rw-r--r--1 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 simulates the Recycle Bin's effect, that is, when you delete a file, you just place the file in a temporary directory so that you can recover it when you need it.