Shell – How to Check if File is Empty or Only Contains Blank Characters

filesshell

This will notify us if the file is empty:

[[ ! -s $file ]] && echo "hello there I am empty file !!!"

But how to check if a file has blank spaces (spaces or tabs)?

  • empty file can include empty spaces / TAB

Best Answer

Just grep for a character other than space:

grep -q '[^[:space:]]' < "$file" &&
  printf '%s\n' "$file contains something else than whitespace characters"
Related Question