Differences between revisions 32 and 36 (spanning 4 versions)
Revision 32 as of 2011-12-13 10:17:00
Size: 4441
Editor: Lhunath
Comment: simpler.
Revision 36 as of 2015-03-05 00:20:13
Size: 5228
Editor: izabera
Comment: syntax hl
Deletions are marked like this. Additions are marked like this.
Line 5: Line 5:
{{{
    # Bash
    shopt -s nullglob dotglob
    files=(*)
    (( ${#files[*]} )) || echo directory is empty
    shopt -u nullglob dotglob
{{{#!highlight bash
# Bash
shopt -s nullglob dotglob
files=(*)
(( ${#files[*]} )) || echo directory is empty
shopt -u nullglob dotglob
Line 13: Line 13:
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. Of course, you can use any glob you like instead of `*`. E.g. `*.mpg` or `/my/music/*.mpg` works fine.
Line 15: Line 15:
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: Bear in mind that you need [[Permissions|read permission]] on the directory, or it will always appear empty.
Line 17: Line 17:
{{{
    # Bash
    if (shopt -s nullglob dotglob; f=(*); ((! ${#f[@]}))); then
        echo "The current directory is empty."
    fi
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 (for such cases consider setting `failglob`). Setting `nullglob` in a SubShell avoids accidentally changing its setting in the rest of the shell, at the price of an extra `fork()`. If you'd like to avoid having to set and unset shell options, you can pour it all into a SubShell:

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

The other disadvantage of this approach (besides the extra `fork()`) is that the array is lost when the subshell exits. If you planned to ''use'' those filenames later, then they have to be retrieved all over again.

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:

{{{#!highlight bash
# Bash
shopt -s nullglob dotglob
files=(*)
echo "The current directory contains ${#files[@]} things."
Line 26: Line 39:
{{{
    # Bash
    files=(*)
    if [[ -e $files ]]; then
     echo "The current directory is empty. It contains:"
     printf '%s\n' "${files[@]}"
    fi
{{{#!highlight bash
# Bash
shopt -s dotglob
files=(*)
if [[ -e ${files[0]} || -L ${files[0]} ]]; then
    echo "The current directory is not empty. It contains:"
    printf '%s\n' "${files[@]}"
fi
Line 35: Line 49:
Of course, you can use any glob you like instead of `*`. E.g. `*.mpg` or `/my/music/*.mpg` works fine. Without `nullglob`, if there are no files in the directory, the glob will be added as the only element in the array. Since `*` is a valid filename, we can't simply check whether the array contains a literal `*`. So instead, we check whether the thing in the array ''exists'' as a file. The `-L` test is required because `-e` fails if the first file is a [[BashFAQ/097|dangling symlink]].
Line 37: Line 51:
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: If your script needs to run with various non-Bash shell implementations, you can try using an external program like python, perl, or [[UsingFind|find]]; or you can try one of these:
Line 39: Line 53:
{{{
    # Bash
    shopt -s nullglob dotglob
    f=(*)
    echo "The current directory contains ${#f[@]} things."
{{{#!highlight bash
# 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 46: Line 62:
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()`. At this stage, the positional parameters have been loaded with the contents of the directory, and can be used for processing.
Line 48: Line 64:
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: In the Bourne shell, it's even worse, because there is no `test -e` or `test -L`:
Line 50: Line 66:
{{{
    # 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
{{{#!highlight bash
# Bourne
# (Of course, the system must have printf(1).)
if test "`printf '%s %s %s' .* *`" = '. .. *' && test ! -f '*'
then
    echo "directory is empty"
fi
Line 59: Line 75:
(The `-L` test is required because `-e` fails if the first file is a [[BashFAQ/097|dangling symlink]].) Of course, that fails if `*` exists as something other than a plain file (such as a directory or FIFO). The absence of a `-e` test really hurts.
Line 61: Line 77:
{{{
    # Bourne
    # (Of course, the system must have printf(1).)
    if test "`printf '%s %s %s' .* *`" = '. .. *' && test ! -f '*'
    then
        echo "directory is empty"
    fi
}}}
Never try to [[ParsingLs|parse 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).
Line 70: Line 79:
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, one 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 72: Line 81:
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:

{{{
   # 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
{{{#!highlight bash
# Bo
urne
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 83: Line 90:
{{{
    # Bourne
    for f in ./*.mpg; do
     test -f "$f" || continue
        mympgviewer "$f"
    done
{{{#!highlight bash
# Bourne
for f in ./*.mpg; do
    test -f "$f" || continue
    mympgviewer "$f"
done
Line 94: Line 101:
{{{
    # ksh93
    for f in ~(N)*; do
        ....
    done
{{{#!highlight bash
# ksh93
for f in ~(N)*; do
    ....
done

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:

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

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

Bear in mind that you need read permission on the directory, or it will always appear empty.

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 (for such cases consider setting failglob). Setting nullglob in a SubShell avoids accidentally changing its setting in the rest of the shell, at the price of an extra fork(). If you'd like to avoid having to set and unset shell options, you can pour it all into a SubShell:

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

The other disadvantage of this approach (besides the extra fork()) is that the array is lost when the subshell exits. If you planned to use those filenames later, then they have to be retrieved all over again.

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:

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

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):

   1 # Bash
   2 shopt -s dotglob
   3 files=(*)
   4 if [[ -e ${files[0]} || -L ${files[0]} ]]; then
   5     echo "The current directory is not empty.  It contains:"
   6     printf '%s\n' "${files[@]}"
   7 fi

Without nullglob, if there are no files in the directory, the glob will be added as the only element in the array. Since * is a valid filename, we can't simply check whether the array contains a literal *. So instead, we check whether the thing in the array exists as a file. The -L test is required because -e fails if the first file is a dangling symlink.

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:

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

At this stage, the positional parameters have been loaded with the contents of the directory, and can be used for processing.

In the Bourne shell, it's even worse, because there is no test -e or test -L:

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

Of course, that fails if * exists as something other than a plain file (such as a directory or FIFO). The absence of a -e test really hurts.

Never try to parse 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, one 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:

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

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

   1 # Bourne
   2 for f in ./*.mpg; do
   3     test -f "$f" || continue
   4     mympgviewer "$f"
   5 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:

   1 # ksh93
   2 for f in ~(N)*; do
   3     ....
   4 done


CategoryShell

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