One linux command every day (5): rm command

Source: Internet
Author: User
One linux command every day (5): rm command link: One linux command every day (1): ls command http://www.2cto.com/ OS /201210/163049.html#linuxlinuxcommand every day (2): cd command quit (..
One linux command every day (5): rm command link: One linux command every day (1): ls command http://www.2cto.com/os/201210/163049.html ; One linux command every day (2): cd command http://www.2cto.com/os/201210/163050.html ; One linux command every day (3): pwd command http://www.2cto.com/os/201210/163462.html ; One linux command every day (4): mkdir command http://www.2cto.com/os/201210/163463.html 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. Www.2cto.com rm is a dangerous command. be careful when using it, especially for new users. 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 function: 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 parameter:-f, -- force ignores non-existent files and never gives a prompt. -I, -- interactive deletion-r,-R, and -- recursive indicate that rm will recursively delete all directories and subdirectories listed in the parameter. -V, -- verbose detailed display steps -- help displays this help information and exits -- version outputs version information and exits www.2cto.com 4. command instance: instance 1: to delete a file, the system will first ask whether to delete it. Command: rm file name output: [root @ localhost test1] # ll total 4-rw-r -- r -- 1 root 56 10-26 log. logroot @ localhost test1] # rm log. log rm: whether to delete the general file "log. log "? Yroot @ localhost test1] # ll Total 0 [root @ localhost test1] # Description: enter rm log. after the log command is run, the system will ask whether to delete the file. after entering y, the system will delete the file. if you do not want to delete the file, the data is n. Www.2cto.com instance 2: forcibly delete the file. Command: rm-f log1.log output: [root @ localhost test1] # ll total 4-rw-r -- r -- 1 root 23 10-26 log1.log [root @ localhost test1] # rm-f log1.log [root @ localhost test1] # ll Total 0 [root @ localhost test1] # instance 3: delete any. log file. before deletion, run the "rm-I *" command to confirm the deletion *. log output: [root @ localhost test1] # ll total 8-rw-r -- r -- 1 root 11 10-26 log1.log-rw-r -- 1 root 24 10-26 log2.log [root @ localhost test1] # rm-I *. logrm: whether to delete general files "Log1.log "? Yrm: Do you want to delete the general file "log2.log "? Y [root @ localhost test1] # ll Total 0 [root @ localhost test1] # instance 4: delete all files in the test1 subdirectory and subdirectory command: rm-r test1 output: [root @ localhost test] # ll total 24drwxr-xr-x 7 root 4096 10-25 scfdrwxr-xr-x 2 root 4096 10-26 test1drwxr-xr-x 3 root 4096 10-25 test2drwxrwxrwx 2 root 4096 10-25 test3drwxr-xr-x 2 root 4096 10-25 test4drwxr-xr-x 3 root 4096 10-25 test5 [root @ Localhost test] # rm-r test1rm: Do you want to enter the "test1" directory "? Yrm: Do you want to delete the general file "test1/log3.log "? Yrm: Do you want to delete the "test1" directory "? Y [root @ localhost test] # ll total 20drwxr-xr-x 7 root 4096 10-25 scfdrwxr-xr-x 3 root 4096 10-25 test2drwxrwxrwx 2 root 4096 10-25 test3drwxr-xr-x 2 root 4096 10-25 test4drwxr-xr-x 3 root 4096 10-25 test5 [root @ localhost test] # instance five: the rm-rf test2 command will delete all files in the test2 sub-directories and sub-directories, and you do not need to confirm the command one by one: rm-rf test2 output: [root @ localhost test] # rm-rf test2 [root @ localhost test] # Ll total 16drwxr-xr-x 7 root 4096 10-25 scfdrwxrwxrwx 2 root 4096 10-25 test3drwxr-xr-x 2 root 4096 10-25 test4drwxr-xr-x 3 root 4096 10-25 test5 [root @ localhost test] # www.2cto.com instance 6: run the "rm ---f" command to delete a file starting with "-f". the output is as follows: [root @ localhost test] # touch ---f [root @ localhost test] # ls ---f [root @ localhost test] # rm ---frm: do you want to delete the generally empty file "-f "? Y [root @ localhost test] # ls ---fls:-f: the file or directory [root @ localhost test] # can also be used as follows: [root @ localhost test] # touch. /-f [root @ localhost test] # ls. /-f. /-f [root @ localhost test] # rm. /-frm: whether to delete a common empty file ". /-f "? Y [root @ localhost test] # www.2cto.com instance 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 -- r -- 1 root 0 10-26. Log-rw-r -- 1 root 0 10-26 15:08 2. log-rw-r -- 1 root 0 10-26 15:08 3. logdrwxr-xr-x 7 root 4096 10-25 scfdrwxrwxrwx 2 root 4096 10-25 test3drwxr-xr-x 2 root 4096 10-25 test4drwxr-xr-x 3 root 4096 10-25 test5 [root @ localhost test] # rm [123]. logmoved to/tmp/20121026150901 OK [root @ localhost test] # ll total 16drwxr-xr-x 7 root 4096 10 -25 scfdrwxrwxrwx 2 root 4096 10-25 test3drwxr-xr-x 2 root 4096 10-25 test4drwxr-xr-x 3 root 4096 10-25 test5 [root @ localhost test] # ls/ tmp/20121026150901/1 .log 2.log 3.log[ root @ localhost test] # description: 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.
 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.