Testing files in shell.

# ------------------------------------------------------------------------------
# > SHELL FILE TEST OPERATORS
# ------------------------------------------------------------------------------

# The following operators returns true if file exists:

-b <file_path>
    File exists and is block special.

-c <file_path>
    File exists and is character special.

-d <file_path>
    File exists and is a directory.

-e <file_path>
    File exists.

-f <file_path>
    File exists and is a regular file.

-g <file_path>
    File exists and is set-group-ID.

-G <file_path>
    File exists and is owned by the effective group ID.

-h <file_path>
    File exists and is a symbolic link (same as -L).

-k <file_path>
    File exists and has its sticky bit set.

-L <file_path>
    File exists and is a symbolic link (same as -h).

-O <file_path>
    File exists and is owned by the effective user ID.

-p <file_path>
    File exists and is a named pipe.

-r <file_path>
    File exists and read permission is granted.

-s <file_path>
    File exists and has a size greater than zero.

-S <file_path>
    File exists and is a socket.

-t <file_descriptor>
    File descriptor is opened on a terminal.

-u <file_path>
    File exists and its set-user-ID bit is set.

-w <file_path>
    File exists and write permission is granted.

-x <file_path>
    File exists and execute (or search) permission is granted.

## EXAMPLES

FILENAME="$HOME/filename.txt"
DATE=$(date +"%a %b %d %T %Y %z")

if [ ! -e "$FILENAME" ]; then
    echo "The file does not exist."
fi

if [ -r "$FILENAME" ]; then
    echo "$FILENAME is readable."
fi

if [ -w "$FILENAME" ]; then
    echo "$FILENAME is writeable"
fi

if [ -x "$FILENAME" ]; then
    echo "$FILENAME is executable"
fi

if [ -u $FILENAME ]; then
    echo "$FILENAME will run as user "`stat --printf=%U $FILENAME`""
fi

if [ -g $FILENAME ]; then
    echo "$FILENAME will run as group "`stat --printf=%G $FILENAME`""
fi

if [ -O $FILENAME ]; then
    echo "You own $FILENAME"
else
    echo "You don't own $FILENAME"
fi

if [ -G $FILENAME ]; then
    echo "Your group owns $FILENAME"
else
    echo "Your group doesn't own $FILENAME"
fi

if [ -L "$FILENAME" ]; then
    echo "$FILENAME is a symbolic link"

elif [ -f "$FILENAME" ]; then
    echo "$FILENAME is a regular file."

elif [ -b "$FILENAME" ]; then
    echo "$FILENAME is a block device"

elif [ -c "$FILENAME" ]; then
    echo "$FILENAME is a character device"

elif [ -d "$FILENAME" ]; then
    echo "$FILENAME is a directory"

elif [ -p "$FILENAME" ]; then
    echo "$FILENAME is a named pipe"

elif [ -S "$FILENAME" ]; then
    echo "$FILENAME is a socket"

else
    echo "I don't know what kind of file that is. Is this a Linux system?"
fi

## extra

echo "Appending $DATE to $FILENAME."
$DATE >> $FILENAME

# create (overwrite) file
> $FILENAME

# append to file
>> $FILENAME

# both output and errors to file
>$FILENAME 2>&1

# read from file
< $FILENAME

# Empty file
: > filename

# Reference
# http://www.cyberciti.biz/faq/unix-linux-test-existence-of-file-in-bash/