Size: 2057
Comment: last example doesn't need the directory printed twice
|
Size: 2494
Comment: #faqNN -> /0NN
|
Deletions are marked like this. | Additions are marked like this. |
Line 26: | Line 26: |
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. | 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. |
Line 35: | Line 35: |
shopt -u nullglob | |
Line 37: | Line 38: |
It also simplifies various other operations: | 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: {{{ shopt -s nullglob files=(*) (( ${#files[*]} )) || echo directory is empty shopt -u nullglob }}} `nullglob` also simplifies various other operations: |
Line 44: | Line 54: |
shopt -u nullglob | |
Line 46: | Line 57: |
Without the {{{shopt}}}, that would have to be: | Without the {{{nullglob}}}, that would have to be: |
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:
if [ -n "$(ls -A somedir)" ] then echo directory is non-empty fi
This can be shortened to:
if [ "$(ls -A somedir)" ] then echo directory is non-empty fi
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.
Nevertheless, if you're willing to use this approach, it does greatly simplify this particular task:
shopt -s nullglob if [[ -z $(echo *) ]]; then echo directory is empty fi shopt -u nullglob
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:
shopt -s nullglob files=(*) (( ${#files[*]} )) || echo directory is empty shopt -u nullglob
nullglob 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 shopt -u nullglob
Without the nullglob, that would have to be:
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.)
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:
find "$somedir" -type f -exec echo Found unexpected file {} \;
It's all a matter of addressing the program's actual requirements.