Skip to main content

Linux Bash if statements to check whether a file exists or not


#!/bin/bash

echo "Checks whether a file exists or not, and identify the type of it"
echo -n "Enter path of the file: "
read thefile
echo


if [ -e $thefile ]
then
echo "$thefile exists."
fi

if [ -f $thefile ]
then
echo "$thefile exists and it is a regular file."
fi

if [ -r $thefile ]
then
echo "$thefile exists and it is a readable file."
fi

if [ -w $thefile ]
then
echo "$thefile exists and it is a writable file."
fi

if [ -x $thefile ]
then
echo "$thefile exists and it is an executable file."
fi

if [ -h $thefile ]
then
echo "$thefile exists and it is a symbolic link."
fi

if [ -p $thefile ]
then
echo "$thefile exists and it is a named pipe."
fi

if [ -S $thefile ]
then
echo "$thefile exists and it is a socket."
fi

if [ -b $thefile ]
then
echo "$thefile exists and it is a block-special file."
fi

if [ -c $thefile ]
then
echo "$thefile exists and it is a character-special file."
fi

if [ -k $thefile ]
then
echo "$thefile exists and its sticky bit is set."
fi

if [ -u $thefile ]
then
echo "$thefile exists and its SUID bit is set."
fi

if [ -g $thefile ]
then
echo "$thefile exists and its SGID bit is set."
fi

if [ -O $thefile ]
then
echo "$thefile exists and it is owned by the effective user."
fi

if [ -G $thefile ]
then
echo "$thefile exists and it is owned by the effective group."
fi

if [ -s $thefile ]
then
echo "$thefile exists and it has a size greater than zero."
fi


Comments

Popular posts from this blog