开发者

How do I check if a directory has child directories?

开发者 https://www.devze.com 2023-03-22 05:36 出处:网络
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 \"..\"?

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 ".."?

开发者_C百科Thanks, - Dave


As the comments have pointed out, things have changed in the last 9 years! The dot dirs are no longer returned as part of find and instead the directory specified in the find command is.

So, if you want to stay with this approach:

#!/bin/bash
subdircount=$(find /tmp/test -maxdepth 1 -type d | wc -l)

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

(originally accepted answer from 2011)

#!/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


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 ls returns error-status 2 and prints the message ls: cannot access <dir>/*/: No such file or directory. The 2>&1 captures stderr and pipes it into stdout and then the whole lot gets piped to null (which gets rid of the regular ls output too, when there is files).


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

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

If you want to script it:

find $DIR/* -type d

should do the trick.


Try this as the condition you test against:

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

subdirs will be empty if there are no subdirectories


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.

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


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

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


I'd like to know if a directory has any subdirectories. For this purpose I don't need recursion, and I don't care what they are. So:

(find -L . -mindepth 1 -maxdepth 1 -type d | wc -l)


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.

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


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

function hasDirs ()
{
    declare $targetDir="$1"    # 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
0

精彩评论

暂无评论...
验证码 换一张
取 消