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: 94
Total answers/comments: 3
Guest [Entry]

[ -d /home/bla/ ] && echo "exits"
Guest [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 ....]"
Guest [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"