Script to check if directory is empty

0

I'm hitting my head to create this script.

I need a script, it can be in bash or php itself, it should check if a directory has a file inside it, other than another directory.

This directory 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, no problems.

Does anyone know how to do it?

    
asked by anonymous 11.01.2018 / 20:23

2 answers

1

In php it can be done like this according to the answer found in: link

$files = scandir($dir); 
foreach($files as $file){
    if(is_file($dir.$file)){
      ....

It uses scandir to retrieve all content from grazing ($ dir) and then uses is_file to check item by item if any of them is a file

    
11.01.2018 / 20:56
1

You can create alias .

Finding 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     

11.01.2018 / 20:45