How to check if a directory exists in Linux command line?

Posted on

Problem :

How to check if a directory exists in Linux command line?

Solution: [ -d ¨a¨ ]&&echo ¨exists¨||echo ¨not exists¨

Solution :

$ if test -d /the/dir; then echo "exist"; fi 

Assuming your shell is BASH:

if [ -d /the/dir ]; then echo 'Exists'; else echo 'Not found'; fi

The canonical way is to use the test(1) utility:

test -d path && echo "Directory Exists"

where path is the pathname of the directory you want to check for.

For example:

test -d Desktop/ && echo "Directory Exists"
Directory Exists
test -d Desktop1/ && echo "Directory Exists"
# nothing appers

[ -d /home/bla/ ] && echo "exits"

Leave a Reply

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