Search code examples
bashunixshell

Checking from shell script if a directory contains files


From a shell script, how do I check if a directory contains files?

Something similar to this

if [ -e /some/dir/* ]; then echo "huzzah"; fi;

but which works if the directory contains one or several files (the above one only works with exactly 0 or 1 files).


Solution

  • The solutions so far use ls. Here's an all bash solution:

    #!/bin/bash
    shopt -s nullglob dotglob     # To include hidden files
    files=(/some/dir/*)
    if [ ${#files[@]} -gt 0 ]; then echo "huzzah"; fi