Differences between revisions 9 and 32 (spanning 23 versions)
Revision 9 as of 2008-06-23 15:25:07
Size: 2596
Editor: chello062178030209
Comment: Added example showing the use of find's -empty option
Revision 32 as of 2011-12-13 10:17:00
Size: 4441
Editor: Lhunath
Comment: simpler.
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? How do I check for any *.mpg files? ==
 ''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, or count how many there are? ==
In Bash, you can do this safely and easily with the `nullglob` and `dotglob` options (which change the behaviour of [[glob|globbing]]), and an [[BashFAQ/005|array]]:
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
}}}

Bear in mind that if you don't have read permission to the current directory, that will appear as being an empty directory with that solution.

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

{{{
    # Bash
    if (shopt -s nullglob dotglob; f=(*); ((! ${#f[@]}))); then
        echo "The current directory is empty."
Line 17: Line 24:
This can be shortened to: You can also avoid the `nullglob` if you're OK with putting a non-existing filename in the array should no files match (instead of an empty array):
Line 20: Line 27:
    if [ "$(ls -A somedir)" ]
    then
        echo directory is non-empty
    # Bash
    files=(*)
    if [[ -e $files ]]; then
        echo "The current directory is empty. It contains:"
        printf '%s\n' "${files[@]}"
Line 26: Line 35:
Another way, using Bash features, involves setting a special shell option which changes the behavior of [:glob:globbing]. Some people prefer to avoid this approach, because it's so drastically different and could severely alter the behavior of scripts. Of course, you can use any glob you like instead of `*`. E.g. `*.mpg` or `/my/music/*.mpg` works fine.
Line 28: Line 37:
Nevertheless, if you're willing to use this approach, it does greatly simplify this particular task: Both of these examples expand a glob and store the resulting filenames into an [[BashFAQ/005|array]], and then check whether the number of elements in the array is 0. If you actually want to ''see'' how many files there are, just print the array's size instead of checking whether it's 0:
Line 31: Line 40:
    shopt -s nullglob
    if [[ -z $(echo *) ]]; then
     echo directory is empty
    f
i
    
shopt -u nullglob
    # Bash
shopt -s nullglob dotglob
    f=(*)
    echo "The current directory contains ${#f[@]} things."
Line 38: Line 46:
This can also be combined with Bash's [:BashFAQ/005:arrays]. The major advantage here is that you probably wanted to ''do'' something with all the files, so having them loaded into an array is something that will help you with the overall task: Some people dislike `nullglob` because having unmatched globs vanish altogether confuses programs like `ls`. Mistyping `ls *.zip` as `ls *.zpi` may cause every file to be displayed. Setting `nullglob` in a SubShell avoids accidentally changing its setting in the rest of the shell, at the price of an extra `fork()`.

If your script needs to run with various non-Bash shell implementations, you can try using an external program like python, perl, or find; or you can try one of these:
Line 41: Line 51:
    shopt -s nullglob
    files=(*)
    (( ${#files[*]} )) || echo directory is empty
    shopt -u nullglob
    # POSIX
    # Clobbers the positional parameters, so make sure you don't need them.
    set -- *
    if test -e "$1" || test -L "$1"; then
        echo "directory is non-empty"
    fi
Line 47: Line 59:
`nullglob` also simplifies various other operations: (The `-L` test is required because `-e` fails if the first file is a [[BashFAQ/097|dangling symlink]].)
Line 50: Line 62:
    shopt -s nullglob
    for i in *.zip; do
        blah blah "$i" # No need to check $i is a file.
    done
    shopt -u nullglob
    # Bourne
    # (Of course, the system must have printf(1).)
    if test "`printf '%s %s %s' .* *`" = '. .. *' && test ! -f '*'
    then
        echo "directory is empty"
    fi
Line 57: Line 70:
Without the {{{nullglob}}}, that would have to be: Yes, they're quite ugly, but they should be more portable than anything depending on [[ParsingLs|ls output]]. Even `ls -A` solutions can break (e.g. on HP-UX, if you are root, `ls -A` does the exact ''opposite'' of what it does if you're not root -- and no, I can't make up something that incredibly stupid).

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 75:
    for i in *.zip; do
        [[ -f $i ]] || continue # If no .zip files, i becomes *.zip
        blah blah "$i"
   # 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
}}}

Most commonly, all that's really needed is something like this:

{{{
    # Bourne
    for f in ./*.mpg; do
        test -f "$f" || continue
        mympgviewer "$f"
Line 66: Line 91:
(You may want to use the latter anyway, if there's a possibility that the glob may match directories in addition to files.) In other words, the person asking the question may have ''thought'' an explicit empty-directory test was needed to avoid an error message like `mympgviewer: ./*.mpg: No such file or directory` when in fact no such test is required.
Line 68: Line 93:
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 appropriate solutions:
Support for a nullglob-like feature is inconsistent. In ksh93 it can be done on a per-pattern basis by prefixing with ~(N)<<FootNote(From: [[http://permalink.gmane.org/gmane.comp.standards.posix.austin.general/2058]], which contains some good discussion.)>>:
Line 71: Line 95:
   find "$somedir" -type d -empty -exec echo "$somedir is empty."
   find "$somedir" -type f -exec echo Found unexpected file {} \;
    # ksh93
    for f in ~(N)*; do
        ....
    done
Line 75: Line 101:
It's all a matter of addressing the program's actual requirements. ----
CategoryShell

How can I check whether a directory is empty or not? How do I check for any *.mpg files, or count how many there are?

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

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

Bear in mind that if you don't have read permission to the current directory, that will appear as being an empty directory with that solution.

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

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

You can also avoid the nullglob if you're OK with putting a non-existing filename in the array should no files match (instead of an empty array):

    # Bash
    files=(*)
    if [[ -e $files ]]; then
        echo "The current directory is empty.  It contains:"
        printf '%s\n' "${files[@]}"
    fi

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

Both of these examples expand a glob and store the resulting filenames into an array, and then check whether the number of elements in the array is 0. If you actually want to see how many files there are, just print the array's size instead of checking whether it's 0:

    # Bash
    shopt -s nullglob dotglob
    f=(*)
    echo "The current directory contains ${#f[@]} things."

Some people dislike nullglob because having unmatched globs vanish altogether confuses programs like ls. Mistyping ls *.zip as ls *.zpi may cause every file to be displayed. Setting nullglob in a SubShell avoids accidentally changing its setting in the rest of the shell, at the price of an extra fork().

If your script needs to run with various non-Bash shell implementations, you can try using an external program like python, perl, or find; or you can try one of these:

    # POSIX
    # Clobbers the positional parameters, so make sure you don't need them.
    set -- *
    if test -e "$1" || test -L "$1"; then
        echo "directory is non-empty"
    fi

(The -L test is required because -e fails if the first file is a dangling symlink.)

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

Yes, they're quite ugly, but they should be more portable than anything depending on ls output. Even ls -A solutions can break (e.g. on HP-UX, if you are root, ls -A does the exact opposite of what it does if you're not root -- and no, I can't make up something that incredibly stupid).

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

Most commonly, all that's really needed is something like this:

    # Bourne
    for f in ./*.mpg; do
        test -f "$f" || continue
        mympgviewer "$f"
    done

In other words, the person asking the question may have thought an explicit empty-directory test was needed to avoid an error message like mympgviewer: ./*.mpg: No such file or directory when in fact no such test is required.

Support for a nullglob-like feature is inconsistent. In ksh93 it can be done on a per-pattern basis by prefixing with ~(N)1:

    # ksh93
    for f in ~(N)*; do
        ....
    done


CategoryShell

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