Differences between revisions 1 and 23 (spanning 22 versions)
Revision 1 as of 2007-05-02 22:47:44
Size: 2071
Editor: redondos
Comment:
Revision 23 as of 2009-07-20 18:09:15
Size: 2700
Editor: localhost
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
[[Anchor(faq4)]]
== How can I check whether a directory is empty or not? ==
 ''I just deleted three completely '''wrong''' answers from this question. Please, people, make sure that when you add to the FAQ, your answers''
  * answer the question that was asked, and
  * actually '''work'''
 ''Thanks. -- GreyCat''

Most modern systems have an "ls -A" which explicitly omits "." and ".." from the directory listing:
<<Anchor(faq4)>>
== How can I check whether a directory is empty or not? How do I check for any *.mpg files? ==
In Bash, you can do this safely and easily with the `nullglob` and `dotglob` options (which change the behaviour of [[glob|globbing]]), and [[BashFAQ/005|arrays]]:
Line 11: Line 6:
    if [ -n "$(ls -A somedir)" ]
    then
        echo directory is non-empty
    # Bash
    shopt -s nullglob dotglob
    files=(*)
    (( ${#files[*]} )) || echo directory is empty
    shopt -u nullglob dotglob
}}}

Or you can pour it into an `if` statement with a subshell to avoid having to unset (in fact, reset! - the code above assumes the shell options were unset before) the shell options again:

{{{
    if (shopt -s nullglob dotglob; f=(*); ((! ${#f[@]}))); then
        echo "The current directory is empty."
Line 17: Line 21:
This can be shortened to: Of course, you can use any glob you like instead of `*`. E.g. `*.mpg` or `/my/music/*.mpg` works fine.

As you can see we unset the nullglob after using it, to prevent it affecting other globs in the script in unexpected ways. `nullglob` also simplifies various other operations:
Line 20: Line 26:
    if [ "$(ls -A somedir)" ]
    then
        echo directory is non-empty
    fi
    # Bash
    shopt -s nullglob
    for i in *.zip; do
        blah blah "$i" # No need to check whether $i is a file.
    done
    shopt -u nullglob
Line 26: Line 34:
Another way, using Bash features, involves setting the special shell option which changes the behavior of globbing. Some people prefer to avoid this approach, because it's so drastically different and could severely alter the behavior of scripts.

Nevertheless, if you're willing to use this approach, it does greatly simplify this particular task:
Without the {{{nullglob}}}, that would have to be:
Line 31: Line 37:
    shopt -s nullglob
    if [[ -z $(echo *) ]]; then
        echo directory is empty
    fi
}}}

It also simplifies various other operations:

{{{
    shopt -s nullglob
    for i in *.zip; do
        blah blah "$i" # No need to check $i is a file.
    done
}}}

Without the {{{shopt}}}, that would have to be:

{{{
    # Bash
Line 57: Line 46:
Finally, you may wish to avoid the ''direct'' question altogether. Usually people want to know whether a directory is empty... ''because'' they want to do something involving the files therein, etc. Look to the larger question. For example, something like this may be an appropriate solution: In fact, you may wish to avoid the ''direct'' question altogether. Usually people want to know whether a directory is empty... ''because'' they want to do something involving the files therein, etc. Look to the larger question. For example, one of these [[UsingFind|find-based examples]] may be an appropriate solution:
Line 60: Line 49:
   find "$somedir" -type f -exec echo Found unexpected file {} in "$somedir" \;    # Bourne
   find "$somedir" -type f -exec echo Found unexpected file {} \;
   find "$somedir" -maxdepth 0 -empty -exec echo {} is empty. \; # GNU/BSD
   find "$somedir" -type d -empty -exec cp /my/configfile {} \; # GNU/BSD
Line 63: Line 55:
It's all a matter of addressing the program's actual requirements. If your script needs to run with various shell implementations, you can try using an external program like python, perl, or find as indicated above, or you can try something like:

{{{
    # Bourne
    # (Of course, the system must have printf(1).)
    cd foo || exit 1
    if test "`printf '%s %s %s' .* *`" = '. .. *' && test ! -f '*'
    then
        echo "directory is empty"
    fi
}}}

Yes, it's extremely ugly, but it should be more portable than anything depending on [[ParsingLs|ls output]]. Even `ls -A` solutions can break (HPUX for one, if you are root).

How can I check whether a directory is empty or not? How do I check for any *.mpg files?

In Bash, you can do this safely and easily with the nullglob and dotglob options (which change the behaviour of globbing), and arrays:

    # Bash
    shopt -s nullglob dotglob
    files=(*)
    (( ${#files[*]} )) || echo directory is empty
    shopt -u nullglob dotglob

Or you can pour it into an if statement with a subshell to avoid having to unset (in fact, reset! - the code above assumes the shell options were unset before) the shell options again:

    if (shopt -s nullglob dotglob; f=(*); ((! ${#f[@]}))); then
        echo "The current directory is empty."
    fi

Of course, you can use any glob you like instead of *. E.g. *.mpg or /my/music/*.mpg works fine.

As you can see we unset the nullglob after using it, to prevent it affecting other globs in the script in unexpected ways. nullglob also simplifies various other operations:

    # Bash
    shopt -s nullglob
    for i in *.zip; do
        blah blah "$i"  # No need to check whether $i is a file.
    done
    shopt -u nullglob

Without the nullglob, that would have to be:

    # Bash
    for i in *.zip; do
        [[ -f $i ]] || continue  # If no .zip files, i becomes *.zip
        blah blah "$i"
    done

(You may want to use the latter anyway, if there's a possibility that the glob may match directories in addition to files.)

In fact, you may wish to avoid the direct question altogether. Usually people want to know whether a directory is empty... because they want to do something involving the files therein, etc. Look to the larger question. For example, one of these find-based examples may be an appropriate solution:

   # Bourne
   find "$somedir" -type f -exec echo Found unexpected file {} \;
   find "$somedir" -maxdepth 0 -empty -exec echo {} is empty. \;  # GNU/BSD
   find "$somedir" -type d -empty -exec cp /my/configfile {} \;   # GNU/BSD

If your script needs to run with various shell implementations, you can try using an external program like python, perl, or find as indicated above, or you can try something like:

    # Bourne
    # (Of course, the system must have printf(1).)
    cd foo || exit 1
    if test "`printf '%s %s %s' .* *`" = '. .. *' && test ! -f '*'
    then
        echo "directory is empty"
    fi

Yes, it's extremely ugly, but it should be more portable than anything depending on ls output. Even ls -A solutions can break (HPUX for one, if you are root).

BashFAQ/004 (last edited 2023-03-28 07:52:15 by emanuele6)