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

版权声明:本文虽为博主原创文章,但是,如果能有帮助的话,允许大家转载 https://blog.csdn.net/xinyuanqianxun1987/article/details/81566538

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 filename Return true filename exists and is a regular file, not including link file, directory, block special, character special
-e filename Return 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 filename Return true if filename is a block special file.
-c filename Return true if filename exists and is a character special file.
-d filename Return true filename exists and is a directory.
-e filename Return true filename exists (regardless of type).
-f filename Return true filename exists and is a regular file.
-g filename Return true filename exists and its set group ID flag is set.
-h filename Return 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 filename Return true filename exists and its sticky bit is set.
-n filename Return true the length of string is nonzero.
-p filename Return true filename is a named pipe (FIFO).
-r filename Return truefilename exists and is readable.
-s filename Return true filename exists and has a size greater than zero.
-t file_descriptor Return true the filename whose file descriptor number is file_descriptor is open and is associated with a terminal.
-u filename Return true filename exists and its set user ID flag is set.
-w filename Return 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 filename Return 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 string Return true the length of string is zero.
-L filename Return true filename exists and is a symbolic link.
-O filename Return true filename exists and its owner matches the effective user id of this process.
-G filename Return true filename exists and its group matches the effective group id of this process.
-S filename Return true filename exists and is a socket.
file1 -nt file2 True if file1 exists and is newer than file2.
file1 -ot file2 True if file1 exists and is older than file2.
file1 -ef file2 True if file1 and file2 exist and refer to the same file.

猜你喜欢

转载自blog.csdn.net/xinyuanqianxun1987/article/details/81566538