Shell command to determine whether a file or folder exists, first look directly at the example:
#!/bin/sh
#判断文件存在 to determine if the folder
testpath= "/volumes/macbookprohd/mr.wen/08 shell command"
testfile=/volumes/ macbookprohd/mr.wen/08 shell command/filewen "
#判断文件夹是否存在-D
if [[!-D" $testPath "]]; then
echo" folder does not exist "
else
echo "folder exists"
fi
#判断文件夹是否存在 with executable permission
if [[!-X ' $testFile]]; then
echo ' file does not exist and does not have executable permissions ' C12/>else
echo "file exists and has executable permissions"
fi
#判断文件是否存在
if [[!-f ' $testFile ']]; then
echo "file does not exist"
else
echo "file exists"
fi
In the shell command script, you will encounter a variety of documents to judge, in addition to the above commonly used judgments, there are other can be used, as follows:
Conditional Logic on Files |
-A file exists. |
-B file exists and is a block special file. |
-c file exists and is a character special file. |
-D file exists and is a directory. |
-e file exists (just the same as-a). |
-F file exists and is a regular file. |
-G file exists and has its setgid (2) bit set. |
-G file exists and has the same group ID as this process. |
-K file exists and has its sticky bit set. |
-l file exists and is a symbolic link. |
-N string length is not zero. |
-o Named option is set on. |
-O file exists and is owned by the user ID of this process. |
-P file exists and is a-a-a-in-a-a-i-Special file or |
Named pipe. |
-R file exists and is readable by the current process. |
-S file exists and has a size greater than zero. |
-s file exists and is a socket. |
-T file descriptor number Fildes is open and associated with a |
Terminal device. |
-U file exists and has its setuid (2) bit set. |
-W file exists and is writable by the current process. |
-X file exists and is executable by the current process. |
-Z string length is zero. |