在Bash shell脚本中,哪个命令可以检查目录是否存在?
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
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";
mkdir -p
。它可以创建目录和路径上缺失的任何目录,并且如果目录已经存在,它也不会失败,因此您可以一次完成所有操作:mkdir -p /some/directory/you/want/to/exist || exit 1
[[ -d $Directory ]] && echo true
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
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
file="foo"
if [[ -e "$file" ]]; then echo "File Exists"; fi;
虽然有很好的解决方案,但如果您不在正确的目录中,最终每个脚本都会失败。因此,像这样的代码:
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
dir=" "
echo "Input directory name to search for:"
read dir
find $HOME -name $dir -type d
这个解决方案很好,因为它允许使用通配符,在搜索文件/目录时非常有用。唯一的问题是,如果搜索的目录不存在,“find”命令将不会在标准输出中打印任何内容(对我来说并不是一个优雅的解决方案),但退出码仍然为零。也许有人可以对此进行改进。
find
可以被使用,find . -type d -name dirname -prune -print
(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: 没有找到 [没有这个文件或目录]