mkdir [OPTION]... DIRECTORY...
!subtitle:功能
创建目录。
!subtitle:类型
可执行文件(/usr/bin/mkdir),属于 coreutils。
!subtitle:参数
OPTION 选项:
-m, --mode=MODE - 设置文件的模式(权限),如果不设置此选项则默认使用 a=rwa 减去 umask 权限
-p, --parents - 自动创建上级目录;如果目录已经存在,不报错
-v, --verbose- 为创建的每一个目录打印信息
-Z - 将每个新建目录的 SELinux 安全上下文设为默认值
--context[=CTX] - 将每个新建目录的 SELinux 安全上下文设为 CTX
--help - 显示帮助
--version - 显示版本
DIRECTORY - 要创建的目录
!subtitle:创建一个目录
mkdir project
创建名为 project 的目录。
!subtitle:一次创建多个目录
mkdir src include bin
同时创建三个目录:src/、include/、bin/。
!subtitle:创建多级目录
mkdir -p project/src/utils
若 project 和 src 不存在,-p 会自动创建它们。
结果:
project/
└── src/
└── utils/
!subtitle:指定权限创建
mkdir -m 700 private
权限为:
drwx------
仅当前用户可访问。
!subtitle:用符号式权限
mkdir -m u=rwx,g=rx,o=rx public
等价于 chmod 755 public
即:
drwxr-xr-x
!subtitle:结合使用
mkdir -p -m 755 /opt/app/logs
自动创建 /opt/app(若不存在)并设置 logs 目录权限为 755。
!subtitle:显示创建过程
mkdir -v test
输出:
mkdir: created directory 'test'
!subtitle:显示层级
mkdir -vp a/b/c
输出:
mkdir: created directory 'a'
mkdir: created directory 'a/b'
mkdir: created directory 'a/b/c'
!subtitle:安全创建目录(不报错)
mkdir -p ~/Downloads/tmp
若目录已存在不会报错,非常适合脚本中使用。
!subtitle:在脚本中先判断再创建
[ ! -d logs ] && mkdir logs
如果 logs 目录不存在,则创建它。
!subtitle:一次创建一组带编号的目录
mkdir dir_{1..5}
创建:
dir_1/ dir_2/ dir_3/ dir_4/ dir_5/
!subtitle:结合变量使用
name="backup_$(date +%Y%m%d)"
mkdir -p ~/backups/$name
创建带日期的备份目录,比如:
~/backups/backup_20251025/
!subtitle:批量创建层级结构
mkdir -p project/{src,include,docs,build}
结果:
project/
├── src/
├── include/
├── docs/
└── build/
| 命令 | 说明 |
|---|---|
| touch | 创建普通文件或刷新访问时间 |
| mkfifo | 创建命名管道 |
| mknod | 创建设备文件 |
| mktemp | 创建临时文件或目录 |
| link | 创建硬链接 |
| ln | 创建硬链接或符号链接 |
MKDIR(1) User Commands MKDIR(1)
NAME
mkdir - make directories
SYNOPSIS
mkdir [OPTION]... DIRECTORY...
DESCRIPTION
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, with
their file modes unaffected by any -m option.
-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
AUTHOR
Written by David MacKenzie.
REPORTING BUGS
GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Report any translation bugs to <https://translationproject.org/team/>
COPYRIGHT
Copyright © 2023 Free Software Foundation, Inc. License GPLv3+: GNU
GPL version 3 or later <https://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.
SEE ALSO
mkdir(2)
Full documentation <https://www.gnu.org/software/coreutils/mkdir>
or available locally via: info '(coreutils) mkdir invocation'
GNU coreutils 9.4 April 2024 MKDIR(1)