Unix to verify file has no content and empty lines

13,132

Solution 1

Your file might have new line character only.

Try this check:

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

Solution 2

A file of 0 size by definition has nothing in it, so you are good to go. However, you probably want to use:

if [ \! -s f ]; then echo "0 Sized and completely empty"; fi

Have fun!

Share:
13,132

Related videos on Youtube

iaav
Author by

iaav

Updated on September 18, 2022

Comments

  • iaav
    iaav less than a minute

    How to verify that a file has absolutely no content. [ -s $file ] gives if file is zero bytes but how to know if file is absolutely empty with no data that including empty lines ?

    $cat sample.text
    
    $ ls -lrt sample.text
    -rw-r--r-- 1 testuser userstest 1 Jul 31 16:38 sample.text
    

    When i "vi" the file the bottom has this - "sample.text" 1L, 1C

    • Kent
      Kent about 9 years
      can you give an example a file with 0 byte, but with content (including empty lines) in it?
    • Ansgar Wiechers
      Ansgar Wiechers about 9 years
      A file with 0 bytes size couldn't possibly contain empty lines. An empty line consists of exactly one character: a line-feed. A file containing at least one empty line must thus be at least 1 byte in size. That is what @Kent was hinting at.