Check if a directory exists in a shell script

2018-12-31 14:29发布

What command can be used to check if a directory exists or not, within a shell script?

标签: shell unix posix
30条回答
长期被迫恋爱
2楼-- · 2018-12-31 15:20

As per Jonathan comment:

If you want to create the directory and it does not exist yet, then the simplest technique is to use mkdir -p which creates the directory — and any missing directories up the path — and does not fail if the directory already exists, so you can do it all at once with:

mkdir -p /some/directory/you/want/to/exist || exit 1
查看更多
裙下三千臣
3楼-- · 2018-12-31 15:23

To check if a directory exists you can use simple if structure like this:

if [ -d directory/path to a directory ] ; then
#Things to do

else #if needed #also: elif [new condition] 
# things to do
fi

You can do it also in negative

if [ ! -d directory/path to a directory ] ; then
# things to do when not an existing directory

Note: Be careful, leave empty spaces on either side of both opening and closing braces.

With the same syntax you can use:

-e: any kind of archive 

-f: file 

-h: symbolic link 

-r: readable file 

-w: writable file 

-x: executable file 

-s: file size greater than zero 
查看更多
君临天下
4楼-- · 2018-12-31 15:23
[[ -d "$DIR" && ! -L "$DIR" ]] && echo "It's a directory and not a symbolic link"

N.B: Quoting variables is a good practice.

查看更多
人间绝色
5楼-- · 2018-12-31 15:23

Great solutions out there, but ultimately every script will fail if you're not in the right directory. So code like this:

if [ -d "$LINK_OR_DIR" ]; then 
if [ -L "$LINK_OR_DIR" ]; then
    # It is a symlink!
    # Symbolic link specific commands go here
    rm "$LINK_OR_DIR"
else
    # It's a directory!
    # Directory command goes here
    rmdir "$LINK_OR_DIR"
fi
fi

will execute successfully only if at the moment of execution you're in a directory that has a subdirectory that you happen to check for.

I understand the initial question like this: to verify if a directory exists irrespective of the user's position in the file system. So using the command 'find' might do the trick:

dir=" "
echo "Input directory name to search for:"
read dir
find $HOME -name $dir -type d

This solution is good because it allows the use of wildcards, a useful feature when searching for files/directories. The only problem is that, if the searched directory doesn't exist, the 'find' command will print nothing to stdout (not an elegant solution for my taste) and will have nonetheless a zero exit. Maybe someone could improve on this.

查看更多
笑指拈花
6楼-- · 2018-12-31 15:25

Shorter form:

[ -d "$DIR" ] && echo "Yes"
查看更多
浮光初槿花落
7楼-- · 2018-12-31 15:25
  1. A simple script to test if dir or file is present or not:

    if [ -d /home/ram/dir ]   # for file "if [-f /home/rama/file]" 
    then 
        echo "dir present"
    else
        echo "dir not present"
    fi
    
  2. A simple script to check whether the directory is present or not:

    mkdir tempdir   # if you want to check file use touch instead of mkdir
    ret=$?
    if [ "$ret" == "0" ]
    then
        echo "dir present"
    else
        echo "dir not present"
    fi
    

    The above scripts will check the dir is present or not

    $? if the last command sucess it returns "0" else non zero value. suppose tempdir is already present then mkdir tempdir will give error like below:

    mkdir: cannot create directory ‘tempdir’: File exists

查看更多
登录 后发表回答