Home » Questions » Computers [ Ask a new question ]

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

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

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

Asked by: Guest | Views: 296
Total answers/comments: 3
bert [Entry]

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

"To check if a directory exists in a shell script you can use the following:

dir=$1

if [ -d ""$dir"" ]; then

#means that $dir exists.

fi

to check the opposite , add ! before the -d ->[ ! -d ....]"
"To check if a directory exists in a shell script you can use the following:

dir=$1

if [ -d ""$dir"" ]; then

#means that $dir exists.

fi

to check the opposite , add ! before the -d ->[ ! -d ....]"
bert [Entry]

"I usually just ls it:
ls my/directory/

If it exists, you'll see its contents, if it doesn't exist you'll see an error like this:
ls: cannot access 'my/directory': No such file or directory"