2018-06-10_Linuxで特定のファイルの権限と所有者に合わせる(chmod , chown)

このページで分かること

  • cui環境で特定の権限に合わせてファイルやディレクトリの権限を更新する方法

結論としてはchmod,chownコマンドで用意されている--referenceオプションを使うことで簡単に実現できます。

chmod --reference <基準にするファイルorディレクトリ> <適用するファイルorディレクトリ>

※chownも同様に使います。

簡単な解説

chmodやchownは指定したファイルと同じ権限や所有者を変更してくれる--referenceオプションが存在します。rootユーザー以外でtar.gzなどを展開した際に展開ユーザーに権限が書き換わってしまった場合などに使うと便利です。**数ファイルだけなど数が少ないときにササッと修正するときにオススメです。

**ファイル数が多い場合などはrootユーザーで展開し直したり、rsyncでコピーし直した方が早いかもしれません。

参考

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
###―――――本項目の箇所ここから―――――####
##基準 (/tmp/workdir)
##適用 (/tmp/tenkai)
[root@sv03 ~]# ls -l /tmp/workdir/ /tmp/tenkai/
/tmp/tenkai/:
合計 20
-rw-r--r-- 1 iuser iuser 4  6月  2 22:31 2017 file1
-rw-r--r-- 1 iuser iuser 4  6月  2 22:31 2017 file2
-rw-r--r-- 1 iuser iuser 4  6月  2 22:31 2017 file3
-rw-r--r-- 1 iuser iuser 4  6月  2 22:31 2017 file4
-rw-r--r-- 1 iuser iuser 4  6月  2 22:31 2017 file5

/tmp/workdir/:
合計 20
-rwxrwxrwx 1 test   test   4  6月  2 22:31 2017 file1
-rwx---rwx 1 ippan  gyoumu 4  6月  2 22:31 2017 file2
----rwxrwx 1 ippan  ippan  4  6月  2 22:31 2017 file3
-rwx------ 1 gyoumu test   4  6月  2 22:31 2017 file4
-------rwx 1 gyoumu gyoumu 4  6月  2 22:31 2017 file5

##権限の変更
[root@sv03 ~]# chmod --reference /tmp/workdir/file1 /tmp/tenkai/file1

##変更後確認 (両方とも777になっている)
[root@sv03 ~]# ls -l /tmp/workdir/file1 /tmp/tenkai/file1
-rwxrwxrwx 1 iuser iuser 4  6月  2 22:31 2017 /tmp/tenkai/file1
-rwxrwxrwx 1 test  test  4  6月  2 22:31 2017 /tmp/workdir/file1

##所有者とグループの変更
[root@sv03 ~]# chown --reference /tmp/workdir/file1 /tmp/tenkai/file1

##変更後確認(test:testになっている)
[root@sv03 ~]# ls -l /tmp/workdir/file1 /tmp/tenkai/file1
-rwxrwxrwx 1 test test 4  6月  2 22:31 2017 /tmp/tenkai/file1
-rwxrwxrwx 1 test test 4  6月  2 22:31 2017 /tmp/workdir/file1
[root@sv03 ~]#
###―――――ここまで―――――###

###―――――以下複数ファイルを処理する一例(コマンドを作りbashに食わせる)―――――###

##コマンドリスト作成の為に基準ファイル(ディレクトリ)と適用ファイル(ディレクトリ)
##をfindで一覧化
[root@sv03 ~]# find /tmp/workdir/ | sort > /tmp/list1
[root@sv03 ~]# find /tmp/tenkai/ | sort> /tmp/list2

##ファイル数が少ないので横並びにして一覧化出来ているか確認
[root@sv03 ~]# paste -d" " /tmp/list1 /tmp/list2
/tmp/workdir/ /tmp/tenkai/
/tmp/workdir/file1 /tmp/tenkai/file1
/tmp/workdir/file2 /tmp/tenkai/file2
/tmp/workdir/file3 /tmp/tenkai/file3
/tmp/workdir/file4 /tmp/tenkai/file4
/tmp/workdir/file5 /tmp/tenkai/file5

##上記の結果にchmodをコマンドを加える
[root@sv03 ~]# paste -d" " /tmp/list1 /tmp/list2 |\
gawk '{print "chmod --reference " $0}' > /tmp/list_chmod

##1行だけ確認
[root@sv03 ~]# head -1 /tmp/list_chown
chown --reference /tmp/workdir/ /tmp/tenkai/

##bashでコマンドリストの内容を一括実行 (-xは好み)
[root@sv03 ~]# bash -x /tmp/list_chmod
+ chmod --reference /tmp/workdir/ /tmp/tenkai/
+ chmod --reference /tmp/workdir/file1 /tmp/tenkai/file1
+ chmod --reference /tmp/workdir/file2 /tmp/tenkai/file2
+ chmod --reference /tmp/workdir/file3 /tmp/tenkai/file3
+ chmod --reference /tmp/workdir/file4 /tmp/tenkai/file4
+ chmod --reference /tmp/workdir/file5 /tmp/tenkai/file5

##変更後確認(権限)
[root@sv03 ~]# ls -l /tmp/workdir/ /tmp/tenkai/
/tmp/tenkai/:
合計 20
-rwxrwxrwx 1 test  test  4  6月  2 22:31 2017 file1
-rwx---rwx 1 iuser iuser 4  6月  2 22:31 2017 file2
----rwxrwx 1 iuser iuser 4  6月  2 22:31 2017 file3
-rwx------ 1 iuser iuser 4  6月  2 22:31 2017 file4
-------rwx 1 iuser iuser 4  6月  2 22:31 2017 file5

/tmp/workdir/:
合計 20
-rwxrwxrwx 1 test   test   4  6月  2 22:31 2017 file1
-rwx---rwx 1 ippan  gyoumu 4  6月  2 22:31 2017 file2
----rwxrwx 1 ippan  ippan  4  6月  2 22:31 2017 file3
-rwx------ 1 gyoumu test   4  6月  2 22:31 2017 file4
-------rwx 1 gyoumu gyoumu 4  6月  2 22:31 2017 file5

##所有者、グループの変更用コマンドリストを作成
##chmod表記 chown表記に置換で変更する
[root@sv03 ~]# sed s/chmod/chown/ /tmp/list_chmod > /tmp/list_chown

##1行だけ確認
[root@sv03 ~]# head -1 /tmp/list_chown
chown --reference /tmp/workdir/ /tmp/tenkai/

##bashでコマンドリストの内容を一括実行
[root@sv03 ~]# bash -x /tmp/list_chown
+ chown --reference /tmp/workdir/ /tmp/tenkai/
+ chown --reference /tmp/workdir/file1 /tmp/tenkai/file1
+ chown --reference /tmp/workdir/file2 /tmp/tenkai/file2
+ chown --reference /tmp/workdir/file3 /tmp/tenkai/file3
+ chown --reference /tmp/workdir/file4 /tmp/tenkai/file4
+ chown --reference /tmp/workdir/file5 /tmp/tenkai/file5

##変更後確認(所有者、グループ)
[root@sv03 ~]# ls -l /tmp/workdir/ /tmp/tenkai/
/tmp/tenkai/:
合計 20
-rwxrwxrwx 1 test   test   4  6月  2 22:31 2017 file1
-rwx---rwx 1 ippan  gyoumu 4  6月  2 22:31 2017 file2
----rwxrwx 1 ippan  ippan  4  6月  2 22:31 2017 file3
-rwx------ 1 gyoumu test   4  6月  2 22:31 2017 file4
-------rwx 1 gyoumu gyoumu 4  6月  2 22:31 2017 file5

/tmp/workdir/:
合計 20
-rwxrwxrwx 1 test   test   4  6月  2 22:31 2017 file1
-rwx---rwx 1 ippan  gyoumu 4  6月  2 22:31 2017 file2
----rwxrwx 1 ippan  ippan  4  6月  2 22:31 2017 file3
-rwx------ 1 gyoumu test   4  6月  2 22:31 2017 file4
-------rwx 1 gyoumu gyoumu 4  6月  2 22:31 2017 file5
[root@sv03 ~]#