Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Linux by (18.4k points)

I don't understand what the ! and -d in the below command means.

if [ ! -d $directory ]

1 Answer

0 votes
by (36.8k points)
edited by

In bash command -d is to check if the directory exists or not.

For example, I having a directory called 

/home/sureshkumar/test/.

The directory variable contains:

"/home/sureshkumar/test/"

if [ -d $directory ]

This condition will be true only when a directory exists. 

I have changed the directory variable to

 "/home/a/b/". 

This directory does not exist.

if [ -d $directory ]

Now, the condition is false. If I put the ! in front of my directory does not exist, then this if the condition is true. If this directory does exists then the 

if [ ! -d $directory ] 

condition is false.

The procedure of the ! the operator is if this condition is true, then it gives the condition is false. If the command is false then it says the command is true. This is the work of! operator.

if [ ! -d $directory ]

This command true only if the $directory does not exist. If the directory exists, it returns false.

To know about Linux join the Linux training

Do check out the video below

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
2 answers
0 votes
1 answer

1.2k questions

2.7k answers

501 comments

693 users

Browse Categories

...