Thursday, 24 December 2015

How To Use File Test Operator in Unix/Linux

In this article, we will see how to use file operators in scripting. We have number of test operators for file but we will see which are used extensively on day to day basis. Once you are good with the following file test operators you can explore the other operator as well using man page.

-s file is not zero size
-e file exists
-d file is a directory
-r file has read permission
-w file has write permission
-x file has execute permission

$ ls -ltr  abc.txt
-rw-rw----   1 baba     baba  0 Dec 24 05:20 abc.txt

F_File_Name="abc.txt"

How to check whether a file exists or not
if [ -e ${F_File_Name} ]
then
  echo "File Exists"
else
  echo "File Doesn't Exists"
fi


As by now we have learnt one liner if-else syntax, we can write above block as below:

$ [ -e ${F_File_Name} ] &&  echo "File Exists" || echo "File Doesn't Exists";

Output: File Exists
-e stands for exists and return TRUE if file exists.


How to check whether a file is empty/zero-size or not

if [ -s ${F_File_Name} ]
then
  echo "File Is Not Empty"
else
  echo "File Is Empty"
fi


[OR]

$ [ -s ${F_File_Name} ] &&  echo "File Is Not Empty" || echo "File Is Empty";

Output: File Exists
-s returns TRUE if file is not empty or else returns FALSE


How to check whether a file is directory or not

if [ -d ${F_File_Name} ]
then
  echo "File Is a Directory"
else
  echo "File Is Not a Directory"
fi


[OR]

$ [ -d ${F_File_Name} ] &&  echo "File Is a Directory" || echo "File Is Not a Directory";

Output: File Is Not a Directory
-s returns TRUE if file is not empty or else returns FALSE


How To Check Whether Testing User Has Read/Write/Execute Permission On File

if [ -r ${F_File_Name} ]
then
  echo "File Has Read Permission"
else
  echo "File Does Not Have Read Permission"
fi

[OR]

$ [ -r ${F_File_Name} ] &&  echo echo "File Has Read Permission" || echo "File Does Not Have Read Permission";


Output: File Has Read Permission
-r returns TRUE if file has read permission or else returns FALSE

  
if [ -w ${F_File_Name} ]
then
  echo "File Has Write Permission"
else
  echo "File Does Not Have Write Permission"
fi


[OR]

$ [ -w ${F_File_Name} ] &&  echo echo "File Has Write Permission" || echo "File Does Not Have Write Permission";


Output: File Has Read Permission
-w returns TRUE if file has write permission or else returns FALSE


if [ -x ${F_File_Name} ]
then
  echo "File Has Execute Permission"
else
  echo "File Does Not Have Execute Permission"
fi


[OR]

$ [ -x ${F_File_Name} ] &&  echo echo "File Has Execute Permission" || echo "File Does Not Have Execute Permission";


Output: File Has Read Permission
-x returns TRUE if file has execute permission or else returns FALSE


Conclusion: The file test operators are very useful in script writing, you will face situations where you need to check these operators and based on the output need to perform required action. Keep practicing..!!

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...