LoginSignup
2
0

More than 5 years have passed since last update.

シェルで空ファイル作成時にパーミッションを指定する方法

Posted at

シェルで空のファイルを新規作成するときにパーミッションを指定する方法。
作成した後で chmod してもよいが、ここでは初期パーミッションを設定する方法を記述する。

サンプル

permission_sample.sh
#!/bin/sh

PERMISSION=644
# ↑ファイルの初期パーミッションを指定
# 以下はそのまま流用する。

MAX=666    
# ファイル作成時の初期パーミッション最大値
# 4:read + 2:write = 6 まで
# 1:execute 実行権限が必要な場合は作成後に chmod で指定する

MASK_VALUE=$(printf %03d $(expr $MAX - $PERMISSION))
umask $MASK_VALUE 
touch newfile

確認

空のファイルが 644 のパーミッションで作成されていることが確認できる。

ls -l newfile

-rw-r--r--  1 user  group  0  7 24 13:15 newfile

2
0
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
2
0