如何在Bash shell脚本中检查目录是否存在?

4400

在Bash shell脚本中,哪个命令可以检查目录是否存在?

35个回答

12

This answer wrapped up as a shell script

示例

$ is_dir ~                           
YES

$ is_dir /tmp                        
YES

$ is_dir ~/bin                       
YES

$ mkdir '/tmp/test me'

$ is_dir '/tmp/test me'
YES

$ is_dir /asdf/asdf                  
NO

# Example of calling it in another script
DIR=~/mydata
if [ $(is_dir $DIR) == "NO" ]
then
  echo "Folder doesnt exist: $DIR";
  exit;
fi

is_dir

:判断指定路径是否是一个目录。
function show_help()
{
  IT=$(CAT <<EOF

  usage: DIR
  output: YES or NO, depending on whether or not the directory exists.

  )
  echo "$IT"
  exit
}

if [ "$1" == "help" ]
then
  show_help
fi
if [ -z "$1" ]
then
  show_help
fi

DIR=$1
if [ -d $DIR ]; then 
   echo "YES";
   exit;
fi
echo "NO";

10
根据Jonathan的评论:
如果您想创建目录并且该目录还不存在,则最简单的方法是使用mkdir -p。它可以创建目录和路径上缺失的任何目录,并且如果目录已经存在,它也不会失败,因此您可以一次完成所有操作:
mkdir -p /some/directory/you/want/to/exist || exit 1

10
一句话概括:
[[ -d $Directory ]] && echo true

9
if [ -d "$DIRECTORY" ]; then
    # Will enter here if $DIRECTORY exists
fi

这并不完全正确...

如果你想进入那个目录,你还需要对该目录具有执行权限。也许你还需要具备写入权限。

因此:

if [ -d "$DIRECTORY" ] && [ -x "$DIRECTORY" ] ; then
    # ... to go to that directory (even if DIRECTORY is a link)
    cd $DIRECTORY
    pwd
fi

if [ -d "$DIRECTORY" ] && [ -w "$DIRECTORY" ] ; then
    # ... to go to that directory and write something there (even if DIRECTORY is a link)
    cd $DIRECTORY
    touch foobar
fi

9

以一种三元形式,

[ -d "$directory" ] && echo "exist" || echo "not exist"

并且使用 test

test -d "$directory" && echo "exist" || echo "not exist"

8

ls 命令与 -l(长列表)选项一起使用,返回有关文件和目录的属性信息。
特别是 ls -l 输出的第一个字符通常是 d-(破折号)。如果是 d,则所列出的是目录。

以下命令只需一行即可告诉您给定的 ISDIR 变量是否包含路径到目录:

[[ $(ls -ld "$ISDIR" | cut -c1) == 'd' ]] &&
    echo "YES, $ISDIR is a directory." || 
    echo "Sorry, $ISDIR is not a directory"

实际应用:
    [claudio@nowhere ~]$ ISDIR="$HOME/Music" 
    [claudio@nowhere ~]$ ls -ld "$ISDIR"
    drwxr-xr-x. 2 claudio claudio 4096 Aug 23 00:02 /home/claudio/Music
    [claudio@nowhere ~]$ [[ $(ls -ld "$ISDIR" | cut -c1) == 'd' ]] && 
        echo "YES, $ISDIR is a directory." ||
        echo "Sorry, $ISDIR is not a directory"
    YES, /home/claudio/Music is a directory.

    [claudio@nowhere ~]$ touch "empty file.txt"
    [claudio@nowhere ~]$ ISDIR="$HOME/empty file.txt" 
    [claudio@nowhere ~]$ [[ $(ls -ld "$ISDIR" | cut -c1) == 'd' ]] && 
        echo "YES, $ISDIR is a directory." || 
        echo "Sorry, $ISDIR is not a directoy"
    Sorry, /home/claudio/empty file.txt is not a directory

+1,但是当ISDIR根本不存在时,您会收到错误消息以及诊断消息。 - ysap

8
file="foo" 
if [[ -e "$file" ]]; then echo "File Exists"; fi;

需要进行解释(通过编辑您的答案,而不是在评论中)。 - Peter Mortensen

7

虽然有很好的解决方案,但如果您不在正确的目录中,最终每个脚本都会失败。因此,像这样的代码:

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

只有在执行时你在一个包含你需要检查的子目录的目录中,这个命令才会成功执行。
我理解最初的问题是:无论用户在文件系统中的位置如何,都要验证目录是否存在。因此使用“find”命令可能会有用。
dir=" "
echo "Input directory name to search for:"
read dir
find $HOME -name $dir -type d

这个解决方案很好,因为它允许使用通配符,在搜索文件/目录时非常有用。唯一的问题是,如果搜索的目录不存在,“find”命令将不会在标准输出中打印任何内容(对我来说并不是一个优雅的解决方案),但退出码仍然为零。也许有人可以对此进行改进。


13
如果一个程序不礼貌地搜索我整个硬盘以查找目录,而不是在我的当前工作目录中礼貌地查找或使用我提供的绝对路径,那我会感到受到冒犯。你所建议的可能适用于名为“locate”的工具,但对其他任何东西都不好用。 - sarnold

7
以下的find可以被使用,
find . -type d -name dirname -prune -print

6

(1)

[ -d Piyush_Drv1 ] && echo ""Exists"" || echo "Not Exists"

(2)

[ `find . -type d -name Piyush_Drv1 -print | wc -l` -eq 1 ] && echo Exists || echo "Not Exists"

(3)

[[ -d run_dir  && ! -L run_dir ]] && echo Exists || echo "Not Exists"

如果发现上述任何一种方法存在问题:
使用ls命令时,当目录不存在时会显示错误消息。
[[ `ls -ld SAMPLE_DIR| grep ^d | wc -l` -eq 1 ]] && echo exists || not exists

-ksh: not: 没有找到 [没有这个文件或目录]


“above” 是指什么?是指这个回答中的三条命令行还是之前的回答?(请通过编辑您的回答来回复,而不是在评论中回复。提前致谢。) - Peter Mortensen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接