Linux commands must learn (5) RM Command _linux

Source: Internet
Author: User
Tags mkdir

Yesterday I learned the command to create files and directories mkdir, today to learn Linux delete files and directories command: RM command. 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 of its files and subdirectories. For linked files, only the link is deleted and the original file remains unchanged.

RM is a dangerous command to be particularly careful when used, especially for beginners, otherwise the entire system will be destroyed in this command (for example, under/(root directory) to perform RM *-RF). So, before we do RM, we'd better make sure which directory you want to delete, and maintain a high level of sanity in the operation.

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. Command instance:

Instance one: Delete file files, and the system will ask if you want to delete them first.

Command:

RM file Name

Output:

Copy Code code as follows:

[Root@localhost test1]# LL

Total 4

Copy Code code as follows:

-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

Copy Code code as follows:

Root@localhost test1]# LL

Total 0[root@localhost test1]#

Description

After you enter the RM log.log command, the system asks if you want to delete it, and then you delete the file after you enter Y, and you do not delete the data n.

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

Command:

Copy Code code as follows:

Rm-f Log1.log

Output:

Copy Code code as follows:

[Root@localhost test1]# LL

Total 4

Copy Code code as follows:

-rw-r--r--1 root 10-26 14:40 log1.log
[Root@localhost test1]# rm-f Log1.log
[Root@localhost test1]# LL

Total 0[root@localhost test1]#

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

Command:

Rm-i *.log

Output:

Copy Code code as follows:

[Root@localhost test1]# LL

Total 8

Copy Code code as follows:

-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]# LL

Total 0[root@localhost test1]#

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

Command:

Copy Code code as follows:

Rm-r test1

Output:

Copy Code code as follows:

[Root@localhost test]# LL

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

Copy Code code as follows:

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
[Root@localhost 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

Copy Code code as follows:

[Root@localhost test]# LL

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

Copy Code code as follows:

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
[Root@localhost test]#

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

Command:

Copy Code code as follows:

RM-RF test2

Output:

Copy Code code as follows:

[Root@localhost test]# RM-RF test2
[Root@localhost test]# LL

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

Copy Code code as follows:

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]#

Example six: Delete a file that starts with-f

Command:

RM---F

Output:

Copy Code code as follows:

[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

Copy Code code as follows:

[Root@localhost test]# ls---f

LS: F: no file or directory

Copy Code code as follows:

[Root@localhost test]#

You can also use the following procedure:

Copy Code code as follows:

[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

Copy Code code as follows:

[Root@localhost test]#

Example seven: Customizing the Recycle Bin Feature

Command:

Copy Code code as follows:

Myrm () {d=/tmp/$ (date +%y%m%d%h%m%s); Mkdir-p $D mv "$@" $D && echo "moved to $D OK";

Output:

Copy Code code as follows:

[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. Log. Log
[Root@localhost test]# LL
Total
-rw-r--r--root root-:. Log
-rw-r--r--root root-:. Log
-rw-r--r--root root-:. Log
Drwxr-xr-x root root-: SCF
drwxrwxrwx root root-: test
Drwxr-xr-x root root-: test
Drwxr-xr-x root root-: test
[Root@localhost test]# RM [].log
Moved To/tmp/ok
[Root@localhost test]# LL
Total Drwxr-xr-x root root-: SCF
drwxrwxrwx root root-: test
Drwxr-xr-x root root-: test
Drwxr-xr-x root root-: test
[Root@localhost test]# ls/tmp//
. log. Log
[Root@localhost test]#

Description

The above procedure simulates the Recycle Bin's effect, that is, when you delete a file, you just place the file in a temporary directory so that you can recover it when you need it.

Below to give you a detailed description of the next name: RM command

Use Rights: Any user

How to use: RM [options] name ...

Description: Delete files and directories.

Parameters: 

-I check to confirm before deleting.
-F Even if the original file property is set to read-only, also delete directly, do not need to confirm each.
-R Deletes the directory and the file below.

Example:

Delete any C language program file and ask for confirmation before deleting:

Rm-i *.c

Delete any files in the finished subdirectory and subdirectories:

Rm-r finished

Feature Description: Deletes a document or directory.

Syntax: RM [-dfirv][--help][--version][document or directory ...]

Supplemental Note: Perform RM directives to delete documents or directories, and if you want to delete the directory you must add the parameter "-r", otherwise the preset will only delete the document.

Parameters

-D or--directory deletes the hard connection data of the directory to be deleted directly to 0, deleting the directory.

-F or--force forces a document or directory to be deleted.

-I or--interactive ask the user before deleting the existing document or directory.

-R or-R or--recursive recursively handles any document and subdirectory in the specified directory.

The-V or--verbose displays the instruction execution process.

--help online Help.

--version Display version Information

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.