LoginSignup
39

More than 1 year has passed since last update.

Linuxでのmkdirのm,pオプションの同時使用時の注意

Last updated at Posted at 2015-03-09

本文

Linuxにてmkdirコマンドでディレクトリを作成するときに

mkdir -m 777 hoge 

とするとパーミッション777のディレクトリhogeが作成できます。

mkdir -p hoge/fuga 

とするとディレクトリhogeの作成と同時に中にディレクトリfugaが作成できます。
この時のデフォルトパーミッションは

Linuxのファイル・ディレクトリ作成した時のパーミッション (umask)
http://qiita.com/yuki2006@github/items/3774bf765eb5ef7deabc
の記事通りです。

(ちなみに、-pはすでにディレクトリがあった時などのエラーメッセージ抑制にも使えます)

さて、-mと-pオプションを同時に使用したらどうなるでしょうか?

mkdir -m 777 -p hoge/fuga 

この場合、
ディレクトリhogeと同時に中にディレクトリfugaが作成できるのですが、

fugaがパーミッション777なのですが、

hogeディレクトリは デフォルトパーミッション(上記)になる罠があります。

ちなみに

  info coreutils 'mkdir invocation'

でみたマニュアルでは (man mkdirには書かれていない)

 Note: The `--mode',`-m' option only applies to the right-most
     directories listed on the command line.  When combined with
     `--parents', `-p' option, any parent directories are created with
     `u+wx' modified by umask.

と書いてあるので一応仕様通りらしいです。

代替策(installコマンド)

install --directory --mode=777 hoge/fuga
install -d -m 777 hoge/fuga

これで、hogeもfugaも777のパーミッションで作成されます。

参考

man MKDIR
http://linuxjm.sourceforge.jp/html/GNU_fileutils/man1/mkdir.1.html

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
39