bash: Test if a file has nonzero size.



  • Take into account the main readme for this section. README first.

    Using the -s test option.

    echo -n "Type in your filename here:"
    read MyFile
    
    if [[ -s "$MyFile" ]];then
       echo "$MyFile is not empty."
    else
       echo "$MyFile is empty"
    fi
    

    The script execution output of the script is:

    $ ./test.sh
    Type in your filename here:/etc/hosts
    /etc/hosts is not empty.
    

    File is empty

    $ ./test.sh
    Type in your filename here:/tmp/myfile1
    /tmp/myfile1 is empty
    

Log in to reply
 

© Lightnetics 2024