Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Shell Scripting  - File test operators
2.1.
Example
3.
Frequently Asked Questions
3.1.
What are file test operators in shell script?
3.2.
What is the use of a file test operator?
3.3.
What is the use of the file test operator in Korn shell?
4.
Conclusion
Last Updated: Mar 27, 2024
Medium

Shell Scripting - File test operators

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Let's ensure we understand the foundational concepts before delving further into the subjects. Here is a brief introduction if you are unfamiliar with the Shell script.

File Test Operators Shell Scripting

shell script is a short piece of software designed to be run by the Unix shell, a command-line interpreter. A shell script is essentially a set of instructions that the shell in an operating system based on Unix follows.

In this blog, we will discuss a few operators for testing different properties in shell scripting.

Without further ado, let's get started.

Shell Scripting  - File test operators

💁 To examine different aspects of a Unix file's features, we have a few operators.

Assume a variable file contains the existing file "ninja.sh," which has the following permissions: read, write, and execute. 

Operator

Description

How to use

-b file Check to see if the file is a block special file; if yes, the condition is met.

[ -b $file ]

-c file Check to see if the file is a character special file; if yes, the condition is met.

[ -c $file ]

-d file Check to see if the file is a directory; if yes, the condition is met.

[ -d $file ]

-f file The condition is true if the file is an ordinary file as opposed to a directory or special file.

[ -f $file ]

-g file Check to see if the file's set group ID (SGID) bit is set; if yes, the condition is met.

[ -g $file ]

-k file Check to see if the file's sticky bit is set; if it is, the condition is met.

[ -k $file ]

-p file Check to see if this is a named pipe; if yes, the condition is met.

[ -p $file ]

-t file The condition is true if the file descriptor is open and linked to a terminal.

[ -t $file ]

-u file The condition is true if the file's Set User ID (SUID) bit is set.

[ -u $file ]

-r file Checks to see if the file can be read; if yes, the condition is met.

[ -r $file ]

-w file Checks to see if the file can be written; if yes, the condition is met.

[ -w $file ]

-x file Checks to see if the file can be executed; if yes, the condition is met.

[ -x $file ]

-s file Check to see if the file is larger than zero; if yes, the condition is met.

[ -s $file ]

-e file When a file is a directory but still exists, the existence check is true.

[ -e $file ]

Example

Each of the file test operators is used in the example below.

Assume a variable file contains the existing file "ninja.sh," which has the given permissions: read, write, and execute.

#!/bin/sh


file="ninja.sh"


if [ -r $file ]
then
   echo "The File has read access."
else
   echo "File does not have read access."
fi


if [ -w $file ]
then
   echo "The file has write access."
else
   echo "There is no write permission for the file."
fi


if [ -x $file ]
then
   echo "File has execute permission."
else
   echo "There is no execute permission for the file."
fi


if [ -f $file ]
then
   echo "File is an ordinary file."
else
   echo "This is a special file."
fi


if [ -d $file ]
then
   echo "File is a directory."
else
   echo "This is not a directory."
fi


if [ -s $file ]
then
   echo "File size is not zero."
else
   echo "File size is zero."
fi


if [ -e $file ]
then
   echo "File exists."
else
   echo "File does not exist."
fi

 

Output

File does not have read access.

There is no write permission for the file.

There is no execute permission for the file.

This is a special file.

This is not a directory.

File size is not zero.

File does not exist.

Output image
Output image

 

Read about Bitwise Operators in C here.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Frequently Asked Questions

What are file test operators in shell script?

You can test many features of a file, such as whether it is readable or not, whether the user has write permissions on a file, the file size, whether the file exists or not, etc. using file test operators. Your Bash scripts can be made error-free by using file test operators.

What is the use of a file test operator?

You can test many features of a file, such as whether it is readable or not, whether the user has write permissions on a file, the file size, whether the file exists or not, etc. using file test operators.

What is the use of the file test operator in Korn shell?

The Korn shell offers a built-in set of options for evaluating file properties, such as whether it is a directory, a plain file (not a directory), a readable file, and so on. It uses the test command to evaluate conditional expressions.

Conclusion

Congratulations on finishing the blog! We have discussed a few operators for testing different properties in shell scripting with an example and  their output.

We hope this blog has helped you enhance your knowledge of File test operators in shell Scripting. Check out the following links for further information:

🔥 Introduction to Linux Shell & Scripting Shell

🔥 Shell Scripting Interview Question

🔥 Types of Unix Operating System

Please refer to our guided pathways on Code studio to learn more about DSACompetitive ProgrammingJavaScriptSystem Design, etc. Enrol in our courses, and use the accessible sample exams and questions as a guide. For placement preparations, look at the interview experiences and interview package.

Please upvote 🏆 our blogs if you find them helpful and informative!

Previous article
Advance Bash Scripting Guide
Next article
Shell Scripting - How to append output to the end of text file
Live masterclass