Linuxのファイルの「権限(パーミッション)」を再帰的に変更するにはchmodコマンドの-Rオプションを使用し、
「所有者(オーナー):グループ」を変更する場合はchownコマンドの-Rオプションを使います。
再帰的に権限・所有者:グループを変更
カレントディレクトリ配下のファイルを対象に変更するコマンドを紹介します。
構文
「権限(パーミッション)」と「所有者(オーナー):グループ」を変更する構文はそれぞれ以下の通りです。
権限変更
chmod -R [変更したい権限] [ディレクトリ]所有者:グループ変更
chown -R [所有者:グループ] [ディレクトリ]使用するオプション
再帰的な変更をする際は、-Rオプションを利用します。
| オプション | 意味 |
|---|---|
| -R | ディレクトリ内の「権限(パーミッション)」・「所有者(オーナー):グループ」を再帰的に変更する。 |
ファイル・ディレクトリを再帰的に755にする
配下のファイル、ディレクトリすべてを755にする場合は、以下コマンドを実行します。
chmod -R 755 .ディレクトリのみを再帰的に755にする
配下のディレクトリすべてを755に変更する場合は、以下コマンドを実行します。
find . -type d -print | xargs chmod 755ファイルのみを再帰的に755にする
配下のファイルすべてを755に変更する場合は、以下コマンドを実行します。
find . -type f -print | xargs chmod 755指定した拡張子のファイルのみを再帰的に755にする
指定した拡張子(.shファイル) のファイルを755に変更する場合は、以下コマンドを実行します。
find . -type f -name "*.sh" -print | xargs chmod 755ファイル・ディレクトリの所有者:グループを再帰的に変更する
配下のファイル、ディレクトリすべてを所有者:グループをtest-user2に変更する場合は、以下コマンドを実行します。
sudo chown -R test-user2:test-user2 .変更前の権限・所有者情報を取得
権限や所有者を変更する際は、事前作業として権限を変更するディレクトリへ移動し、
配下の権限情報(BEFORE)を取得しておきます。
※誤った操作をした際に、戻せるようにするために取得することをお勧めします。
ls -lRa ..:total 12drwxr-xr-x 3 test-user test-user 4096 Jul 17 22:37 .drwxr-xr-x 3 test-user test-user 4096 Jul 17 22:40 ..drwxr-xr-x 3 test-user test-user 4096 Jul 17 22:38 test_script
./test_script:total 12drwxr-xr-x 3 test-user test-user 4096 Jul 17 22:38 .drwxr-xr-x 3 test-user test-user 4096 Jul 17 22:37 ..-rw-r--r-- 1 test-user test-user 0 Jul 17 22:37 test1.sh-rw-r--r-- 1 test-user test-user 0 Jul 17 22:37 test2.sh-rw-r--r-- 1 test-user test-user 0 Jul 17 22:37 test3.shdrwxr-xr-x 2 test-user test-user 4096 Jul 17 22:38 test_script_r
./test_script/test_script_r:total 8drwxr-xr-x 2 test-user test-user 4096 Jul 17 22:38 .drwxr-xr-x 3 test-user test-user 4096 Jul 17 22:38 ..-rw-r--r-- 1 test-user test-user 0 Jul 17 22:46 test.conf-rw-r--r-- 1 test-user test-user 0 Jul 17 22:38 test4.sh変更後の権限・所有者情報を取得
shファイルをパーミッション755へ、所有者:グループをtest-user2へ再帰的に変更した場合の実行結果例になります。
$ ls -lRa ..:total 12drwxr-xr-x 3 test-user2 test-user2 4096 Jul 17 22:37 .drwxr-xr-x 3 test-user2 test-user2 4096 Jul 17 22:40 ..drwxr-xr-x 3 test-user2 test-user2 4096 Jul 17 22:38 test_script
./test_script:total 12drwxr-xr-x 3 test-user2 test-user2 4096 Jul 17 22:38 .drwxr-xr-x 3 test-user2 test-user2 4096 Jul 17 22:37 ..-rwxr-xr-x 1 test-user2 test-user2 0 Jul 17 22:37 test1.sh-rwxr-xr-x 1 test-user2 test-user2 0 Jul 17 22:37 test2.sh-rwxr-xr-x 1 test-user2 test-user2 0 Jul 17 22:37 test3.shdrwxr-xr-x 2 test-user2 test-user2 4096 Jul 17 22:46 test_script_r
./test_script/test_script_r:total 8drwxr-xr-x 2 test-user2 test-user2 4096 Jul 17 22:46 .drwxr-xr-x 3 test-user2 test-user2 4096 Jul 17 22:38 ..-rw-r--r-- 1 test-user2 test-user2 0 Jul 17 22:46 test.conf-rwxr-xr-x 1 test-user2 test-user2 0 Jul 17 22:38 test4.shtest.confは-rw-r--r--(644)のままで変わっていないことと、
すべてのファイル・ディレクトリの所有者:グループがtest-user2へ変更されたことを確認できました。
まとめ
- 権限や所有者(グループ)を再帰的に変更する場合は
-Rオプションを利用する。 - ディレクトリのみ、ファイルのみ、指定した拡張子を対象にする場合は
chmodやchownとfindコマンドを組み合わせて指定する。 - 権限変更前後はls等でリストを取得しておくとよい。
パーミッションの読み方などchmod,chownコマンドの基本的な使い方を確認したい場合は、こちらのリンクをご参照ください。
以上で本記事の解説を終わります。
よいITライフを!