File Comparisons

File Comparisons

-d Checks if file exists and is a directory
if [ -d $file ]
then
do_something
fi
-e Checks if file exists
if [ -e $file ]
then
do_something
fi
-f Checks if file exists and is a file
if [ -f $file ]
then
do_something
fi
-r Checks if file exists and is readable
if [ -r $file ]
then
do_something
fi
-s Checks if file exists and is not empty
if [ -s $file ]
then
do_something
fi
-w Checks if file exists and is writable
if [ -w $file ]
then
do_something
fi
-x Checks if file exists and is executable
if [ -x $file ]
then
do_something
fi
-O Checks if file exists and is owned by the current user
if [ -O $file ]
then
do_something
fi
-G Checks if file exists and the default group is the same as the current user
if [ -G $file ]
then
do_something
fi
file1 -nt file2 Checks if file1 is newer than file2
if [ $file1 -nt $file2 ]
then
do_something
fi
file1 -ot file2 Checks if file1 is older than file2
if [ $file1 -ot $file2 ]
then
do_something
fi