いつも忘れるのでメモ.
find . -type f とかで file をリストして,そのそれぞれに chmod 644 をしたい,とかそういう話.Windows とかから Linux に rsync したすべて 777 の file/directory に対して,
chmod a-x,go-w . -R(全て 644 になる)- あるいは
chmod 644 . -R
- あるいは
- directory の mode を手動で 755 に調整
してもいいし,chmod 644 `find . -type f` で全てのファイルに対して chmod 644 してもいいのだけど,ファイル名やディレクトリ名にスペースが含まれると大変ややこしい.
2通りやり方があって,1つは find の -exec option を使う方法.{} は引数に置換され,\; で終了を表す.{} と \; の間にはスペースが必要.
% find . -type f -exec chmod 644 -v {} \;% find . -type d -exec chmod 755 -v {} \;
もう1つは xargs を使う方法.find の -print0 オプションは null 文字(^@)を区切りにしてリストするので,それを xargs -0 で 1つずつ処理していく.
% find . -type f -print0 | xargs -0 chmod 644% find . -type d -print0 | xargs -0 chmod 755
あるいは shell の機能でなんとかするか.$i を " でくくるのがポイント.
% find . -type f | while read i ; do chmod 644 "$i" ; done% find . -type d | while read i ; do chmod 755 "$i" ; done