Linux or Unix shell 判断路径是否存在

检查Linux or Unix shell中是否存在一个目录

目录只不过是在Linux系统中以层次格式存储文件的位置。例如,$HOME/Downloads/ 将存储所有下载的文件,/tmp/将存储临时文件。这个页面展示了如何查看一个目录是否存在于Linux或类unix系统中。


Linux下如何查看目录是否存在

可以使用以下语法检查Linux shell脚本中是否存在一个目录:

[ -d "/path/dir/" ] && echo "Directory /path/dir/ exists."

你可以用 ! 检查一个目录在Unix上是否不存在:

[ ! -d "/dir1/" ] && echo "Directory /dir1/ DOES NOT exists."

可以按照下面的方法检查Linux脚本中是否存在一个目录:

DIR="/etc/httpd/"
if [ -d "$DIR" ]; then
  # 如果 $DIR 存在,请执行操作. #
  echo "Installing config files in ${DIR}..."
fi

OR

DIR="/etc/httpd/"
if [ -d "$DIR" ]; then
  ### 如果 $DIR 存在,请执行操作 ###
  echo "Installing config files in ${DIR}..."
else
  ###  如果 $DIR 不存在,Control 将跳转到这里 ###
  echo "Error: ${DIR} not found. Can not continue."
  exit 1
fi

通常将“$DIR”括在双引号中,以处理名称中有白/黑空格的目录。例如:

DIR="/home/project/Data Files/"
if [ -d "$DIR" ]; then
   echo "'$DIR' found and now copying files, please wait ..."
else
   echo "Warning: '$DIR' NOT found."
fi

关于符号链接的注释(symlink)

当目录带有符号链接(symbolic links)时,需要使用-L开关(-L switch)对目录进行如下特殊考虑:

DEST="/aws/efs/shared/nixcraft.com/"
if [ -d "$DEST" ]; then 
  if [ -L "$DEST" ]; then
    # It is a symbolic links #
    echo "Symbolic link found and doing something on it ..."
  else
    # It is a directory #
    echo "Directory found and doing nothing here ..."
  fi
fi

Linux检查目录是否存在并执行一些操作

下面是一个示例shell脚本,它可以查看一个文件夹在 Linux 中是否存在:

#!/bin/bash
d="$1"
 
[ "$d" == "" ] && { echo "Usage: $0 directory"; exit 1; }
[ -d "${d}" ] &&  echo "Directory $d found." || echo "Directory $d not found."

按如下方式运行它:

./test.sh
./test.sh /tmp/
./test.sh /nixCraft

检查bash中是否存在该目录,如果没有,则创建它

下面是一个示例shell脚本,用于检查目录是否存在,并根据需要创建它:

#!/bin/bash
dldir="$HOME/linux/5.x"
_out="/tmp/out.$$"
 
# Build urls
url="some_url/file.tar.gz"
file="${url##*/}"
 
### Check for dir, if not found create it using the mkdir ##
[ ! -d "$dldir" ] && mkdir -p "$dldir"
 
# Now download it
wget -qc "$url" -O "${dldir}/${file}"
 
# Do something else below #

确保你总是将shell变量如 $DIR 括在双引号中( "$DIR" 以避免shell脚本中的任何意外:

DIR="foo"
[ -d "$DIR" ] && echo "Found"
##
## this will fail as DIR will only expand to "foo" and not to "foo bar stuff" 
## hence wrap it 
##
DIR="foo bar stuff"
[ -d $DIR ] && echo "Found"

使用测试命令

可以使用test命令检查文件类型并比较值。例如,查看FILE是否存在并且是一个目录。语法:

test -d "DIRECTORY" && echo "Found/Exists" || echo "Does not exist"

test命令与[条件表达式]相同。因此,您也可以使用下面的语法:

[ -d "DIR" ] && echo "yes" || echo "noop"

寻求帮助

通过输入下面的man命令或在线访问这里来阅读bash shell手册页:

man bash
help [
help [[
man test

结论

本页解释了在Linux或类unix系统上运行的shell脚本中,可以用来检查目录是否存在的各种命令。如果DIR1存在并且是一个目录, -d DIR1 选项返回true。类似地,如果发现 DIR1 并且是一个符号链接symbolic links), -L DIR1 选项将返回true。


  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小泉映月

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值