How to check if a file is empty in Bash?

后端 未结 10 627
情书的邮戳
情书的邮戳 2020-11-30 18:41

I have a file called diff.txt. I Want to check whether it is empty.

I wrote a bash script something like below, but I couldn\'t get it work.

10条回答
  •  北海茫月
    2020-11-30 19:07

    [[ -s file ]] --> Checks if file has size greater than 0

    if [[ -s diff.txt ]]; then echo "file has something"; else echo "file is empty"; fi
    

    If needed, this checks all the *.txt files in the current directory; and reports all the empty file:

    for file in *.txt; do if [[ ! -s $file ]]; then echo $file; fi; done
    

提交回复
热议问题