One Linux command per day (5): RM command

Source: Internet
Author: User

Yesterday I learned 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:

-F,--force ignores nonexistent files and never gives hints.

-I,--interactive for interactive deletion

-R,-R,--recursive instructs RM to delete all directories and subdirectories listed in the parameters recursively.

-V,--verbose detailed display of the steps performed

--HELP Display this help message and exit

--version output version information and exit

4. Command instance:

Instance one: Delete file, the system will first ask whether to delete.

Command:

RM file Name

Output:

[email protected] test1]# LL

Total 4

-rw-r--r--1 root root 10-26 14:31 log.log

[Email protected] test1]# RM log.log

RM: Do you want to delete the generic file "Log.log"? Y

[email protected] test1]# LL

Total 0[[email protected] test1]#

Description

After entering the RM log.log command, the system asks whether to delete the file after entering Y, and does not want to delete the data n.

Example two: Forcibly delete file, the system no longer prompt.

Command:

Rm-f Log1.log

Output:

[email protected] test1]# LL

Total 4

-rw-r--r--1 root root 10-26 14:40 Log1.log

[Email protected] test1]# rm-f Log1.log

[email protected] test1]# LL

Total 0[[email protected] test1]#

Example three: Delete any. log file, ask for confirmation before deleting

Command:

Rm-i *.log

Output:

[email protected] test1]# LL

Total 8

-rw-r--r--1 root root one 10-26 14:45 log1.log

-rw-r--r--1 root root 10-26 14:45 Log2.log

[Email protected] 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

[email protected] test1]# LL

Total 0[[email protected] test1]#

Example four: Delete all the files in the Test1 subdirectory and subdirectories

Command:

Rm-r test1

Output:

[email protected] test]# LL

Total 24drwxr-xr-x 7 root root 4096 10-25 18:07 SCF

Drwxr-xr-x 2 root root 4096 10-26 14:51 test1

Drwxr-xr-x 3 root root 4096 10-25 17:44 test2

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

[Email protected] test]# rm-r test1

RM: Do you want to enter the directory "Test1"? Y

RM: Do you want to delete the generic file "Test1/log3.log"? Y

RM: Do you want to delete the directory "Test1"? Y

[email protected] test]# LL

Total 20drwxr-xr-x 7 root root 4096 10-25 18:07 SCF

Drwxr-xr-x 3 root root 4096 10-25 17:44 test2

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

[Email protected] test]#

Example five: The RM-RF test2 command will delete all files in the Test2 subdirectory and subdirectories without one by one confirmation

Command:

RM-RF test2

Output:

[Email protected] test]# RM-RF test2

[email protected] test]# LL

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

[Email protected] test]#

Example six: Delete a file that begins with-F

Command:

RM---F

Output:

[[email protected] test]# touch---F

[[email protected] test]# ls---f

-f[[email protected] test]# RM---F

RM: Do you want to delete the generic empty file "-F"? Y

[[email protected] test]# ls---f

LS:-F: No file or directory

[Email protected] test]#

You can also use the following procedure:

[[email protected] test]# touch./-f

[[email protected] test]# ls./-f

./-f[[email protected] test]# rm./-f

RM: Delete the generic empty file "./-f"? Y

[Email protected] test]#

Example seven: Customizing the Recycle Bin Feature

Command:

Myrm () {d=/tmp/$ (date +%y%m%d%h%m%s), Mkdir-p $D, MV "[email protected]" $D && echo "moved to $D OK";}

Output:

[Email protected] test]# Myrm () {d=/tmp/$ (date +%y%m%d%h%m%s); Mkdir-p $D; MV "[email protected]" $D && echo "moved to $D OK"; }

[[email protected] test]# alias rm= ' Myrm '

[[email protected] test]# Touch 1.log 2.log 3.log

[email protected] test]# LL

Total 16

-rw-r--r--1 root root 0 10-26 15:08 1.log

-rw-r--r--1 root root 0 10-26 15:08 2.log

-rw-r--r--1 root root 0 10-26 15:08 3.log

Drwxr-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

[Email protected] test]# RM [123].log

Moved to/tmp/20121026150901 OK

[email protected] test]# LL

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

[Email protected] test]# ls/tmp/20121026150901/

1.log 2.log 3.log

[Email protected] test]#

Description

The above procedure simulates the effect of the Recycle Bin, that is, when deleting a file, simply place the file in a temporary directory so that it can be recovered when needed.

Resources:

http://codingstandards.iteye.com/blog/983531

One Linux command per day (5): RM command

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.