30

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

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

1
41
$ if test -d /the/dir; then echo "exist"; fi 
5
  • I need it in command line, not in a script.
    – Emanuel
    Jan 21 '10 at 15:33
  • 6
    That is command line. You can type that into bash directly, or you can resume it to test -d /the/dir: test -d /the/dir && echo "exist" || echo "does not exist" but they are really the same. Jan 21 '10 at 15:37
  • Not everybody's shell is bash! Nov 14 '16 at 10:29
  • @reinierpost then good luck to you... Nov 14 '16 at 10:57
  • @Stefano Borini: I still use tcsh because I'm too lazy to rewrite my .tcshrc. But more to the point: this might have explained the OP's problem. Nov 14 '16 at 23:25
14

Assuming your shell is BASH:

if [ -d /the/dir ]; then echo 'Exists'; else echo 'Not found'; fi
2
  • syntax error near unexpected token `then'
    – Emanuel
    Jan 21 '10 at 15:52
  • Then figure out what your shell is. Perhaps csh or tcsh? Nov 14 '16 at 10:28
9
[ -d /home/bla/ ] && echo "exits"
2
  • Can I have something like ELSE, to show a text even if the directory does´n exists?
    – Emanuel
    Jan 21 '10 at 15:40
  • Workaround: [ -d /home/bla/ ] && echo "exist" ; [ ! -d /home/bla/ ] && echo "doesnt exist" Oct 5 '16 at 2:31
9

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
3
  • 1
    The command doesn't appear to be doing by itself but when joined with a echo "Directory Exists".
    – Ejaz
    Aug 24 '16 at 21:17
  • @Steve Emmerson test is not a command. This is not working... Sep 27 '20 at 6:37
  • @ChamindaBandara then you have a non-compliant environment and/or operating system. Linux follows the Unix standard and that standard specifies the test(1) utility. See <pubs.opengroup.org/onlinepubs/9699919799>. Sep 27 '20 at 17:13
2

[ -d "YOUR_DIR" ] && echo "is a dir"

e.g.:

[ -d / ] && echo "root dir"

will output: root dir.

2
  • [-d: command not found this is what I get
    – Emanuel
    Jan 21 '10 at 15:35
  • Between "[" and "-" MUST be a space. This should work on Bourne and Bash shell.
    – dz
    Jan 21 '10 at 15:38
0

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 ....]

1
  • Welcome to Super User! This duplicates another answer and adds no new content. Please don't post an answer unless you actually have something new to contribute.
    – DavidPostill
    Nov 14 '16 at 10:52
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy