Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Programming»Bash Shell»5 Ways to check if a file is empty in Bash

    5 Ways to check if a file is empty in Bash

    By RahulDecember 30, 20224 Mins Read

    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 `[`
    2. 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:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      #!/usr/bin/env bash
       
      FILENAME=myfile.txt
       
      # Check if the file is empty
      if [ ! -s "${FILENAME}" ]; then
          echo "File is empty"
      else
          echo "File is not empty"
      fi

      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:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      #!/usr/bin/env bash
       
      FILENAME=myfile.txt
       
      [ -z "$(cat ${FILENAME})" ] then
          echo "File is empty"
      else
          echo "File is not empty"
      fi

      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.

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      #!/usr/bin/env bash
       
      FILENAME=myfile.txt
       
      if [ -f "${FILENAME}" ];then
          if [ -s "${FILENAME}" ];then
              echo "File ${FILENAME} exists and not empty"
          else
              echo "File ${FILENAME} exists but empty"
          fi
      else
          echo "File ${FILENAME} not exists"
      fi

      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”.

    3. Using the wc command
    4. 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:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      #!/usr/bin/env bash
       
      FILENAME=myfile.txt
       
      # Check if file has any lines
      if [ $(wc -l < "${FILENAME}") -eq 0 ]; then
          echo "File $FILENAME is empty"
      else
          echo "File $FILENAME is not empty"
      fi

      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.

    5. Using the grep command
    6. 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:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      #!/usr/bin/env bash
       
      FILENAME=myfile.txt
       
      if grep -q . "${FILENAME}"; then
          echo "File is not empty"
      else
          echo "File is empty"
      fi

      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.

      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:

      1
      2
      3
      4
      5
      6
      7
      8
      #!/usr/bin/env bash
       
      FILENAME=myfile.txt
      if find . -type f -empty -name "${FILENAME}"; then
          echo "File is empty"
      else
          echo "File is not empty"
      fi

      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:

      1
      2
      3
      4
      5
      6
      7
      8
      #!/usr/bin/env bash
       
      FILENAME=myfile.txt
      if [ $(stat -c %s "${FILENAME}") -eq 0 ]; then
          echo "File is empty"
      else
          echo "File is not empty"
      fi

      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.

    bash file script test
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    10 Bash Tricks Every Developer Should Know

    Understanding 2>&1 in Bash: A Beginner’s Guide

    Best Practices and Tips for Writing Shell Scripts as Pro

    View 3 Comments

    3 Comments

    1. Don on August 21, 2019 5:12 pm

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

      Reply
    2. HARINADH on July 25, 2019 3:57 pm

      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

      Reply
    3. Dave on July 9, 2019 9:17 am

      Thanks Rahul, great post

      Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • How to List Manually Installed Packages in Ubuntu & Debian
    • 10 Bash Tricks Every Developer Should Know
    • How to Validate Email Address in JavaScript
    • Firewalld: Common Firewall Rules and Commands
    • 12 Apk Commands in Alpine Linux Package Management
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.