3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

install(1) コマンドでディレクトリを作りつつファイルを配置する

Last updated at Posted at 2021-08-12

coreutils の install(1)はファイルコピー時のownerやmodeを設定できるだけでなく、ディレクトリの作成も一度に行ってくれます。

hoge.conf/etc/bar/ へ 0600 として配置する時には mkdir/cp/chmod と3つ行うことになりますが、install コマンドなら一発です。

カレントディレクトリの hoge.conf/etc/bar/ ディレクトリへ 0600 としてコピーする時にはこのようになります。この時 /etc/bar/ が無ければさくせいしてく

$ sudo install -m 0600 -D -t /etc/bar/ hoge.conf

chown/chgrp相当も -o-g で指定できます。詳しくは install(1) をご覧ください。

注意点

ファイルのmodeやowner/groupは指定できますが、作成されるディレクトリは 0755 で固定です。(https://linuxjm.osdn.jp/info/GNU_coreutils/coreutils-ja_80.html)

比較内容

mkdir/cp/chmodを使った場合
$ ls hoge.conf
hoge.conf

$ sudo mkdir -p /etc/bar/
$ sudo cp hoge.conf /etc/bar/
$ sudo chmod 600 /etc/bar/hoge.conf

$ ls -laF /etc/bar/
total 12
drwxr-xr-x   2 root root 4096 Aug 12 09:45 ./
drwxr-xr-x 118 root root 4096 Aug 12 09:45 ../
-rw-------   1 root root  484 Aug 12 09:45 hoge.conf
installを使った場合
$ ls hoge.conf
hoge.conf

$ sudo install -m 0600 -D -t /etc/bar/ hoge.conf

$ ls -laF /etc/bar/
total 12
drwxr-xr-x   2 root root 4096 Aug 12 09:45 ./
drwxr-xr-x 118 root root 4096 Aug 12 09:45 ../
-rw-------   1 root root  484 Aug 12 09:45 hoge.conf

EoT

3
1
0

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
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?