Bash Commands - File test operators

Returns true if...


-e             file exists

-a             file exists.     This is identical in effect to -e. It has been "deprecated," [34] and its use is discouraged.

-f              file is a regular file (not a directory or device file)
-s            file is not zero size
-d             file is a directory
-b             file is a block device
-c              file is a character device


device0="/dev/sda2"

# /
(root directory)
if [ -b "$device0" ]
then
echo "$device0 is a block device."
fi


# /dev/sda2 is a block device.

device1="/dev/ttyS1"
# PCMCIA modem card.
if [ -c "$device1" ]
then
echo "$device1 is a character device."
fi

# /dev/ttyS1 is a character device.


-p                      file is a pipe


function show_input_type()

{
[ -p /dev/fd/0 ] && echo PIPE || echo STDIN
}
show_input_type "Input"                             
# STDIN

echo "Input" | show_input_type                  # PIPE



-h                      file is a symbolic link

-L                         file is a symbolic link

-S                         file is a socket
-t                           file (descriptor) is associated with a terminal device
This test option may be used to check whether the stdin [ -t 0 ] or stdout [ -t 1 ] in a
given script is a terminal.
-r                           file has read permission (for the user running the test)
-w                       file has write permission (for the user running the test)
-x                         file has execute permission (for the user running the test)
-g                         set-group-id (sgid) flag set on file or directory

If a directory has the sgid flag set, then a file created within that directory belongs to the group that
owns the directory, not necessarily to the group of the user who created the file. This may be useful
for a directory shared by a workgroup.

-u                            set-user-id (suid) flag set on file

-k                            sticky bit set

-O                             you are owner of file
-G                             group-id of file same as yours
-N                             file modified since it was last read
f1 -nt f2                    file f1 is newer than f2
f1 -ot f2                    file f1 is older than f2
f1 -ef f2                    files f1 and f2 are hard links to the same file
!                                    "not" -- reverses the sense of the tests above (returns true if condition absent).





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值