Linux Command (5): rm command, linux Command rm
Command for deleting files and directories in linux: rm command. Rm is a common command used to delete one or more files or directories in a directory. It can also delete all files and subdirectories in a directory. For linked files, only the links are deleted, and the original files remain unchanged.
Rm is a dangerous command. Be careful when using it, especially for beginners. Otherwise, the entire system will be destroyed in this command (for example, in the/(root directory) run rm *-rf ). Therefore, before executing rm, we 'd better first check which directory we are in and what we want to delete and keep a high degree of clarity during operations.
1. Command Format:
Rm [Option] file...
2. command functions:
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 restore it to its original state.
3. command parameters:
-F, -- force ignores non-existing files and never gives a prompt.
-I, -- interactive for interactive Deletion
-R,-R, -- recursive indicates that rm recursively deletes all directories and subdirectories listed in the parameter.
-V, -- verbose detailed display steps
-- Help: displays the help information and exits.
-- Version: Output version information and exit
4. Command instance:
Instance 1: to delete a file, the system first asks whether to delete the file.
Command:
Rm file name
Output:
[Root @ localhost test1] # ll
Total 4
-Rw-r -- 1 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] #
Note:
After entering the rm log. log command, the system will ask whether to delete the file. After Entering y, the file will be deleted. If you do not want to delete the file, data n will be returned.
Example 2: Force delete the file and the system will not prompt again.
Command:
Rm-f log1.log
Output:
[Root @ localhost test1] # ll
Total 4
-Rw-r -- 1 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 Files. Before deletion, ask for confirmation one by one.
Command:
Rm-I *. log
Output:
[Root @ localhost test1] # ll
Total 8
-Rw-r -- 1 root 11 10-26 14:45 log1.log
-Rw-r -- 1 root 24 10-26 14:45 log2.log
[Root @ localhost test1] # rm-I *. log
Rm: Do you want to delete the general file "log1.log "? Y
Rm: Do you want to delete the common file "log2.log "? Y
[Root @ localhost test1] # ll
Total 0 [root @ localhost test1] #
Example 4: delete all files in the test1 sub-directory and sub-directory
Command:
Rm-r test1
Output:
[Root @ localhost test] # ll
Total 24drwxr-xr-x 7 root 4096 10-25 scf
Drwxr-xr-x 2 root 4096 10-26 test1
Drwxr-xr-x 3 root 4096 10-25 test2
Drwxrwxrwx 2 root 4096 10-25 test3
Drwxr-xr-x 2 root 4096 10-25 test4
Drwxr-xr-x 3 root 4096 10-25 test5
[Root @ localhost test] # rm-r test1
Rm: Do you want to enter the "test1" directory "? Y
Rm: Do you want to delete the general file "test1/log3.log "? Y
Rm: Do you want to delete the "test1" directory "? Y
[Root @ localhost test] # ll
Total 20drwxr-xr-x 7 root 4096 10-25 scf
Drwxr-xr-x 3 root 4096 10-25 test2
Drwxrwxrwx 2 root 4096 10-25 test3
Drwxr-xr-x 2 root 4096 10-25 test4
Drwxr-xr-x 3 root 4096 10-25 test5
[Root @ localhost test] #
Instance 5: the rm-rf test2 command will delete all files in the test2 sub-directories and sub-Directories Without one-by-one confirmation.
Command:
Rm-rf test2
Output:
[Root @ localhost test] # rm-rf test2
[Root @ localhost test] # ll
Total 16drwxr-xr-x 7 root 4096 10-25 scf
Drwxrwxrwx 2 root 4096 10-25 test3
Drwxr-xr-x 2 root 4096 10-25 test4
Drwxr-xr-x 3 root 4096 10-25 test5
[Root @ localhost test] #
Example 6: delete an object starting with-f
Command:
Rm ---f
Output:
[Root @ localhost test] # touch ---f
[Root @ localhost test] # ls ---f
-F [root @ localhost test] # rm ---f
Rm: Do you want to delete the generally empty file "-f "? Y
[Root @ localhost test] # ls ---f
Ls:-f: the file or directory does not exist.
[Root @ localhost test] #
You can also follow these steps:
[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] #
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:
[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 -- 1 root 0 10-26 15:08 1.log
-Rw-r -- 1 root 0 10-26 15:08 2.log
-Rw-r -- 1 root 0 10-26 3.log
Drwxr-xr-x 7 root 4096 10-25 scf
Drwxrwxrwx 2 root 4096 10-25 test3
Drwxr-xr-x 2 root 4096 10-25 test4
Drwxr-xr-x 3 root 4096 10-25 test5
[Root @ localhost test] # rm [123]. log
Moved to/tmp/20121026150901 OK
[Root @ localhost test] # ll
Total 16drwxr-xr-x 7 root 4096 10-25 scf
Drwxrwxrwx 2 root 4096 10-25 test3
Drwxr-xr-x 2 root 4096 10-25 test4
Drwxr-xr-x 3 root 4096 10-25 test5
[Root @ localhost test] # ls/tmp/20121026150901/
1. log 2.log 3.log
[Root @ localhost test] #
Note:
The above operation simulates the effect of the recycle bin, that is, when you delete a file, you just put the file in a temporary directory so that it can be restored as needed.