How to determine if a file is empty in the shell

Source: Internet
Author: User

When writing scripts in Linux, there is always the need to determine whether the existence of files, the contents of the file is empty and so on, and these operations can be implemented with the test instructions, through the man test instructions can be viewed on the test instructions manual, the manual has the following description:

-S FILE
FILE exists and has a size greater than zero
If the file exists and the file size is greater than 0, return the true

-E FILE
FILE exists
If the file exists, return the true

The sample script that tests whether the file is empty through the test instruction in the shell is as follows:

#! /bin/sh

If test-s file.txt then
        echo "HI"
else
        echo "Empty"
fi

In the shell, the test instruction has another way of writing, and the above script is equivalent to the following script:

#! /bin/sh

If [-s file.txt]; then
        echo "HI"
else
        echo "Empty"
fi

The difference between the two people to observe their own slowly AH ~ ~

Reprint Please specify: Coding ants» How to determine whether a file is empty in the shell

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.