test ~ ファイルの演算子(よく使いそう) 条件 説明 -a FILE FILE が存在する( -e と同じ) -e FILE FILE が存在する( -a と同じ) -f FILE FILE が存在し、通常のファイルである -d FILE FILE が存在し、ディレクトリである -r FILE FILE が存在し、読み取り権限がある -w FILE FILE が存在し、書き込み権限がある -x FILE FILE が存在し、実行(または検索)権限がある -s FILE FILE が存在し、サイズが 0 より大きい
code
# test -a FILE | FILE が存在する
test -a sample1.txt
echo "test -a sample1.txt : \$?=$?"
touch sample1.txt
test -a sample1.txt
echo "test -a sample1.txt : \$?=$?"
stdout
test -a sample1.txt : $?=1
test -a sample1.txt : $?=0
code
# test -e FILE | FILE が存在する
test -e sample1.txt
echo "test -e sample1.txt : \$?=$?"
touch sample1.txt
test -e sample1.txt
echo "test -e sample1.txt : \$?=$?"
stdout
test -e sample1.txt : $?=1
test -e sample1.txt : $?=0
code
touch sample1.txt
ln -s sample1.txt sample2.txt
mkdir sample1
stat -c '%A %n' *
echo '-------------------------------'
# test -f FILE | FILE が存在し、通常のファイルである
test -f sample1
echo "test -f sample1 : \$?=$?"
test -f sample1.txt
echo "test -f sample1.txt : \$?=$?"
test -f sample2.txt
echo "test -f sample2.txt : \$?=$?"
stdout
drwxr-xr-x sample1
-rw-r--r-- sample1.txt
lrwxrwxrwx sample2.txt
-------------------------------
test -f sample1 : $?=1
test -f sample1.txt : $?=0
test -f sample2.txt : $?=0
code
touch sample1.txt
mkdir sample1
ln -s sample1 sample2
stat -c '%A %n' *
echo '-------------------------------'
# test -d FILE | FILE が存在し、ディレクトリである
test -d sample1
echo "test -d sample1 : \$?=$?"
test -d sample1.txt
echo "test -d sample1.txt : \$?=$?"
test -d sample2
echo "test -d sample2 : \$?=$?"
stdout
drwxr-xr-x sample1
-rw-r--r-- sample1.txt
lrwxrwxrwx sample2
-------------------------------
test -d sample1 : $?=0
test -d sample1.txt : $?=1
test -d sample2 : $?=0
code
touch sample1.txt sample2.txt
mkdir sample1 sample2
chmod 400 sample1.txt sample1
chmod 200 sample2.txt sample2
stat -c '%A %n' *
echo '-------------------------------'
# test -r FILE | FILE が存在し、読み取り権限がある
test -r sample1
echo "test -r sample1 : \$?=$?"
test -r sample1.txt
echo "test -r sample1.txt : \$?=$?"
test -r sample2
echo "test -r sample2 : \$?=$?"
test -r sample2.txt
echo "test -r sample2.txt : \$?=$?"
stdout
dr-------- sample1
-r-------- sample1.txt
d-w------- sample2
--w------- sample2.txt
-------------------------------
test -r sample1 : $?=0
test -r sample1.txt : $?=0
test -r sample2 : $?=1
test -r sample2.txt : $?=1
code
touch sample1.txt sample2.txt
mkdir sample1 sample2
chmod 200 sample1.txt sample1
chmod 400 sample2.txt sample2
stat -c '%A %n' *
echo '-------------------------------'
# test -w FILE | FILE が存在し、書き込み権限がある
test -w sample1.txt
echo "test -w sample1.txt : \$?=$?"
test -w sample2.txt
echo "test -w sample2.txt : \$?=$?"
stdout
d-w------- sample1
--w------- sample1.txt
dr-------- sample2
-r-------- sample2.txt
-------------------------------
test -w sample1.txt : $?=0
test -w sample2.txt : $?=1
code
touch sample1.txt sample2.txt
mkdir sample1 sample2
chmod 100 sample1.txt sample1
chmod 000 sample2.txt sample2
stat -c '%A %n' *
echo '-------------------------------'
# test -x FILE | FILE が存在し、実行(または検索)権限がある
test -x sample1
echo "test -x sample1 : \$?=$?"
test -x sample1.txt
echo "test -x sample1.txt : \$?=$?"
test -x sample2
echo "test -x sample2 : \$?=$?"
test -x sample2.txt
echo "test -x sample2.txt : \$?=$?"
stdout
d--x------ sample1
---x------ sample1.txt
d--------- sample2
---------- sample2.txt
-------------------------------
test -x sample1 : $?=0
test -x sample1.txt : $?=0
test -x sample2 : $?=1
test -x sample2.txt : $?=1
code
printf '' > sample1.txt
printf 0 > sample2.txt
stat -c '%s %n' *
echo '-------------------------------'
# test -s FILE | FILE が存在し、サイズが 0 より大きい
test -s sample1.txt
echo "test -s sample1.txt : \$?=$?"
test -s sample2.txt
echo "test -s sample2.txt : \$?=$?"
stdout
0 sample1.txt
1 sample2.txt
-------------------------------
test -s sample1.txt : $?=1
test -s sample2.txt : $?=0
test ~ ファイルの演算子(たまに使いそう) 条件 説明 -t FD ファイル記述子 FD が端末上で開かれている -h FILE FILE が存在し、シンボリックリンクである(-L と同じ) -L FILE FILE が存在し、シンボリックリンクである(-h と同じ) -N FILE FILE が存在し、最後に読み取られてから変更されている
code
# test -t FD | ファイル記述子 FD が端末上で開かれている
tty
test -t 0
echo "test -t 0 : \$?=$?"
stdout
not a tty
test -t 0 : $?=1
以下は端末上で test -t FD を実行した結果のキャプチャーです。
code
touch sample1.txt
ln -s sample1.txt sample2.txt
stat -c '%A %n' *
echo '-------------------------------'
# test -h FILE | FILE が存在し、シンボリックリンクである(-L と同じ)
test -h sample1.txt
echo "test -h sample1.txt : \$?=$?"
test -h sample2.txt
echo "test -h sample2.txt : \$?=$?"
# test -L FILE | FILE が存在し、シンボリックリンクである(-h と同じ)
test -L sample1.txt
echo "test -L sample1.txt : \$?=$?"
test -L sample2.txt
echo "test -L sample2.txt : \$?=$?"
stdout
-rw-r--r-- sample1.txt
lrwxrwxrwx sample2.txt
-------------------------------
test -h sample1.txt : $?=1
test -h sample2.txt : $?=0
test -L sample1.txt : $?=1
test -L sample2.txt : $?=0
code
# [[ -N FILE ]] | FILE が存在し、最後に読み取られてから変更されている
echo 'L1' >> sample1.txt
# バックグラウンドで 0.1 秒後にファイルを更新
( sleep 0.1; echo 'L2' >> sample1.txt ) &
# ファイルの読み取り
echo "$(<sample1.txt)"
# バックグラウンド処理の終了を待機
wait
test -N sample1.txt
echo "test -N sample1.txt : \$?=$?"
stdout
L1
test -N sample1.txt : $?=0
test ~ ファイルの演算子(所有者など) 条件 説明 -G FILE FILE が存在し、実行しているユーザーのグループが所有している -O FILE FILE が存在し、実行しているユーザーが所有している -g FILE FILE が存在し、SGID ビットが設定されている -u FILE FILE が存在し、SUID ビットが設定されている -k FILE FILE が存在し、スティッキービットが設定されている
code
id -a
echo '-------------------------------'
stat -c '%U:%G %A %n' /home/corp/*.txt
echo '-------------------------------'
# test -G FILE | FILE が存在し、実行しているユーザーのグループが所有している
test -G /home/corp/corp.txt
echo "test -G /home/corp/corp.txt : \$?=$?"
test -G /home/corp/dep1.txt
echo "test -G /home/corp/dep1.txt : \$?=$?"
test -G /home/corp/dep2.txt
echo "test -G /home/corp/dep2.txt : \$?=$?"
stdout
uid=1000(user1) gid=100(corp) groups=100(corp),101(dep1)
-------------------------------
corp:corp -rw-r--r-- /home/corp/corp.txt
corp:dep1 -rw-r--r-- /home/corp/dep1.txt
corp:dep2 -rw-r--r-- /home/corp/dep2.txt
-------------------------------
test -G /home/corp/corp.txt : $?=0
test -G /home/corp/dep1.txt : $?=1
test -G /home/corp/dep2.txt : $?=1
code
id -a
echo '-------------------------------'
stat -c '%U:%G %A %n' /home/corp/dep1/user1.txt
stat -c ' %U:%G %A %n' /home/corp/dep1.txt
echo '-------------------------------'
# test -O FILE | FILE が存在し、実行しているユーザーが所有している
test -O /home/corp/dep1/user1.txt
echo "test -O /home/corp/dep1/user1.txt : \$?=$?"
test -O /home/corp/dep1.txt
echo "test -O /home/corp/dep1.txt : \$?=$?"
stdout
uid=1000(user1) gid=100(corp) groups=100(corp),101(dep1)
-------------------------------
user1:dep1 -rw-r--r-- /home/corp/dep1/user1.txt
corp:dep1 -rw-r--r-- /home/corp/dep1.txt
-------------------------------
test -O /home/corp/dep1/user1.txt : $?=0
test -O /home/corp/dep1.txt : $?=1
code
stat -c '%U:%G %A %n' /home/corp/dep1
stat -c '%U:%G %A %n' /home/corp/dep1/share
echo '-------------------------------'
# test -g FILE | FILE が存在し、SGID ビットが設定されている
test -g /home/corp/dep1
echo "test -g /home/corp/dep1 : \$?=$?"
test -g /home/corp/dep1/share
echo "test -g /home/corp/dep1/share : \$?=$?"
stdout
corp:dep1 drwxr-xr-x /home/corp/dep1
corp:dep1 drwxr-sr-x /home/corp/dep1/share
-------------------------------
test -g /home/corp/dep1 : $?=1
test -g /home/corp/dep1/share : $?=0
code
stat -c '%U:%G %A %n' /home/corp/corp.sh
echo '-------------------------------'
# test -u FILE | FILE が存在し、SUID ビットが設定されている
test -u /home/corp/corp.sh
echo "test -u /home/corp/corp.sh : \$?=$?"
stdout
corp:corp -rwsr-xr-x /home/corp/corp.sh
-------------------------------
test -u /home/corp/corp.sh : $?=0
code
stat -c '%U:%G %A %n' /tmp
echo '-------------------------------'
# test -k FILE | FILE が存在し、スティッキービットが設定されている
test -k /tmp
echo "test -k /tmp : \$?=$?"
stdout
root:root drwxrwxrwt /tmp
-------------------------------
test -k /tmp : $?=0
test ~ ファイルの比較 条件 説明 FILE1 -ef FILE2 FILE1 と FILE2 が同じデバイス番号と iノード番号を参照している FILE1 -nt FILE2 FILE1 が FILE2 より更新日が新しい、または FILE1 が存在し FILE2 が存在しない FILE1 -ot FILE2 FILE1 が FILE2 より更新日が古い、または FILE2 が存在し FILE1 が存在しない
code
touch sample1.txt
touch sample2.txt
ln sample1.txt sample3.txt
stat -c 'device=%d inode=%i %n' *
echo '-------------------------------'
# test FILE1 -ef FILE2 | FILE1 と FILE2 が同じデバイス番号と iノード番号を参照している
test sample1.txt -ef sample2.txt
echo "test sample1.txt -ef sample2.txt : \$?=$?"
test sample1.txt -ef sample3.txt
echo "test sample1.txt -ef sample3.txt : \$?=$?"
stdout
device=46 inode=3969513 sample1.txt
device=46 inode=3969514 sample2.txt
device=46 inode=3969513 sample3.txt
-------------------------------
test sample1.txt -ef sample2.txt : $?=1
test sample1.txt -ef sample3.txt : $?=0
code
touch -d '2023-01-01' sample1.txt
touch -d '2023-01-01' sample2.txt
touch -d '2023-01-01' sample3.txt
touch -d '2023-01-02' -m sample2.txt
stat -c 'ctime=%W mtime=%Y %n' *
echo '-------------------------------'
# test FILE1 -nt FILE2 | FILE1 が FILE2 より更新日が新しい、または FILE1 が存在し FILE2 が存在しない
test sample1.txt -nt sample2.txt
echo "test sample1.txt -nt sample2.txt : \$?=$?"
test sample2.txt -nt sample3.txt
echo "test sample2.txt -nt sample3.txt : \$?=$?"
test sample1.txt -nt sample4.txt
echo "test sample1.txt -nt sample4.txt : \$?=$?"
test sample4.txt -nt sample1.txt
echo "test sample4.txt -nt sample1.txt : \$?=$?"
stdout
ctime=1717283168 mtime=1672531200 sample1.txt
ctime=1717283168 mtime=1672617600 sample2.txt
ctime=1717283168 mtime=1672531200 sample3.txt
-------------------------------
test sample1.txt -nt sample2.txt : $?=1
test sample2.txt -nt sample3.txt : $?=0
test sample1.txt -nt sample4.txt : $?=0
test sample4.txt -nt sample1.txt : $?=1
code
touch -d '2023-01-01' sample1.txt
touch -d '2023-01-01' sample2.txt
touch -d '2023-01-01' sample3.txt
touch -d '2023-01-02' -m sample2.txt
stat -c 'ctime=%W mtime=%Y %n' *
echo '-------------------------------'
# test FILE1 -ot FILE2 | FILE1 が FILE2 より更新日が古い、または FILE2 が存在し FILE1 が存在しない
test sample1.txt -ot sample2.txt
echo "test sample1.txt -ot sample2.txt : \$?=$?"
test sample2.txt -ot sample3.txt
echo "test sample2.txt -ot sample3.txt : \$?=$?"
test sample1.txt -ot sample4.txt
echo "test sample1.txt -ot sample4.txt : \$?=$?"
test sample4.txt -ot sample1.txt
echo "test sample4.txt -ot sample1.txt : \$?=$?"
stdout
ctime=1717283168 mtime=1672531200 sample1.txt
ctime=1717283168 mtime=1672617600 sample2.txt
ctime=1717283168 mtime=1672531200 sample3.txt
-------------------------------
test sample1.txt -ot sample2.txt : $?=0
test sample2.txt -ot sample3.txt : $?=1
test sample1.txt -ot sample4.txt : $?=1
test sample4.txt -ot sample1.txt : $?=0
test ~ ファイルの演算子(特殊なファイル) 条件 説明 -S FILE FILE が存在し、ソケットファイルである -b FILE FILE が存在し、ブロックデバイスファイルである -c FILE FILE が存在し、キャラクタデバイスァイルである -p FILE FILE が存在し、名前付きパイプファイルである