shin
shin

Reputation: 351

Check if a directory contains only directories and suppress output

What I have so far is this:

if [[ $(find $directory -type f) -eq 0 ]]; then
  echo "Specified directory contains only subdirectories and no file";
  exit 1;
fi

The check works, but if said directory does have files in it, I see the output of the command 'find'. I don't need to see it, I just want to know if the directory in question contains only directories, and if it does, show that message. I put this check before a for loop over the directory, so that if it doesn't have files terminates.

I feel like there has to be a simple way to achieve this result but couldn't find anything specific to this. Do you have a solution?

Upvotes: 1

Views: 327

Answers (1)

Arkadiusz Drabczyk
Arkadiusz Drabczyk

Reputation: 12393

You don't need ; at the end of the line in shell. Also, find does not return a number - you have to count number of matches. This should work:

if [[ "$(find "$directory" -type f -printf . | wc -c)"  -eq 0 ]]; then
  echo "Specified directory contains only subdirectories and no file"
  exit 1
fi

Upvotes: 1

Related Questions