mkdir方法用于创建一个新的目录。
- mkdir命令的基本用法如下所示:
[root@vagrant-centos65 linux_shell_example]# ls
example Example hello.php more.php ncflm ncflm1 ncflm2 tmp touch.php
[root@vagrant-centos65 linux_shell_example]# mkdir williams
[root@vagrant-centos65 linux_shell_example]# ls
example Example hello.php more.php ncflm ncflm1 ncflm2 tmp touch.php williams
上述命令将用给定的目录名在当前目录下创建一个目录。你并不一定要在一个目录下创建这个目录的新目录。你可以使用相对路径或者绝对路径来创建他们。
- 你想在其他文件夹下创建新的目录:
[root@vagrant-centos65 linux_shell_example]# mkdir ./williams/boy
[root@vagrant-centos65 linux_shell_example]# ls
example Example hello.php more.php ncflm ncflm1 ncflm2 tmp touch.php williams
[root@vagrant-centos65 linux_shell_example]# cd williams/
[root@vagrant-centos65 williams]# ls
boy
[root@vagrant-centos65 williams]# mkdir /data/linux_shell_example/williams/girl
[root@vagrant-centos65 williams]# ls
boy girl
以上既可以是绝对路径也可以是相对路径。
- 使用-p选项,mkdir命令会自动创建所有还不存在的父目录,推测单词当然是:parent:
[root@vagrant-centos65 linux_shell_example]# mkdir -p ./mkdir_command/test_parent_dir
[root@vagrant-centos65 linux_shell_example]# ls
example Example hello.php mkdir_command more.php ncflm ncflm1 ncflm2 tmp touch.php williams
[root@vagrant-centos65 linux_shell_example]# cd mkdir_command/
[root@vagrant-centos65 mkdir_command]# ls
test_parent_dir
不使用-p则会爆出类似如下错误:
mkdir: cannot create directory `./mkdir_command/practice’: No such file or directory
- 使用-p选项,同样可以在要创建的目录已经存在的情况下,阻止错误的发生。
[root@vagrant-centos65 linux_shell_example]# ls
example Example hello.php mkdir_command more.php ncflm ncflm1 ncflm2 tmp touch.php williams
[root@vagrant-centos65 linux_shell_example]# mkdir mkdir_command
mkdir: cannot create directory `mkdir_command’: File exists
?使用-p选项将阻止上述【file exists】错误信息的输出?
[root@vagrant-centos65 linux_shell_example]# mkdir -p mkdir_command
[root@vagrant-centos65 linux_shell_example]# ls
example Example hello.php mkdir_command more.php ncflm ncflm1 ncflm2 tmp touch.php williams
- 使用-m选项,你可以设置将要创建的目录的权限。
[root@vagrant-centos65 linux_shell_example]# mkdir -m 777 ./mkdir_command/mkdir_m_option
[root@vagrant-centos65 linux_shell_example]# cd mkdir_command/
[root@vagrant-centos65 mkdir_command]# ls
mkdir_m_option test_parent_dir
[root@vagrant-centos65 mkdir_command]# ls -lh
total 8.0K
drwxrwxrwx 2 root root 4.0K Jul 12 02:25 mkdir_m_option