Linux Command: rm command learning and use, linux Command rm Learning
Yesterday I learned the command mkdir for creating files and directories. Today I want to learn the command "rm" for deleting files and directories in linux. 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 and-force ignore non-existing files and never give a prompt.
-I,-interactive Deletion
-R,-R, and-recursive indicate 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 @ localhosttest1] # ll
Total 4
-Rw-r-r-1rootroot5610-2614: 31log. log
Root @ localhosttest1] # rmlog. log
Rm: Do you want to delete the general file "log. log "? Y
Root @ localhosttest1] # ll
Total 0 [root @ localhosttest1] #
Note:
After you enter the rmlog. log command, the system will ask whether to delete the file. After you enter y, the file will be deleted. If you do not want to delete the file, the data will be n.
Example 2: Force delete the file and the system will not prompt again.
Command:
Rm-flog1.log
Output:
[Root @ localhosttest1] # ll
Total 4
-Rw-r-r-1rootroot2310-2614: 40log1. log
[Root @ localhosttest1] # rm-flog1.log
[Root @ localhosttest1] # ll
Total 0 [root @ localhosttest1] #
Example 3: delete any. log Files. Before deletion, ask for confirmation one by one.
Command:
Rm-i.log
Output:
[Root @ localhosttest1] # ll
Total 8
-Rw-r-r-1rootroot1110-2614: 45log1. log
-Rw-r-r-1rootroot2410-2614: 45log2. log
[Root @ localhosttest1] # 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 @ localhosttest1] # ll
Total 0 [root @ localhosttest1] #
Example 4: delete all files in the test1 sub-directory and sub-directory
Command:
Rm-rtest1
Output:
[Root @ localhosttest] # ll
Total 24drwxr-xr-x7rootroot409610-2518: 07scf
Drwxr-xr-x2rootroot409610-2614: 51test1.
Drwxr-xr-x3rootroot409610-2517: 44test2.
Drwxrwxrwx2rootroot409610-2517: 46test3.
Drwxr-xr-x2rootroot409610-2517: 56test4.
Drwxr-xr-x3rootroot409610-2517: 56test5.
[Root @ localhosttest] # rm-rtest1
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 @ localhosttest] # ll
Total 20drwxr-xr-x7rootroot409610-2518: 07scf
Drwxr-xr-x3rootroot409610-2517: 44test2.
Drwxrwxrwx2rootroot409610-2517: 46test3.
Drwxr-xr-x2rootroot409610-2517: 56test4.
Drwxr-xr-x3rootroot409610-2517: 56test5.
[Root @ localhosttest] #
Example 5: The rm-rftest2 command will delete all files in the test2 subdirectory and subdirectory, and do not confirm one by one
Command:
Rm-rftest2
Output:
[Root @ localhosttest] # rm-rftest2
[Root @ localhosttest] # ll
Total 16drwxr-xr-x7rootroot409610-2518: 07scf
Drwxrwxrwx2rootroot409610-2517: 46test3.
Drwxr-xr-x2rootroot409610-2517: 56test4.
Drwxr-xr-x3rootroot409610-2517: 56test5.
[Root @ localhosttest] #
Example 6: delete an object starting with-f
Command:
Rm -- f
Output:
[Root @ localhosttest] # touch -- f
[Root @ localhosttest] # ls -- f
-F [root @ localhosttest] # rm -- f
Rm: Do you want to delete the generally empty file "-f "? Y
[Root @ localhosttest] # ls -- f
Ls:-f: the file or directory does not exist.
[Root @ localhosttest] #
You can also follow these steps:
[Root @ localhosttest] # touch./-f
[Root @ localhosttest] # ls./-f
./-F [root @ localhosttest] # rm./-f
Rm: Do you want to delete the General empty file "./-f "? Y
[Root @ localhosttest] #
Example 7: Custom recycle bin Function
Command:
Myrm () {D =/tmp/(date + % Y % m % d % H % M % S); mkdir-p D; mv "@" D & echo "movedtodd OK ";}
Output:
[Root @ localhost test] # myrm () {D =/tmp/(date + % Y % m % d % H % M % S); mkdir-p D; mv "@" D & echo "moved to Dok ";}
[Root @ localhosttest] # aliasrm = 'myrm'
[Root @ localhosttest] # touch1.log2. log3.log
[Root @ localhosttest] # ll
Total 16
-Rw-r-r-1rootroot010-2615: 081.log
-Rw-r-r-1rootroot010-2615: 082.log
-Rw-r-r-1rootroot010-2615: 083.log
Drwxr-xr-x7rootroot409610-2518: 07scf
Drwxrwxrwx2rootroot409610-2517: 46test3.
Drwxr-xr-x2rootroot409610-2517: 56test4.
Drwxr-xr-x3rootroot409610-2517: 56test5.
[Root @ localhosttest] # rm [123]. log
Movedto/tmp/20121026150901ok
[Root @ localhosttest] # ll
Total 16drwxr-xr-x7rootroot409610-2518: 07scf
Drwxrwxrwx2rootroot409610-2517: 46test3.
Drwxr-xr-x2rootroot409610-2517: 56test4.
Drwxr-xr-x3rootroot409610-2517: 56test5.
[Root @ localhosttest] # ls/tmp/20121026150901/
1. log2.log3. log
[Root @ localhosttest] #
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.