【Linux】ファイル・ディレクトリの権限・所有者を再帰的に変更する

【Linux】ファイル・ディレクトリの権限・所有者を再帰的に変更する

ファイルの権限(パーミッション)、所有者を再帰的に変更する場合は、chmod・chownコマンドの-Rオプションを利用します。

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 .

) 実行結果例

$ ls -lRa .
.:
total 12
drwxr-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 12
drwxr-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.sh
drwxr-xr-x 2 test-user test-user 4096 Jul 17 22:38 test_script_r

./test_script/test_script_r:
total 8
drwxr-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 12
drwxr-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 12
drwxr-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.sh
drwxr-xr-x 2 test-user2 test-user2 4096 Jul 17 22:46 test_script_r

./test_script/test_script_r:
total 8
drwxr-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.sh

test.confは-rw-r--r--(644)のままで変わっていないことと、 すべてのファイル・ディレクトリの所有者:グループがrootへ変更されたことを確認できました。

まとめ

  • 権限や所有者(グループ)を再帰的に変更する場合は-Rオプションを利用する。
  • ディレクトリのみ、ファイルのみ、指定した拡張子を対象にする場合はchmodchownfindコマンドを組み合わせて指定する。
  • 権限変更前後はls等でリストを取得しておくとよい。

パーミッションの読み方などchmod,chownコマンドの基本的な使い方を確認したい場合は、こちらのリンクをご参照ください。


以上で本記事の解説を終わります。
よいITライフを!
Scroll to Top