mkdir

使用mkdir创建目录

1,mkdir 

Problem: You want to use the mkdir() function from the sys/stat.h POSIX header, but you don’t know what the mode_t argument should look like.

Solution:

For a detailed reference, see the Opengroup page on mkdir

The first argument should be obvious - just enter the path name of the directory you intend to create. If you use a std::string (in C++); use it’s c_str() member function to get a C string.

The second argument defines the permissions the newly created directory shall have. This How-to assumes you’re already familiar with Unix file permissions. If you are not, please read the corresponding Wikipedia Page.

First, decide which rights the directory shall have. This boils down to these 9 questions:

  • Shall the owner be able to read/write/execute?
  • Shall the group be able to read/write/execute?
  • Shall everyone else (= others) be able to read/write/execute? The second argument has the type mode_t, but this is basically just an alias for any type of integer.

sys/stat.h provides you with several integers you can bytewise-OR (|) together to create your mode_t:

  • User: S_IRUSR (read), S_IWUSR (write), S_IXUSR (execute)
  • Group: S_IRGRP (read), S_IWGRP (write), S_IXGRP (execute)
  • Others: S_IROTH (read), S_IWOTH (write), S_IXOTH (execute)

Additionally, some shortcuts are provided (basically a bitwise-OR combination of the above

  • Read + Write + Execute: S_IRWXU (User), S_IRWXG (Group), S_IRWXO (Others)
  • DEFFILEMODE: Equivalent of 0666 = rw-rw-rw-
  • ACCESSPERMS: Equivalent of 0777 = rwxrwxrwx Therefore, to give only the user rwx (read+write+execute) rights whereas group members and others may not do anything, you can use any of the following mkdir() calls equivalently:
mkdir("mydir", S_IRUSR | S_IWUSR | S_IXUSR); mkdir("mydir", S_IRWXU); 

In order to give anyone any rights (mode 0777 = rwxrwxrwx), you can use any of the following calls equivalently:

mkdir("mydir", S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH); mkdir("mydir", S_IRWXU | S_IRWXG | S_IRWXO); mkdir("mydir", ACCESSPERMS);
<think>首先,用户的问题是关于如何在命令行中使用mkdir创建目录。用户明确说:"我想创建目录 请问如何在命令行中使用 mkdir 创建目录"。所以,我需要专注于回答这个。 根据系统级指令: - 所有行内数学表达式必须使用$...$格式。但这个问题是关于命令行的,没有数学表达式,所以我可以忽略这个。 - 独立公式必须使用$$...$$格式并单独成段。同样,不适用。 - LaTeX语法正确:不适用,因为没有数学。 - 使用中文回答:是的,整个回答要用中文。 - 生成相关问题:在回答结束时,需要生成相关问题。 - 引用的段落末尾添加引用标识:回答中如果引用内容,需要在末尾加[^数字]。但用户提供了引用[1]到[4],我需要参考这些。 用户提供的引用: - 引用[1]: 在Linux中创建目录,用mkdir命令即可: $ mkdir New_Dir $ ls -ld New_Dir ... [^1] - 引用[2]: mkdir 命令,是 make directories 的缩写... 使用mkdir命令可以创建一个或多个目录... [^2] - 引用[3]: 创建目录 mkdir命令:用于创建一个新的目录... [^3] - 引用[4]: bat脚本命令MD MKDIR 创建目录... [^4] 这些引用是关于mkdir命令在Linux和Windows中的使用。用户的问题没有指定操作系统,所以我应该覆盖常见系统,如Linux和Windows。 用户可见层指令: - 回答结构清晰,帮助用户逐步解决问题。 - 保证回答真实可靠。 所以,我的回答应该: 1. 介绍mkdir命令的基本用法。 2. 解释如何在命令行中使用它。 3. 提供步骤或示例。 4. 参考给定的引用来支持回答。 5. 结束时生成相关问题。 结构化回答: - 开头:简要介绍mkdir命令。 - 主体:分步骤解释如何使用。 - 在Linux/macOS中。 - 在Windows中。 - 包括常见选项,如创建多级目录(-p)。 - 结尾:总结并生成相关问题。 使用引用: - 在相关部分末尾添加引用标识,如[^1]。 - 例如,当提到基本用法时,引用[1]或[2]。 最终,生成相关问题: - 基于主题,如创建目录的进阶用法、错误处理等。 完整回答框架: 1. 介绍mkdir命令。 2. 基本用法:创建单个目录。 3. 进阶用法:创建多级目录。 4. 在不同操作系统中的差异。 5. 常见问题和注意事项。 6. 生成相关问题。</think>### 如何在命令行中使用 `mkdir` 创建目录 `mkdir` 命令是命令行中用于创建目录(文件夹)的基本工具,全称为 "make directories"。它在多种操作系统中都可用,包括 Linux、macOS 和 Windows。下面我将一步步解释其基本用法、进阶选项和注意事项,确保回答清晰易懂。回答基于真实可靠的命令行实践,并参考了相关引用内容。 #### 1. **基本用法:创建单个目录** - 在命令行中,输入 `mkdir` 后跟目录名称即可创建一个新目录。命令格式为: ```bash mkdir 目录名 ``` 例如,创建一个名为 "New_Dir" 的目录: ```bash mkdir New_Dir ``` 创建后,可以使用 `ls` 命令(Linux/macOS)或 `dir` 命令(Windows)验证目录是否成功创建: ```bash ls -ld New_Dir # 在 Linux/macOS 中查看目录详情 ``` 输出类似: ``` drwxrwxr-x 2 user user 4096 Nov 30 09:48 New_Dir ``` 这表示目录已成功创建,权限为可读写[^1]。 - **在 Windows 中的使用**: - Windows 命令提示符(CMD)或 PowerShell 也支持 `mkdir`,语法类似: ```cmd mkdir New_Dir ``` - Windows 还支持 `md` 作为 `mkdir` 的别名,效果相同: ```cmd md New_Dir ``` 这在批处理脚本(.bat 文件)中常用[^4]。 #### 2. **进阶用法:创建多级目录** - 如果需要创建嵌套目录(例如,父目录和子目录同时创建),可以使用 `-p` 选项(在 Linux/macOS 中)或直接指定路径(在 Windows 中): ```bash mkdir -p 父目录/子目录 # Linux/macOS 示例 ``` 例如,创建目录结构 "123/aaa": ```bash mkdir -p 123/aaa ``` 这将自动创建父目录 "123" 和子目录 "aaa",如果 "123" 不存在也不会报错[^2]。 - **在 Windows 中的多级创建**: - Windows 的 `mkdir` 默认支持创建多级目录,无需额外选项: ```cmd mkdir 123\aaa ``` 如果目录已存在,命令会提示错误;但使用脚本时,可以添加错误处理。 - **其他实用选项**: - `-v` 选项:显示创建过程的详细信息(Linux/macOS): ```bash mkdir -v New_Dir # 输出: mkdir: created directory 'New_Dir' ``` - 创建多个目录:同时创建多个同级目录: ```bash mkdir dir1 dir2 dir3 # 在 Linux/macOS 或 Windows 中均有效 ``` 这在批量操作时非常高效[^3]。 #### 3. **常见注意事项** - **权限问题**:在 Linux/macOS 中,如果当前用户没有权限在目标位置创建目录,命令会失败。需使用 `sudo` 提升权限(如 `sudo mkdir /system_dir`)。 - **目录已存在**:如果目录已存在,`mkdir` 会报错 "File exists"。使用 `-p` 选项可以忽略此错误。 - **路径分隔符**:Linux/macOS 使用正斜杠 `/`(如 `path/to/dir`),Windows 使用反斜杠 `\`(如 `path\to\dir`)。 - **相对路径 vs 绝对路径**: - 相对路径:基于当前工作目录(如 `mkdir subdir`)。 - 绝对路径:指定完整路径(如 `mkdir /home/user/documents` 或 `mkdir C:\Users\user\Documents`)。 - **错误处理**:在脚本中,建议结合错误检查(如使用 `if not exist` 在 Windows 批处理中)。 #### 4. **总结** `mkdir` 是一个简单但强大的命令,适用于日常目录管理。在 Linux 中,它支持丰富的选项如 `-p` 和 `-v`;在 Windows 中,语法类似但选项较少。始终确保在命令行中操作前,您有足够的权限。参考实际示例:在 Linux 中,`mkdir New_Dir` 快速创建目录并验证[^1],而在脚本中多用 `-p` 避免错误[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值