Linux Learning Note 5--command RmDir and RM

Source: Internet
Author: User
Tags parent directory

Yesterday I learned the command to create a directory mkdir, and then learn about the commands for deleting files and directories in Linux: the RM command.

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.

The RM command can delete one or more files or directories in a directory, or it can delete all files and subdirectories of a directory and its subordinates. For linked files, only the entire linked file is deleted, and the original file remains unchanged. Note: Use the RM command with extreme caution. Because once a file is deleted, it can no longer be recovered. So, before deleting a file, it's a good idea to look at the contents of the file and determine if you really want to delete it. The RM command can be used with the-I option, which is particularly useful when deleting multiple files using the file extension name character. With this option, you will be asked to determine whether you want to delete it. In this case, you must enter Y and press ENTER to delete the file. If you press the ENTER key or other characters only, the file is not deleted.

The rmdir command is used to delete empty directories. When the directory is no longer in use, or if disk space has reached the usage limit, you need to delete the directory where the value is lost. Use the rmdir command to remove one or more empty subdirectories from a directory. This command removes one or more subdirectories from a directory, where dirname represents the directory name. If no path is specified in DirName, the directory specified by dirname in the current directory is deleted, or the directory at the specified location is deleted if the path is included in DirName. When you delete a directory, you must have write access to its parent directory.

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

-D: Delete the hard connection data of the directory you want to delete to 0, delete the directory;

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

Grammar

RmDir (option) (parameter)

Options

-P or--parents: delete the specified directory if the upper-level directory of the directory has become an empty directory;

--ignore-fail-on-non-empty: This option causes the rmdir command to ignore the error message that is caused by deleting a non-empty directory;

-V or-verboes: Displays the detailed execution process of the command;

--help: Displays the help information of the command;

--version: Displays the version information of the command.

Parameters

Directory list: The list of empty directories to delete. When you delete multiple empty directories, the directory names are separated by spaces.

Instance Delete subdirectory Os_1 and its parent directory bin

Cd/usr/meng/test

Rmdir-p Bin/os_1



Instance one: RmDir cannot delete a non-empty directory

Command:

RmDir doc

Output:

[Email protected] scf]# tree

.

|--bin

|--doc

| |--Info

| '--product

|--Lib

|--logs

| |--Info

| '--product

'--Service

'--Deploy

|--Info

'--product

Directories, 0 files

[Email protected] scf]# rmdir doc

Rmdir:doc: Directory Non-empty

[Email protected] scf]# rmdir doc/info

[Email protected] scf]# rmdir doc/product

[Email protected] scf]# tree

.

|--bin

|--doc

|--Lib

|--logs

| |--Info

| '--product

'--Service

'--Deploy

|--Info

'--product

Ten directories, 0 files

Description

RmDir Directory name command cannot delete a non-empty directory directly

Instance 2:rmdir-p when a subdirectory is deleted and makes it an empty directory, it is deleted by the way

Command:

Rmdir-p logs

Output:

[Email protected] scf]# tree

.

|--bin

|--doc

|--Lib

|--logs

| '--product

'--Service

'--Deploy

|--Info

'--product

Ten directories, 0 files

[Email protected] scf]# rmdir-p logs

Rmdir:logs: Directory Non-empty

[Email protected] scf]# tree

.

|--bin

|--doc

|--Lib

|--logs

| '--product

'--Service

'--Deploy

|--Info

'--product

9 Directories, 0 files

[Email protected] scf]# rmdir-p logs/product

[Email protected] scf]# tree

.

|--bin

|--doc

|--Lib

'--Service

'--Deploy

|--Info

'--product

7 directories, 0 files

I often use the RM and RmDir commands:

RM-FR filename/dirname: Force the deletion of files or folders without asking, and will be deleted regardless of whether the folder is non-empty.

Rm-r DirName: Recursively deletes the contents of a folder.

RM can delete files, delete folders, and use them more frequently than rmdir. More is the combination of LS and grep filtering to remove certain eligible files.

Linux Learning Note 5--command RmDir and RM

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.