Basic use of mkdir and rmdir commands in Linux system

insert image description here

1. Introduction to mkdir command

1.1 Introduction to the mkdir command

mkdir is a command used to create a directory, its full name is "make directory", which means to create a new directory. This command can be used under any Unix or Unix-like operating system, including Linux, macOS, FreeBSD, etc.

1.2 The origin of the mkdir command

The origin of the mkdir command can be traced back to the Unix V1 version in 1971. In earlier versions of Unix, directories were manually created at specific locations in the filesystem hierarchy. Over time, the mkdir command and other filesystem tools have evolved to make administration of Unix filesystems easier and more efficient.

2. Use help of mkdir command

2.1 help information of mkdir command

On the shell terminal, check the help information of mkdir.

Basic format:

mkdir [选项] 目录名称
[root@server-01 ~]# mkdir --help
Usage: mkdir [OPTION]... DIRECTORY...
Create the DIRECTORY(ies), if they do not already exist.

Mandatory arguments to long options are mandatory for short options too.
  -m, --mode=MODE   set file mode (as in chmod), not a=rwx - umask
  -p, --parents     no error if existing, make parent directories as needed
  -v, --verbose     print a message for each created directory
  -Z                   set SELinux security context of each created directory
                         to the default type
      --context[=CTX]  like -Z, or if CTX is specified then set the SELinux
                         or SMACK security context to CTX
      --help     display this help and exit
      --version  output version information and exit

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'mkdir invocation'

2.2 Option explanation of mkdir command

Common options:

  • -p, create a multi-layer directory, that is, it can be created even if the parent directory does not exist.
  • -m, set permissions.
  • -v, show the creation process.
  • --version display version information

Three, the basic use of mkdir command

3.1 Check the version of mkdir

Check the version of mkdir

[root@server-01 ~]# mkdir --version
mkdir (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by David MacKenzie.

3.2 Create a new directory

To create a directory called test under the current directory, you can use the following command:

mkdir test
  • If you want to create a multi-level directory named path/to/my/folder under the current directory, you can use the following command:
mkdir -p  /test/a/b/c

3.3 Set directory permissions when creating a directory

  • If you want to create a directory named test under the current directory, and set the permission to the directory to 755 (read, write, execute permission), you can use the following command:
mkdir -m 755 test
  • View new directory permissions
[root@server-01 ~]# ls -ld test/
drwxr-xr-x 2 root root 4096 Sep  1 22:42 test/

3.4 Show the process of creating a directory

  • If you want to show the creation process as the directory is being created, you can use the following command:
[root@server-01 ~]# mkdir -pv test/a/b/c/d
mkdir: created directory ‘test/a’
mkdir: created directory ‘test/a/b’
mkdir: created directory ‘test/a/b/c’
mkdir: created directory ‘test/a/b/c/d’

.

3.5 Precautions when using

When using the mkdir command, there are some precautions:

  • The directory name cannot contain spaces or other special characters, such as *, ?, $, etc.

  • If you want to create a multi-level directory, you need to use the -p parameter.

  • No error will be generated if an existing directory is created, but the existing directory will not be overwritten.

  • If there is no permission to create a directory under the current directory, a Permission denied error will be reported.

  • If you try to create a directory with the same name as an existing file, a File exists error will be reported.

  • When using the mkdir command, you need to pay attention to the current working directory, otherwise the directory will be created in an incorrect location.

  • If you need to create a hidden directory, you need to add a "." before the directory name.

  • On Linux/Unix systems, file and directory names are case-sensitive.

4. Introduction to rmdir command

4.1 Introduction to the rmdir command

The rmdir command is a command for the file system of the operating system in the command line interface, which is used to delete the specified directory. This command can only delete empty directories. If there are files or subdirectories in the directory, they cannot be deleted.

4.2 rmdir command usage help

rmdir [选项] 目录名

Commonly used options include:

  • -p: delete directory and its parent directory recursively;
  • -v: Display the name of each deleted directory.

Five, the basic use of the rmdir command

5.1 Delete a single empty directory

For example, in the empty directory test/a/b/c/d, delete a single empty directory ./d

rmdir dirname
[root@server-01 ~]# ls -ld test/a/b/c/d/
drwxr-xr-x 2 root root 4096 Sep  1 22:44 test/a/b/c/d/
[root@server-01 ~]# rmdir test/a/b/c/d/
[root@server-01 ~]# tree ./test/
./test/
└── a
    └── b
        └── c

3 directories, 0 files

5.2 Recursively delete a directory and its parent directory

For example, in the empty directory test/a/b/c/, delete the directory and its parent directory recursively.

rmdir -p dirname
[root@server-01 ~]# ls -ld test/a/b/c/
drwxr-xr-x 2 root root 4096 Sep  1 22:58 test/a/b/c/
[root@server-01 ~]# rmdir -p test/a/b/c/
[root@server-01 ~]# ls test
ls: cannot access test: No such file or directory

5.2 Show each deleted directory name

With the -v option, each deleted directory name is displayed.

rmdir -v dirname
[root@server-01 ~]# rmdir -pv test/a/b/c/d/
rmdir: removing directory, ‘test/a/b/c/d/’
rmdir: removing directory, ‘test/a/b/c’
rmdir: removing directory, ‘test/a/b’
rmdir: removing directory, ‘test/a’
rmdir: removing directory, ‘test’

5.3 Precautions for use

  • When using the rmdir command to delete a directory, the directory must be empty; otherwise, the deletion will fail.

  • If you want to delete a non-empty directory, you can use the -r option of the rm command or use the rm -rf command.

  • When using the rmdir command to delete a directory, you need to have write permission for the directory and its parent directory.

  • Using the rmdir command to delete a non-existing directory will generate an error message.

  • When using the rmdir command to delete a directory, it is recommended to perform a backup operation first to avoid deleting important data by mistake.

  • When deleting system files or important directories, be careful to avoid system problems.

Guess you like

Origin blog.csdn.net/jks212454/article/details/132631766