LoginSignup
347

More than 5 years have passed since last update.

posted at

updated at

ファイルやディレクトリのパーミッションを一括で置換したい

追記 2015/01/09 13:00

コメントで、さらにスマートな書き方をお教えいただきました。ありがとうございます!

追記 2015/01/10 02:20

更にコメントで、そもそもfindを使わずchmodだけで実現する方法をお教えいただきました!qiita、すごいです。


例えば、/path/to/dir以下のディレクトリのパーミッションを変更したいとき、

chmod -R 755 /path/to/dir

などとやると、/path/to/dir以下にあるディレクトリはもちろん、ファイルのパーミッションまで755になってしまいます。644にすると、ディレクトリも644になってしまって困ります。手動でディレクトリのパーミッションだけ直すのも何だかスマートではありません。

ディレクトリは755、ファイルは644に一発で置換したい、というときはこんなのがよい、とこちらの記事のコメントで紹介されておりました。

find /path/to/dir -type d -exec chmod 755 {} +
find /path/to/dir -type f -exec chmod 644 {} +

なるほど。あと、よく使うのであれば、~/.bashrcに関数を登録しておくのも便利かもしれません。

.bashrc
function chmod-r(){
  find $1 -type $2 -exec chmod $3 {} +
}

chmod-rの部分は他のコマンドとかぶらなければお好みで構わないのですが、chmod-r /path/to/dir d 755などと入力すると、find /path/to/dir -type d -exec chmod 755 {} +が実行されるようになります。

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
What you can do with signing up
347