Learn the command to create files and directories mkdir, today learn about the commands for deleting files and directories in Linux: RM command. RM is a commonly used command that removes one or more files or directories from a directory, and it can delete all files and subdirectories under a directory. For linked files, only the links are deleted and the original files remain unchanged.
RM is a dangerous command, especially when used, especially for novices, or the entire system will be destroyed in this command (e.g., RM *-rf under/(root directory)). So, before we execute RM, it's a good idea to check in which directory, exactly what to delete, and keep a high level of sanity in the operation.
1. Command format:
RM [Options] File ...
2. Command function:
Delete one or more files or directories in a 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:
-I,--interactive for interactive deletion
- F,--force ignores nonexistent files and never gives hints
- R,-R,--recursive instructs RM to recursively delete all directories and subdirectories listed in the parameters
4. Command instance:
Instance one: Delete the Text1 after creating the Text1 file, the system will first ask whether to delete
Command:
RM file name
Output:
[BEGIN] 2015/12/24 23:44:26[[email protected] ~]# [[email protected] ~]# lsanaconda-ks.cfg install.log install.log.syslo G Log.txt text[[email protected] ~]# touch Text1[[email protected] ~]# lsanaconda-ks.cfg install.log install.log.syslo G Log.txt text Text1[[email protected] ~]# RM text1rm:remove Regular empty file ' Text1 '? Y[[email protected] ~]# lsanaconda-ks.cfg install.log install.log.syslog log.txt text[end] 2015/12/24 23:44:52
After entering the RM Text1 command, the system will ask whether to delete, enter Y (yes) to delete the file, and do not want to delete the data n (NO).
Example Two: forcibly delete the newly created Text1, the system no longer prompts.
Command:
Rm-f Text1
Output:
[BEGIN] 2015/12/24 23:48:02[[email protected] ~]# [[email protected] ~]# Touch Text1[[email protected] ~]# LSANACONDA-KS.C FG Install.log Install.log.syslog log.txt text text1[[email protected] ~]# rm-f text1[[email protected] ~]# Lsanacon Da-ks.cfg install.log install.log.syslog log.txt text[end] 2015/12/24 23:48:32
Example Three: Delete any new. txt files; Ask for confirmation before deleting
Command:
rm-i *.txt
output:
[begin] 2015/12/24 23:52:04 [[email protected] ~]# lsanaconda-ks.cfg install.log install.log.syslog text[[email protected] ~]# [[email protected] ~]# touch text1.txt text2.txt text3.txt[[email protected] ~]# lsanaconda-ks.cfg install.log install.log.syslog text text1.txt text2.txt text3.txt[[email protected] ~]# rm -i *.txtrm: remove regular empty file ' Text1.txt '? yrm: remove regular empty file ' text2.txt '? yrm: remove regular empty file ' Text3.txt '? y[[email protected] ~]# Lsanaconda-ks.cfg install.log install.log.syslog text[end] 2015/12/24 23:52:36
Example four: Delete all files in the text subdirectory and subdirectories
Command:
rm-r Text
output:
[begin] 2015/12/24 23:58:34[[email protected] ~]# lsanaconda-ks.cfg Install.log install.log.syslog text[[email protected] ~]# tree /root /text//root/text/└── init.d ├── dnsmasq ├── firstboot ├── functions ├── haldaemon ├── halt ├── htcacheclean ├── httpd ├── ip6tables ├── iptables ├── irqbalance ├── mdmonitor ├── messagebus ├── NetworkManager ├── portreserve ├── postfix ├── psacct ├── quota_nld ├── udev-post └── xinetd1 directory, 19 files[[email protected] ~]# rm -r textrm: descend into directory ' text '? yrm: descend into directory ' text/init.d '? yrm: remove regular file ' text/init.d/dnsmasq '? yrm: remove regular file ' Text/init.d/postfix '? yrm: remove regular file ' Text/init.d/htcacheclean '? yrm: remove regular file ' text/init.d/ Portreserve '? yrm: remove regular file ' text/init.d/httpd '? yrm: remove regular file ' Text/init.d/messagebus '? yrm: remove regular file ' text /init.d/quota_nld '? yrm: remove regular file ' Text/init.d/haldaemon '? yrm: remove regular file ' Text/init.d/halt '? yrm: remove regular file ' text /init.d/mdmonitor '? yrm: remove regular file ' text/init.d/xinetd '? yrm: remove regular file ' text/init.d/iptables '? yrm: remove regular file ' text/init.d/ip6tables '? yrm: remove regular file ' Text/init.d/networkmanager '? yrm: remove regular file ' Text/init.d/psacct '? yrm: remove regular file ' text/init.d/ Irqbalance '? yrm: remove regular file ' text/init.d/firstboot '? yrm: remove regular file ' text/init.d/functions '? yrm: remove regular file ' text/ Init.d/udev-post '? yrm: remove directory ' text/init.d '? yrm: remove directory ' text '? y[[email protected] ~]# [end] 2015/12/24 23:59:35
Example five: The RM-RF command will delete all files in the text subdirectory and subdirectories without one by one confirmation
Command:
RM-RF text
Output:
[begin] 2015/12/25 0:05:00[[email protected] ~]# lsanaconda-ks.cfg Install.log install.log.syslog text[[email protected] ~]# tree /root /text/root/text└── init.d ├── haldaemon ├── halt ├── htcacheclean ├── httpd ├── NetworkManager ├── portreserve ├── postfix ├── psacct ├── quota_nld ├── rdisc ├── restorecond ├── rngd ├── rsyslog ├── wdaemon ├── winbind ├── wpa_supplicant └── xinetd1 directory, 17 files [[email protected] ~]# rm -rf text[[email protected] ~]# lsanaconda-ks.cfg Install.log install.log.syslog[end] 2015/12/25 0:05:37
A Linux RM command every day