File Test – Shell Script

Video Tutorials
Artificial Intelligence Tutorial
Machine Learning Projects TutorialPython Video tutorial
Computer Graphics Video TutorialC++ Video Tutorial
Big Data Analytics Video TutorialSoft Computing Video Tutorial
Placement Video TutorialJava Video Tutorial

File Test – Shell Script

File test is the process of testing different file permissions such as read, write and execute. test command is used file attributes testing. The below table shows file-related tests with a test command:

A file exists and is a regular file


True if file

-f filename

Test if the file exists and the file is a regular file

-r filename

Test if the file exists and the file is readable

-w filename

Test if the file exists and the file is writable

-x filename

Test if the file exists and the file is executable

-d filename

Test if the file exists and the file is a directory

-s filename

A file exists and has a size greater than zero


1. Following Shell script demonstrates the file attributes testing using the test command

# Test file attributes-

if [ -f $1 ]
   echo "File exist and file is regular file"
   echo "File Does not exists"

if [ -r $1 ]
   echo "File exist and file readable"
   echo "File is not readable"

if [ -w $1 ]
   echo "File exist and file writeble"
   echo "File is not writeble"

if [ -x $1 ]
   echo "File exist and file executable"
   echo "File is not executable"

if [ -s $1 ]
   echo "File exist and size is more than zero"
   echo "File size is less than zero"

The output of the above program:

File exist and file regular file
File exist and file readable
File exist and file writeble
File exist and file executable
File exist and size is more than zero

2. Write a Shell script which accepts two file names as arguments through the command line, checks if the permissions for these two files are identical (same) and if permissions are identical (same) outputs the common permission, otherwise outputs each file name followed by its permissions.


First, we check whether two filenames are passed through the command line and if not we print the message as no arguments. Then we check whether both the files exists using test command (-e option is used for this purpose). Using ls -l command we read the file attributes of both the files and we extract the file permission using the cut command (from column 2 to 10), store in p1 and p2 variables. Using string comparison we check the equivalence of file attributes p1 and p2. If both the permission are same then we print the permissions are same and permissions are printed. If the permissions are different then permissions of both the files are printed along with file names.

if [ $# -ne 2 ]
   echo "No arguments"
elif [ ! -e $1 -o ! -e $2 ]
   echo "FIle does not exist"
   p1=`ls -l $1|cut -c2-10`
   p2=`ls -l $2|cut -c2-10`
   if [ $p1 = $p2 ]
     echo "File permissions are equal & is $p1"
     echo "File permissions are not equal"
     echo "1st file $p1"
     echo "2nd file $p2"

The output of the program is:


File permissions are not equal
1st file rw-rw-r--
2nd file rwxrwxrwx

File permissions are equal & is rw-rw-r--


3. Display only the names of all users who are logged in and also store the result in user.txt


echo `who | tee user.txt`

The output of the program:

$ sh
mahesh tty7 2018-11-27 22:01 (:0)

In the above program first, we display the currently logged in users using who command. Next, we use tee command which takes the output of who command and redirects the output to the user.txt file.

Leave a Comment

Your email address will not be published. Required fields are marked *