Search This Blog

2020-09-29

test if directory is empty ... :)

https://stackoverflow.com/questions/91368/checking-from-shell-script-if-a-directory-contains-files

https://superuser.com/questions/352289/bash-scripting-test-for-empty-directory

https://www.cyberciti.biz/faq/linux-unix-shell-check-if-directory-empty/

# ?O.o ... well ::: dash/bash only... :)

[ "`echo *`" = '*' -a "`echo **`" = '**' ] && {
  echo has no public files ;
  [ "`echo .*`" = '.*' -a "`echo .**`" = '.**' ] &&
    echo also has no hidden files, is empty ;
} ;

there it is ... :)

# example of use ... :)

cdl ()  # cd and maybe list
{
  local owd="`pwd`" ;
  cd "$@" &&  {
    [ "$owd" = "`pwd`" ] ||
    # actually changed directory,
      [ "`echo *`" = '*' \
      -a "`echo **`" = '**' ] ||
        # has public files,
        { echo ; .ls ; }  # list them.
  };
} ;