2

This question already has an answer here:

as all know this syntax will print file is empty

[ -s file.name ] || echo "file is empty"

but what in case file is with spaces or TAB or blank spaces ? , but without any real character as a-z 1-9 and so on

so I use this

[[ $(tr -d "\r\n" < file.name |wc -c) -eq 0 ]] && echo "File has no content"

but when we set that

echo "       " >file.name

then the follwing syntax will not print - File has no content

 [[ $(tr -d "\r\n" < file.name |wc -c) -eq 0 ]] && echo "File has no content" 

so we are in trouble here

how to verify file is empty but can include spaces / TABs / blank or empty without anything

goal - verify file is empty but can include spaces / tab /blank lines , or empty without any spaces / blank lines / empty tab

marked as duplicate by don_crissti, Jeff Schaller, αғsнιη, Stéphane Chazelas bash Jan 18 '18 at 11:20

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

5

With grep:

grep -q '[^[:space:]]' file.name || echo "file is empty or unreadable"
  • grep '[^[:space:]]' file.name - will generate output only if any non-whitespace character [^[:space:]] found.
  • -q suppresses the output to only retain the exit status that tells us if a non-spacing character was found or not.

Not the answer you're looking for? Browse other questions tagged or ask your own question.