Linux RM Delete Command use method notes

Source: Internet
Author: User

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 the files and subdirectories under it.
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, some examples:
Instance one: Deleting files


#rm the Log.txt system asks if you want to delete it, and you delete the file when you enter Y

Instance two: Force delete file, do not need prompt


#rm-F Log1.log is deleted directly without prompting and used in shell scripts

Instance three: You must ask when you delete


#rm-I *.txt actually uses RM alone, and has implied-I, in this case, if the TXT file has more than one query


#rm: Remove regular empty file ' Aas.txt '? Y
#rm: Remove regular empty file ' Fd.txt '? Y
#rm: Remove regular empty file ' Ggs.txt '? Y

Example four: Deleting a folder


#rm-R Rubbish so that the folder named rubbish deleted, of course, the system is still to ask


Rm:descend into directory ' rubbish '? Y
Rm:remove regular empty file ' Rubbish/website.logs '? Y
Rm:remove regular empty file ' Rubbish/ggs.txt '? Y
Rm:remove regular empty file ' Rubbish/aas.txt '? Y
Rm:remove regular empty file ' Rubbish/web.logs '? Y
Rm:remove directory ' rubbish '? Y

If you don't need to ask for a deletion, you can use it, but do it carefully, haha


#rm-RF Rubbish


RM file Name
Delete the file and the system will ask if you want to delete it.
[Root@localhost test1]# Ls-l
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]# Ls-l
Total 0[root@localhost test1]#
Note: After you enter RM log.log command, the system asks if you want to delete it, then you delete the file after you enter Y, and you do not wish to delete the data n.



Rm-f file name
Forced deletion of the file, the system is no longer prompted.
[Root@localhost test1]# Ls-l
Total 4
-rw-r--r--1 root 10-26 14:40 log1.log
[Root@localhost test1]# rm-f Log1.log
[Root@localhost test1]# Ls-l
Total 0[root@localhost test1]#



Rm-i file name
Delete any files and ask for confirmation before deleting
[Root@localhost test1]# Ls-l
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]# Ls-l
Total 0[root@localhost test1]#


Rm-r Directory name
Delete all files in subdirectories and subdirectories   
[root@localhost test]# ls-l
Total 24drwxr-xr-x 7 root 4096 10-25 18:07 SCF
drwxr-xr-x 2 root 4096 10-26 14:51 test1
drwxr-xr-x 3 root root 4096 10-25 17:44 test2
drwxrwxrwx 2 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: Enter directory "Test1"? Y
Rm: Delete general files Test1/log 3.log "? Y
RM: Do you want to delete the directory "Test1"? y
[root@localhost test]# ls-l
Total 20drwxr-xr-x 7 root 4096 10-25 18:07 SCF
Drwxr-xr-x 3 root 4096 10-25 17:44 test2
drwxrwxrwx 2 root root 4096 10-25 17:46
test3 2 root ro OT 4096 10-25 17:56 test4
drwxr-xr-x 3 root root 4096 10-25 17:56
Test5 [root@localhost test]#
 
 

RM-RF Directory Name
Delete all files in the directory and subdirectory, and do not use one by one confirmation
[Root@localhost test]# RM-RF test2
[Root@localhost test]# Ls-l
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]#



RM---F
Delete a file that starts with-f
[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]#


Note: Interactively delete files in the current directory test and example

Rm-i Test Example
Remove Test? n (do not delete file test)
Remove example y (delete file example) Delete all files and subdirectories under the current directory except hidden files

# Rm-r * It should be noted that doing so is very dangerous!

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.