bash 如何检查目录是否有子目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6781225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:25:00  来源:igfitidea点击:

How do I check if a directory has child directories?

bashunixscripting

提问by Dave

Using bash, how do I write an if statement that checks if a certain directory, stored in the a script variable named "$DIR", contains child directories that are not "." or ".."?

使用 bash,我如何编写 if 语句来检查存储在名为“$DIR”的脚本变量中的某个目录是否包含不是“.”的子目录。或者 ”..”?

Thanks, - Dave

谢谢, - 戴夫

回答by AlG

Here's one way:

这是一种方法:

#!/usr/bin/bash
subdircount=`find /d/temp/ -maxdepth 1 -type d | wc -l`

if [ $subdircount -eq 2 ]
then
    echo "none of interest"
else
    echo "something is in there"
fi

回答by robert

Here's a more minimalist solution that will perform the test in a single line..

这是一个更简约的解决方案,它将在一行中执行测试..

ls $DIR/*/ >/dev/null 2>&1 ; 

if [ $? == 0 ]; 
then 
  echo Subdirs
else 
  echo No-subdirs
fi

By putting /after the *wildcard you select only directories, so if there is no directories then lsreturns error-status 2and prints the message ls: cannot access <dir>/*/: No such file or directory. The 2>&1captures stderrand pipes it into stdoutand then the whole lot gets piped to null(which gets rid of the regular ls output too, when there is files).

通过将/*通配符只选择目录,因此如果没有目录,那么ls将返回错误状态2和打印消息ls: cannot access <dir>/*/: No such file or directory。在2>&1捕获标准错误和管道入标准输出,然后一大堆得到管道输送到(这摆脱了常规的LS输出过,当有文件)。

回答by sbtkd85

I'm not really sure what you trying to do here, but you can use find:

我不太确定你想在这里做什么,但你可以使用find

find /path/to/root/directory -type d

If you want to script it:

如果要编写脚本:

find $DIR/* -type d

should do the trick.

应该做的伎俩。

回答by jfg956

A solution in pure bash, without needing any other program execution. This is not the most compact solution, but if run in a loop, it could be more efficient because no process creation is needed. If there is a lot of files in '$dir', the filename expansion could break though.

纯 bash 中的解决方案,无需任何其他程序执行。这不是最紧凑的解决方案,但如果在循环中运行,它可能会更高效,因为不需要创建进程。如果 中有很多文件'$dir',文件名扩展可能会中断。

shopt -s dotglob   # To include directories beginning by '.' in file expansion.
nbdir=0
for f in $dir/*
do
  if [ -d $f ]
  then
    nbdir=$((nbdir+1))
  fi
done

if [ nbdir -gt 0 ]
then
   echo "Subdirs"
else
   echo "No-Subdirs"
fi

回答by ?ukasz Starowicz

in my case it doesn't work as @AIG wrote - for empty dir I got subdircount=1 (find returns only dir itself).

在我的情况下,它不像@AIG 写的那样工作 - 对于空目录,我得到 subdircount=1 (find 只返回目录本身)。

what works for me:

什么对我有用:

#!/usr/bin/bash
subdircount=`find /d/temp/ -maxdepth 1 -type d | wc -l`
if [ $subdircount -ge 2 ]
then
    echo "not empty"
else
    echo "empty"
fi

回答by ennuikiller

Try this as the condition you test against:

试试这个作为你测试的条件:

subdirs=$(ls  -d $DIR/.*/ | grep -v "/./\|/../")

subdirs will be empty if there are no subdirectories

如果没有子目录,则 subdirs 将为空

回答by jman

How about:

怎么样:

num_child=`ls -al $DIR | grep -c -v ^d`

If $num_child > 2, then you have child directories. If you do not want hidden directories, replace ls -al with ls -l.

如果 $num_child > 2,则您有子目录。如果您不想隐藏目录,请将 ls -al 替换为 ls -l。

if [ $num_child -gt 2 ]
then
    echo "$num_child child directories!"
fi

回答by Anthony Rutledge

Here is the best answer you will ever see on this issue.

这是您将在此问题上看到的最佳答案。

function hasDirs ()
{
    declare $targetDir=""    # Where targetDir ends in a forward slash /
    ls -ld ${targetDir}*/
}

If you are already in the target directory:

如果您已经在目标目录中:

if hasDirs ./
then

fi

If you want to know about another directory:

如果您想了解另一个目录:

if hasDirs /var/local/   # Notice the ending slash
then

fi