Differences between revisions 36 and 56 (spanning 20 versions)
Revision 36 as of 2015-03-05 00:20:13
Size: 5228
Editor: izabera
Comment: syntax hl
Revision 56 as of 2025-01-30 04:47:30
Size: 8719
Editor: larryv
Comment: added extra whitespace for breathing room, standardized on 4-space indentation, made "highlight" languages more accurate
Deletions are marked like this. Additions are marked like this.
Line 3: Line 3:
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]]: In Bash, you can count files safely and easily with the `nullglob` and `dotglob` options (which change the behaviour of [[glob|globbing]]), and an [[BashFAQ/005|array]]:

{{{#!highlight bash
# Bash
shopt -s nullglob dotglob

files=(*)

if (( ${#files[*]} )); then
    echo 'directory is not empty'
else
    echo 'directory is empty'
fi

shopt -u nullglob dotglob
}}}

See ArithmeticExpression for explanations of arithmetic commands.

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 [[Permissions|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:

{{{#!highlight bash
# Bash
if (shopt -s nullglob dotglob; f=(*); ((${#f[@]}))); then
    echo 'directory is not empty'
else
    echo '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:
Line 9: Line 45:
(( ${#files[*]} )) || echo directory is empty
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 [[Permissions|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:

{{{#!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
echo "directory contains ${#files[@]} entries"
}}}

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

{{{#!highlight bash
# Bash
shopt -s dotglob
Line 34: Line 55:
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):

{{{#!highlight bash
# Bash
shopt -s dotglob
files=(*)
Line 44: Line 57:
    echo "The current directory is not empty. It contains:"
    printf '%s\n' "${files[@]}"
    echo "directory contains ${#files[@]} entries"
else
    echo 'directory is empty'
Line 51: Line 65:
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:

{{{#!highlight bash
If you don't care how many matching files there are and don't want to store the results in an array, you can use bash's `compgen` command. Unfortunately, due to a [[https://lists.gnu.org/archive/html/bug-bash/2023-03/msg00062.html|bug]], you need to use a hack to make it recognize `dotglob`:

{{{#!highlight bash
# Bash
if (shopt -s dotglob; : *; compgen -G '*' >/dev/null); then
    echo 'directory is not empty'
else
    echo 'directory is empty'
fi
}}}

Or you can use an [[glob#extglob|extended glob]]:

{{{#!highlight bash
# Bash
# The subshell may be avoided by enabling extglob for the whole script.
# Doing so should be safe.
if (shopt -s extglob; compgen -G '@(*|.[!.]*|..?*)' >/dev/null); then
    echo 'directory is not empty'
else
    echo 'directory is empty'
fi
}}}

You may also use `failglob`:

{{{#!highlight bash
# Bash
if (shopt -s dotglob failglob; : ./*) 2>/dev/null; then
    echo 'directory is not empty'
else
    echo 'directory is empty'
fi
}}}

But, if you use `failglob`, note that the subshell is required; the following code does not work because failglob will raise a shell error that will cause bash to stop running the current command (including the `if` command, any outer compound command, and the entire function that ran this code if it is part of a function), so this will only work in the true case, the `else` branch will never run:
{{{#!highlight bash
# BROKEN!
shopt -s dotglob failglob

if { : ./* ;} 2> /dev/null; then
    echo 'directory is not empty'
else
    echo 'directory is empty'
fi
}}}

If you really want to avoid using the subshell and want to set failglob globally, you can either "catch" the shell error using `command eval`, or you can write a function that expands the glob indirectly:
{{{#!highlight bash
shopt -s dotglob failglob

if command eval ': ./*' 2> /dev/null; then
    echo 'directory is not empty'
else
    echo 'directory is empty'
fi

# or

shopt -s dotglob failglob

any_match () {
    local IFS=
    { : $@ ;} 2> /dev/null
}

if any_match './*'; then
    echo 'directory is not empty'
else
    echo 'directory is empty'
fi
}}}

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. Note the "magic 3 globs"<<FootNote(https://www.etalabs.net/sh_tricks.html)>> as POSIX does not have the `dotglob` option.

{{{#!highlight sh
Line 56: Line 143:
set -- *
if test -e "$1" || test -L "$1"; then
    echo "directory is non-empty"
fi
}}}
set -- * .[!.]* ..?*

is_empty=1
for f in "$@"; do
    if test -e "$f" || test -L "$f"; then
        is_empty=
        break
    fi
done

if test "$is_empty"; then
    echo 'directory is empty'
else
    echo 'directory is not empty'
fi
}}}
Line 64: Line 161:
If you just want to count files:

{{{#!highlight sh
# POSIX
n=0
for f in * .[!.]* ..?*; do
    if test -e "$f" || test -L "$f"; then
        n=$((n+1))
    fi
done
printf 'directory contains %d entries\n' "$n"
}}}
Line 66: Line 176:
{{{#!highlight bash {{{#!highlight sh
Line 71: Line 181:
    echo "directory is empty"     echo 'directory is empty'
else
    echo 'directory is not empty'
Line 77: Line 189:
Here is another solution [[UsingFind|using find]]:

{{{#!highlight sh
# POSIX
# Print a single `.' for each file and count the number of characters printed.
# This one will recurse. If that is not desired, see below.
n=$(find . -type f -exec printf %.0s. {} + | wc -m)
printf 'directory contains %d files\n' "$n"
}}}

If you want it not to recurse, then you need to tell find not to recurse into directories. This gets really tricky and ugly. GNU find has a `-maxdepth` option to do it. With standard POSIX find, you're stuck with `-prune`. This is left as an exercise for the reader.
Line 81: Line 205:
{{{#!highlight bash
# Bourne
{{{#!highlight sh
# Bourne / POSIX
Line 84: Line 208:
find "$somedir" -maxdepth 0 -empty -exec echo {} is empty. \; # GNU/BSD find "$somedir" -prune -empty -exec printf '%s is empty\n' {} \; # GNU/BSD
Line 90: Line 214:
{{{#!highlight bash
# Bourne
{{{#!highlight sh
# Bourne / POSIX
Line 101: Line 225:
{{{#!highlight bash
{{{#!highlight ksh
Line 107: Line 232:

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 count files 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 
   4 files=(*)
   5 
   6 if (( ${#files[*]} )); then
   7     echo 'directory is not empty'
   8 else
   9     echo 'directory is empty'
  10 fi
  11 
  12 shopt -u nullglob dotglob

See ArithmeticExpression for explanations of arithmetic commands.

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 'directory is not empty'
   4 else
   5     echo 'directory is empty'
   6 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 "directory contains ${#files[@]} entries"

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 
   4 files=(*)
   5 
   6 if [[ -e ${files[0]} || -L ${files[0]} ]]; then
   7     echo "directory contains ${#files[@]} entries"
   8 else
   9     echo 'directory is empty'
  10 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 you don't care how many matching files there are and don't want to store the results in an array, you can use bash's compgen command. Unfortunately, due to a bug, you need to use a hack to make it recognize dotglob:

   1 # Bash
   2 if (shopt -s dotglob; : *; compgen -G '*' >/dev/null); then
   3     echo 'directory is not empty'
   4 else
   5     echo 'directory is empty'
   6 fi

Or you can use an extended glob:

   1 # Bash
   2 # The subshell may be avoided by enabling extglob for the whole script.
   3 # Doing so should be safe.
   4 if (shopt -s extglob; compgen -G '@(*|.[!.]*|..?*)' >/dev/null); then
   5     echo 'directory is not empty'
   6 else
   7     echo 'directory is empty'
   8 fi

You may also use failglob:

   1 # Bash
   2 if (shopt -s dotglob failglob; : ./*) 2>/dev/null; then
   3     echo 'directory is not empty'
   4 else
   5     echo 'directory is empty'
   6 fi

But, if you use failglob, note that the subshell is required; the following code does not work because failglob will raise a shell error that will cause bash to stop running the current command (including the if command, any outer compound command, and the entire function that ran this code if it is part of a function), so this will only work in the true case, the else branch will never run:

   1 # BROKEN!
   2 shopt -s dotglob failglob
   3 
   4 if { : ./* ;} 2> /dev/null; then
   5     echo 'directory is not empty'
   6 else
   7     echo 'directory is empty'
   8 fi

If you really want to avoid using the subshell and want to set failglob globally, you can either "catch" the shell error using command eval, or you can write a function that expands the glob indirectly:

   1 shopt -s dotglob failglob
   2 
   3 if command eval ': ./*' 2> /dev/null; then
   4     echo 'directory is not empty'
   5 else
   6     echo 'directory is empty'
   7 fi
   8 
   9 # or
  10 
  11 shopt -s dotglob failglob
  12 
  13 any_match () {
  14     local IFS=
  15     { : $@ ;} 2> /dev/null
  16 }
  17 
  18 if any_match './*'; then
  19     echo 'directory is not empty'
  20 else
  21     echo 'directory is empty'
  22 fi

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. Note the "magic 3 globs"1 as POSIX does not have the dotglob option.

   1 # POSIX
   2 # Clobbers the positional parameters, so make sure you don't need them.
   3 set -- * .[!.]* ..?*
   4 
   5 is_empty=1
   6 for f in "$@"; do
   7     if test -e "$f" || test -L "$f"; then
   8         is_empty=
   9         break
  10     fi
  11 done
  12 
  13 if test "$is_empty"; then
  14     echo 'directory is empty'
  15 else
  16     echo 'directory is not empty'
  17 fi

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

If you just want to count files:

   1 # POSIX
   2 n=0
   3 for f in * .[!.]* ..?*; do
   4     if test -e "$f" || test -L "$f"; then
   5         n=$((n+1))
   6     fi
   7 done
   8 printf 'directory contains %d entries\n' "$n"

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 else
   7     echo 'directory is not empty'
   8 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.

Here is another solution using find:

   1 # POSIX
   2 # Print a single `.' for each file and count the number of characters printed.
   3 # This one will recurse.  If that is not desired, see below.
   4 n=$(find . -type f -exec printf %.0s. {} + | wc -m)
   5 printf 'directory contains %d files\n' "$n"

If you want it not to recurse, then you need to tell find not to recurse into directories. This gets really tricky and ugly. GNU find has a -maxdepth option to do it. With standard POSIX find, you're stuck with -prune. This is left as an exercise for the reader.

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 / POSIX
   2 find "$somedir" -type f -exec echo Found unexpected file {} \;
   3 find "$somedir" -prune -empty -exec printf '%s is empty\n' {} \;  # 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 / POSIX
   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)2:

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


CategoryShell

BashFAQ/004 (last edited 2025-01-30 04:47:30 by larryv)