题
什么样的命令可用于检查,如果一个目录是否存在或不在壳的剧本吗?
解决方案
到检查,如果一个目录存在一个脚本可以使用如下:
if [ -d "$DIRECTORY" ]; then
# Control will enter here if $DIRECTORY exists.
fi
或检查,如果一个目录并不存在:
if [ ! -d "$DIRECTORY" ]; then
# Control will enter here if $DIRECTORY doesn't exist.
fi
然而,作为 乔恩*埃里克森 所指出的,随后命令不可以工作为目的,如果你不考虑一个象征性的链接目录还将通过这种检查。E.g。在运行这样的:
ln -s "$ACTUAL_DIR" "$SYMLINK"
if [ -d "$SYMLINK" ]; then
rmdir "$SYMLINK"
fi
会产生错误的信息:
rmdir: failed to remove `symlink': Not a directory
所以象征性的联系,可能有区别对待,如果随后的命令预计目录:
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
特别注意到双报价用于包裹的变量,因为这是解释的8jean 在另一个答案.
如果的变量包含空间或其他不寻常的人物就可能会导致脚本到失败。
其他提示
记住,总是包裹的变量在双引号的时引用它们在 bash脚本。这些孩子天生长的想法,他们可以有空间和很多其它有趣的人物在他们的目录名称。(空间!回到我的天,我们没有没有花哨的空间!;))
有一天,那些孩子将运行你的脚本 $DIRECTORY
设置 "My M0viez"
和你的剧本会炸毁。你不希望出现这种情况。因此,使用这个。
if [ -d "$DIRECTORY" ]; then
# Will enter here if $DIRECTORY exists, even if it contains spaces
fi
我找到的 双架 版本的 test
使编写的逻辑测试更自然的:
if [[ -d "${DIRECTORY}" && ! -L "${DIRECTORY}" ]] ; then
echo "It's a bona-fide directory"
fi
较短的形式:
[ -d "$DIR" ] && echo "Yes"
到检查,如果一个目录是否存在可以使用简单的如果结构是这样的:
if [ -d directory/path to a directory ] ; then
#Things to do
else #if needed #also: elif [new condition]
# things to do
fi
您可以做它还负
if [ ! -d directory/path to a directory ] ; then
# things to do when not an existing directory
注意到:要小心,留空的空间,在两边都打开和关闭括号。
有相同的语法可以使用:
-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
if [ -d "$DIRECTORY" ]; then
# Here if $DIRECTORY exists
fi
你可以使用 test -d
(见 man test
).
-d file
真的,如果文件存在并且是一个目录。
例如:
test -d "/etc" && echo Exists || echo Does not exist
注:的 test
命令是相同的条件式 [
(参见: man [
),因此它是便携式的跨壳脚本。
[
-这是一个同义词test
内,但最后的参数必须是一个文字]
, 配合开口[
.
对于可能的选择或进一步的帮助,检查:
help [
help test
man test
或man [
一个简单的脚本中测试,如果dir或文件是存在或者不:
if [ -d /home/ram/dir ] # for file "if [-f /home/rama/file]" then echo "dir present" else echo "dir not present" fi
一个简单的脚本来检查是否该目录本或不:
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
上述脚本将检查dir是存在或者不
$?
如果最后的命令的成功返回"0"其他非零值。假设tempdir
已经是本然后mkdir tempdir
将会得到错误,如下:mkdir:无法创建directory'tempdir':文件的存在
或者什么东西完全没有用的:
[ -d . ] || echo "No"
这是一个非常务实的成语:
(cd $dir) || return # is this a directory,
# and do we have access?
我通常包装在一个功能:
can_use_as_dir() {
(cd ${1:?pathname expected}) || return
}
或者:
assert_dir_access() {
(cd ${1:?pathname expected}) || exit
}
好的事情关于这种方法是,我没有想到一个很好的错误信息。
cd
会给我一个标准的一线消息来stderr。它还将提供更多的信息比我将能够提供的。通过执行 cd
里面有子shell ( ... )
, ,该命令不会影响目前的目录。如果目录存在,这种子shell和功能仅仅是没有任择议定书》。
下一个论点是,我们通过 cd
: ${1:?pathname expected}
.这是一个更复杂形式的参数取代,这是更详细地解释如下。
Tl博士:如果串通过了这个功能是空的,我们再次离子shell ( ... )
和返回自的功能与给的错误消息。
引述 ksh93
man page:
${parameter:?word}
如果
parameter
设置并非空,然后代替它的价值;否则,印word
出外壳(如果不是交互式).如果word
省略了,然后一个标准的消息是印刷。
和
如果结肠
:
省略从上面的表达,那么 壳只检查是否参数设置或没有。
该措辞在这里就是奇特的外壳的文件,作为 word
可以参考
任何合理的串,包括空白。
在这种特定情况下,我知道,标准的错误信息 1: parameter not set
是不足够,所以我放大的类型的价值,我们期望在这里-对 pathname
一个目录。
一philosphical注意:壳不是面向对象的语言,因此消息说 pathname
, ,不 directory
.在这个水平,我宁愿保持它的简单的参数的一个函数都是串。
if [ -d "$Directory" -a -w "$Directory" ]
then
#Statements
fi
上述代码的检查,如果目录存在,并且如果可写的。
输入这个代码的应用程序bash
if [ -d "$DIRECTORY" ]; then
# if true this block of code will execute
fi
更多的功能,使用 find
检查存在内的文件夹子目录:
found=`find -type d -name "myDirectory"` if [ -n "$found"] then # The variable 'found' contains the full path where "myDirectory" is. # It may contain several lines if there are several folders named "myDirectory". fi
检查存在的一个或几个文件夹基于一种模式在当前的目录:
found=`find -maxdepth 1 -type d -name "my*"` if [ -n "$found"] then # The variable 'found' contains the full path where folders "my*" have been found. fi
两者的组合。在下面的例子中,检查存在的文件夹在当前的目录:
found=`find -maxdepth 1 -type d -name "myDirectory"` if [ -n "$found"] then # The variable 'found' is not empty => "myDirectory"` exists. fi
实际上,你应该使用多种工具以获得防弹的方法:
DIR_PATH=`readlink -f "${the_stuff_you_test}"` # Get rid of symlinks and get abs path
if [[ -d "${DIR_PATH}" ]] ; Then # now you're testing
echo "It's a dir";
fi
没有必要担心关于空间和特殊字符只要你使用 "${}"
.
注意, [[]]
不如便携式作 []
, 但由于大多数人的工作与现代版本的庆典(因为毕竟,大多数人甚至不起作用命令行:-p),获益大于麻烦。
来检查多个目录使用这个代码:
if [ -d "$DIRECTORY1" ] && [ -d "$DIRECTORY2" ] then
# Things to do
fi
你有没有考虑只是在做任何你想要做的 if
而不是之前你飞跃?
即,如果你想检查是否存在的一个目录之前输入它,试试只是在做这样的:
if pushd /path/you/want/to/enter; then
# commands you want to run in this directory
popd
fi
如果你给的路径 pushd
存在,只进入它,它就会退出 0
, 这意味着 then
部的声明执行。如果不存在,什么都不会发生(比其他一些输出说的目录不存在,这可能是一个有用的副作用无论如何,为调试)。
似乎比这更好的,这就需要重复自己:
if [ -d /path/you/want/to/enter ]; then
pushd /path/you/want/to/enter
# commands you want to run in this directory
popd
fi
同样的事情作品 cd
, mv
, rm
, 等等...如果你试试他们的文件上,不存在,他们将退出一个错误和打印一个消息,说它不存在,和你 then
框会被跳过。如果你试试他们的文件上,不存在,该命令将执行和退出带状态 0
, ,允许你 then
框到执行。
如果检查目录存在,使一个人
[ -d "$DIRECTORY" ] || mkdir $DIRECTORY
[[ -d "$DIR" && ! -L "$DIR" ]] && echo "It's a directory and not a symbolic link"
注:引用变量是一个很好的做法。
[ -d ~/Desktop/TEMPORAL/ ] && echo "DIRECTORY EXISTS" || echo "DIRECTORY DOES NOT EXIST"
这个答案 包裹起来,作为一个脚本
例
$ 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";
使用 -e
检查将检查的文件和这包括目录。
if [ -e ${FILE_PATH_AND_NAME} ]
then
echo "The file or directory exists."
fi
为每 乔纳森 评论:
如果你想要建立目录,它尚不存在,则最简单的技术是使用 mkdir -p
它创建的目录和任何缺失的目录中的路径并不会失败如果目录已经存在,所以你可以做所有的一次有:
mkdir -p /some/directory/you/want/to/exist || exit 1
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;
如果你想检查,如果一个目录存在,无论如何,如果它是一个真正的目录或一种链接,使用这样的:
ls $DIR
if [ $? != 0 ]; then
echo "Directory $DIR already exists!"
exit 1;
fi
echo "Directory $DIR does not exist..."
说明:"Ls"命令提供了一个错误"ls:/x:没有这样的文件或录"如果目录或symlink不存在,也将返回代码,可以通过检索"$?", 非null(通常"1").可以肯定,你检查返回代码之后直接呼吁"ls"。
(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
-eq1]]&&echo存在||不存在
-股:不:找不到[没有这样的文件或录]
伟大的解决方案的存在,但是最终,每个脚本将失败如果你不在的权利目录。这样的代码这样的:
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
这种解决方案是良好的,因为它允许使用通配符的、有用的特征时,搜索文件/目录。唯一的问题是,如果所搜索目录不存在的'发现'的命令将打印什么stdout(没有一个优雅的解决方案,为我的味道),并将有仍然有零退出。也许有人可以改善这一点。
下面找到可以使用,
find . -type d -name dirname -prune -print