In Linux, an empty file is a file that has a size of zero bytes. This means that the file does not contain any data, and it does not have any content when it is opened in a text editor. An empty file can be created using the touch command: `touch myfile.txt`

Advertisement

This will create an empty file called myfile.txt in the current directory. You can also create an empty file using the echo command with the > operator: `echo > myfile.txt`

This will overwrite any existing content in the file with an empty string, effectively creating an empty file.

Method to check if a file is empty in Bash

There are several different ways to check if a file is empty in Bash, depending on your specific needs and the tools that are available on your system. In this article, we’ll look at five different approaches for checking if a file is empty in Bash.

  1. Using the `test` command or `[`

    The `test` command (which is an alias for the `[` command) is a simple and widely-available utility for performing various tests on files and other objects in Bash. To check if a file is empty using `[`, you can use the `-s` option, which checks the size of the file:

    Note that the `-s` option considers a file to be empty if its size is zero, but it does not check for the presence of hidden characters or whitespace. If you want to check for a completely empty file (i.e., one that contains only whitespace or has no lines at all), you can use the `-z` option, which checks if the string is empty:

    This will read the contents of the file and pass them to the test command as a string. If the string is empty, the file is considered empty.

    You can also check for files existing before testing for empty file. The below script will check if the file exists and if the file is empty or not.

    As per the above example, if myfile.txt does not exist, the script will show the output as “File not exists” and if the file exists and is an empty file then it will show the output as “File exists but empty”, else if file exists has some content in it will show output as “File exists and not empty”.

  2. Using the wc command

    The `wc` (word count) command is another utility that can be used to check if a file is empty in Bash. To check the number of lines in a file, you can use the `-l` option:

    This will pass the contents of the file to the `wc` command as standard input, and the `-l` option will count the number of lines. If the number of lines is zero, the file is considered empty.

  3. Using the grep command

    The `grep` command is a powerful tool for searching and processing text files. To check if a file is empty using grep, you can use the `-q` option, which causes grep to run quietly and return an exit code:

    This will search the file for any character (. is a regular expression that matches any character) and return a zero exit code if a match is found, or a non-zero exit code if the file is empty.

  4. Using the find command

    The `find` command is a utility for searching and processing files and directories. To check if a file is empty using find, you can use the -empty option, which matches files that are empty:

    This will search the current directory (.) for files (-type f) that are empty (-empty) and have the name “myfile.txt” (-name “myfile.txt”). If a match is found, the find command will return a zero exit code, indicating that the file is empty. If no match is found, the find command will return a non-zero exit code, indicating that the file is not empty.

    Using the stat command

    The `stat` command is a utility for displaying information about files and filesystems. To check if a file is empty using stat, you can use the -c option to display the size of the file in bytes:

    This will pass the size of the file to the `[` command, which will compare it to zero. If the size is zero, the file is considered empty. If the size is non-zero, the file is considered not empty.

I hope these examples have given you a good understanding of the different ways to check if a file is empty in Bash. As you can see, there are many different approaches to choose from, and the best one will depend on your specific needs and the tools that are available on your system.

Share.

3 Comments

  1. Rahul, great, short and sweet. Another website had a more involved approach to this, I always like the clear and concise approach.

  2. what if file name variable is empty ?? Still it says File exists and not empty

    #!/bin/bash

    FILENAME=””

    if [ -f ${FILENAME} ];then
    if [ -s ${FILENAME} ]
    then
    echo “File exists and not empty”
    else
    echo “File exists but empty”
    fi
    else
    echo “File not exists”
    fi

Leave A Reply


Exit mobile version