Shell命令(每天学一个shell命令)第十二天 mkdir命令实例:创建文件夹

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

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值