Home
Back To Top
Home
To check if a file exists or
not (of any type)
if [ -a $file_name ]
then
echo
"File $file_name exists";
else
echo
"File $file_name does not exists";
fi
OR
if [ -e $file_name ]
then
echo
"File $file_name exists";
else
echo
"File $file_name does not exists";
fi
To check if a file of block
special type exists or not
if [ -b $file_name ]
then
echo
"Block File $file_name exists";
else
echo
"Block File $file_name does not exists";
fi
To check if a file of
character special type exists or not
if [ -c $file_name ]
then
echo
"Character File $file_name exists";
else
echo
"Character File $file_name does not exists";
fi
To check if a file of type
directory exists or not
if [ -d $folder_name ]
then
echo
"Directory $folder_name exists";
else
echo
"Directory $folder_name does not exists";
fi
To check if a file of type
symbolic link exists or not
if [ -h $file_name ]
then
echo
"File $file_name exists which is a symbolic link";
else
echo
"File $file_name does not exists or not a symbolic link";
fi
OR
if [ -L $file_name ]
then
echo
"File $file_name exists which is a symbolic link";
else
echo
"File $file_name does not exists or not a symbolic link";
fi
To check if a file of regular
type exists or not
if [ -f $file_name ]
then
echo
"Regular file $file_name exists";
else
echo
"Regular file $file_name does not exists";
fi
To check if a file exists or
not and its SGID bit is set or not
if [ -g $file_name ]
then
echo
"File $file_name with SGID bit on exists";
else
echo
"File $file_name does not exists or SGID bit is off";
fi
To check if a file exists or
not and its STICKY bit is set or not
if [ -k $file_name ]
then
echo
"File $file_name with SGID bit on exists";
else
echo
"File $file_name does not exists or SGID bit is off";
fi
To check if a file of PIPE
special type exists or not
if [ -p $file_name ]
then
echo
"Named PIPE File $file_name exists";
else
echo
"Named PIPE File $file_name does not exists";
fi
To check if a file exists and
readable or not to current user
if [ -r $file_name ]
then
echo
"File $file_name exists and is readable";
else
echo
"File $file_name does not exists or its not readable ";
fi
To check if a file exists and
is of nonzero size
if [ -s $file_name ]
then
echo
"File $file_name exists and is nonzero size";
else
echo
"File $file_name does not exists or is of zero size";
fi
To check if a file exists or
not and its SUID bit is set or not
if [ -u $file_name ]
then
echo
"File $file_name exists and is readable";
else
echo
"File $file_name does not exists or is not readable ";
fi
To check if a file exists and
writable or not to current user
if [ -w $file_name ]
then
echo
"File $file_name exists and is writable";
else
echo
"File $file_name does not exists or is not writable ";
fi
To check if a file exists and
executable or not to current user
if [ -x $file_name ]
then
echo
"File $file_name exists and is executable";
else
echo
"File $file_name does not exists and is not executable ";
fi
To check if a file of socket
special type exists or not
if [ -S $file_name ]
then
echo
"Socket File $file_name exists";
else
echo
"Socket File $file_name does not exists";
fi
To check if a file newer than
other or not
# touch file_name2 file_name1
# ls -lrt file_name*
-rw-r--r--. 1 root root 0 Nov 12 22:54
file_name2
-rw-r--r--. 1 root root 0 Nov 12 22:54
file_name1
# if [ $file_name1 -nt $file_name2 ];
then
echo "File $file_name1 is Newer than
$file_name2 ";
else
echo "File $file_name1 is Older than
$file_name2 ";
fi
# touch file_name2
# ls -lrt file_name*
-rw-r--r--. 1 root root 0 Nov 12 22:54
file_name1
-rw-r--r--. 1 root root 0 Nov 12 22:58
file_name2
# if [ $file_name1 -nt $file_name2 ];
then
echo "File $file_name1 is Newer than
$file_name2 ";
else
echo "File $file_name1 is Older than
$file_name2 ";
fi
# touch file_name1
# ls -lrt file_name*
-rw-r--r--. 1 root root 0 Nov 12 22:54
file_name1
-r////w-r--r--. 1 root root 0 Nov 12 22:57
file_name2
# if [ $file_name1 -nt $file_name2 ]
then
echo "File $file_name1 is Newer than
$file_name2 ";
else
echo "File $file_name1 is Older than
$file_name2 ";
fi
To check if
a file older than other or not
# touch file_name2 file_name1
# ls -lrt file_name*
-rw-r--r--. 1 root root 0 Nov 12 22:54
file_name2
-rw-r--r--. 1 root root 0 Nov 12 22:54
file_name1
# if [ $file_name1 -ot $file_name2 ];
then
echo "File $file_name1 is Older than
$file_name2 ";
else
echo "File $file_name1 is Newer than
$file_name2 ";
fi
# touch file_name2
# ls -lrt file_name*
-rw-r--r--. 1 root root 0 Nov 12 22:54
file_name1
-rw-r--r--. 1 root root 0 Nov 12 22:58
file_name2
# if [ $file_name1 -ot $file_name2 ];
then
echo "File $file_name1 is Older than
$file_name2 ";
else
echo "File $file_name1 is Newer than
$file_name2 ";
fi
# touch file_name1
# ls -lrt file_name*
-rw-r--r--. 1 root root 0 Nov 12 22:54
file_name1
-rw-r--r--. 1 root root 0 Nov 12 22:57
file_name2
# if [ $file_name1 -ot $file_name2 ];
then
echo "File $file_name1 is Older than
$file_name2 ";
else
echo "File $file_name1 is Newer than
$file_name2 ";
fi
To check whether string variable is zero or not
# if [ -z $variaible ];
then
echo "String is zero ";
else
echo " String is nonzero ";
fi
To check whether length of string variable is nonzero
# if [ -n $variaible ];
then
echo "String length is nonzero ";
else
echo " String length is zero ";
fi
OR
# if [ $variaible ];
then
echo "String length is zero ";
else
echo " String length is nonzero ";
fi
To check two string variables are matching or not
# if [ $variaible1 == $variable2 ];
then
echo "Both Variables are matching ";
else
echo "Both Variable are not matching ";
fi
OR
# if [ $variaible1 != $variable2 ];
then
echo "Both Variables are not matching ";
else
echo "Both Variable are matching ";
fi
To check two integer variables are equal or not
# if [ $value1 -eq $value2 ];
then
echo "Both Values are equal";
else
echo "Both Values are not equal ";
fi
OR
# if [ $value1 -ne $value2 ];
then
echo "Both Values are not equal";
else
echo "Both Values are equal ";
fi
To check which integer variable is smaller
# if [ $value1 -gt $value2 ];
then
echo " $value1 is greater than $value2";
else
echo " $value1 is less than or equal to $value2";
fi
OR
# if [ $value1 -lt $value2 ];
then
echo " $value1 is less than $value2";
else
echo " $value1 is greater than or equal to
$value2";
fi
OR
# if [ $value1 -ge $value2 ];
then
echo " $value1 is greater than or equal to
$value2";
else
echo " $value1 is less than $value2";
fi
OR
# if [ $value1 -le $value2 ];
then
echo " $value1 is less than or equal to
$value2";
else
echo " $value1 is greater than $value2";
fi
Home
No comments:
Post a Comment