How to check if a file is empty in Bash?

后端 未结 10 664
情书的邮戳
情书的邮戳 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 18:49

    Easiest way for checking file is empty or not:

    if [ -s /path-to-file/filename.txt ]
    then
         echo "File is not empty"
    else
         echo "File is empty"
    fi
    

    You can also write it on single line:

    [ -s /path-to-file/filename.txt ] && echo "File is not empty" || echo "File is empty"
    

提交回复
热议问题