SHELL determines whether the file directory or file exists

1, file descriptor

-e Determine whether the object exists

-d Determine whether the object exists and is a directory
-f Determine whether the object exists, and it is a regular file
-L Determine whether the object exists and is a symbolic link
-h Determine whether the object exists and is a soft link
-s Determine whether the object exists and the length is not 0
-r Determine whether the object exists and is readable
-w Determine whether the object exists and can be written
-x Determine whether the object exists and is executable
-O Determine whether the object exists and belongs to the current user
-G Determine whether the object exists and belongs to the current user group
-nt Determine whether file1 is newer than file2 ["/data/file1 " -nt "/data/file2"]
-ot Determine whether file1 is older than file2 ["/data/file1 " -ot "/data/file2" ]

2, example

Current Does the directory jar exist under the directory

if [-d "jar" ];

then
echo " yes"
else
echo " no"
fi

Does the file jar.sh exist in the current directory

if [! -f "jar.sh" ];

then
echo " yes"
else
echo " no"
fi

Note:

[! -f “jar.sh” ]’S [] requires spaces before and after the syntax, otherwise an error will be reported

-e Determine whether the object exists

-d Determine whether the object exists and is a directory
-f Determine whether the object exists, and it is a regular file
-L Determine whether the object exists and is a symbolic link
-h Determine whether the object exists and is a soft link
-s Determine whether the object exists and the length is not 0
-r Determine whether the object exists and is readable
-w Determine whether the object exists and can be written
-x Determine whether the object exists and is executable
-O Determine whether the object exists and belongs to the current user
-G Determine whether the object exists and belongs to the current user group
-nt Determine whether file1 is newer than file2 ["/data/file1 " -nt "/data/file2"]
-ot Determine whether file1 is older than file2 ["/data/file1 " -ot "/data/file2" ]

if [-d "jar" ];

then
echo " yes"
else
echo " no"
fi

if [! -f "jar.sh" ];

then
echo " yes"
else
echo " no"
fi

Leave a Comment

Your email address will not be published.