shell 判断文件是否存在,-e or -f ?

Sometimes, we have met issue how to check whether one file exists using shell script.

you use "-e" or "-f" ? the two of them have some different which may mislead you if you don't know their exact usage.

-f filenameReturn true filename exists and is a regular file, not including link file, directory, block special, character special
-e filenameReturn true filename exists (regardless of type).

"regular file", as most UNIX/POSIX docs refer generically to all types of file system entries a simply "files", e.g., a symbolic link is a type of a file, as is a named pipe, regular file, directory, block special, character special, socket, et(转https://www.cyberciti.biz/tips/find-out-if-file-exists-with-conditional-expressions.html)

So, you see -e might stop you making mistakes.

 

Append the full list to check file exist.

[ Expression ]Meaning
-b filenameReturn true if filename is a block special file.
-c filenameReturn true if filename exists and is a character special file.
-d filenameReturn true filename exists and is a directory.
-e filenameReturn true filename exists (regardless of type).
-f filenameReturn true filename exists and is a regular file.
-g filenameReturn true filename exists and its set group ID flag is set.
-h filenameReturn true filename exists and is a symbolic link. This operator is retained for compatibility with previous versions of this program. Do not rely on its existence; use -L instead.
-k filenameReturn true filename exists and its sticky bit is set.
-n filenameReturn true the length of string is nonzero.
-p filenameReturn true filename is a named pipe (FIFO).
-r filenameReturn truefilename exists and is readable.
-s filenameReturn true filename exists and has a size greater than zero.
-t file_descriptorReturn true the filename whose file descriptor number is file_descriptor is open and is associated with a terminal.
-u filenameReturn true filename exists and its set user ID flag is set.
-w filenameReturn true filename exists and is writable. True indicates only that the write flag is on. The file is not writable on a read-only file system even if this test indicates true.
-x filenameReturn true filename exists and is executable. True indicates only that the execute flag is on. If file is a directory, true indicates that file can be searched.
-z stringReturn true the length of string is zero.
-L filenameReturn true filename exists and is a symbolic link.
-O filenameReturn true filename exists and its owner matches the effective user id of this process.
-G filenameReturn true filename exists and its group matches the effective group id of this process.
-S filenameReturn true filename exists and is a socket.
file1 -nt file2True if file1 exists and is newer than file2.
file1 -ot file2True if file1 exists and is older than file2.
file1 -ef file2True if file1 and file2 exist and refer to the same file.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值