Script to check if directory is empty

Asked

Viewed 1,165 times

0

I’m knocking my head to create this script.

I need a script, can be in bash or php even, check if a directory has some file inside, that is not another directory.

This directory that I have to scan is in linux server that I have here in the company, so the script can be in bash, which I put to run daily on linux. Or in PHP, I make linux run the PHP file, without problems tb.

Someone knows how to do?

  • Have you tried ls /path/to | wc -l ??

  • I’ll test it here Valdeir, but from what I read here I think it already helps me.

2 answers

1

You can create alias.

Find empty directories (Shellscript):

 alias searchDirEmpty='dirEmpty() { find $1 -type d -empty -print; }; dirEmpty'

How to use: searchDirEmpty /path/to


Checks if a given directory is empty (Shellscript):

alias checkDirEmpty='checkDir() { [[ $(ls $1 | wc -l) == 0 ]] && echo "Diretório vázio"; }; checkDir'

How to use: checkDirEmpty /path/to

1


  • Thank you Ari, that came out right.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.