How to check if a file is empty in Bash?

I have a file called diff.txt. Want to check if it is empty. Did something similar, but could not get it to work.

if [ -s diff.txt ] then touch empty.txt rm full.txt else touch full.txt rm emtpy.txt fi 
+151
bash is-empty if-statement file-handling
Apr 01 2018-12-12T00:
source share
9 answers

Mistakes are annoying, aren't they? Check the spelling of empty , but then try the following:

 #!/bin/bash -e if [ -s diff.txt ] then rm -f empty.txt touch full.txt else rm -f full.txt touch empty.txt fi 

I really like the use of shell scripts, but one of the drawbacks of this is that the shell cannot help you when you make a mistake, while a compiler like your C ++ compiler can help you.

Note that I have changed the roles of empty.txt and full.txt , as @Matthias suggests.

+197
Apr 01 2018-12-12T00:
source share
 [ -s file.name ] || echo "file is empty" 
+61
Dec 30 '15 at 18:03
source share

[[-s file]] → Checks if the file size is greater than 0

 if [[ -s diff.txt ]]; then echo "file has something"; else echo "file is empty"; fi 

If necessary, all * .txt files in the current directory are checked; and reports everything about the empty file:

 for file in *.txt; do if [[ ! -s $file ]]; then echo $file; fi; done 
+45
May 09 '17 at 3:25
source share

While the other answers are correct, using the "-s" will also indicate that the file is empty, even if the file does not exist.
By adding this extra check "-f" to see if the file exists first, we guarantee that the result is correct.

 if [ -f diff.txt ] then if [ -s diff.txt ] then rm -f empty.txt touch full.txt else rm -f full.txt touch empty.txt fi else echo "File diff.txt does not exist" fi 
+8
05 Oct '17 at 11:48 on
source share

@geedoubleya is my favorite answer.

However, I prefer this

 if [[ -f diff.txt && -s diff.txt ]] then rm -f empty.txt touch full.txt elif [[ -f diff.txt && ! -s diff.txt ]] then rm -f full.txt touch empty.txt else echo "File diff.txt does not exist" fi 
+4
Apr 11 '18 at 10:52
source share

Many of the answers are correct, but I feel they can be more complete / simplified, etc., for example:

Example 1. The basic if statement

 # BASH4+ example on Linux : typeset read_file="/tmp/some-file.txt" if [ ! -s "${read_file}" ] || [ ! -f "${read_file}" ] ;then echo "Error: file (${read_file}) not found.. " exit 7 fi 

if $ read_file is empty or not, stop showing up. I read the top answer more than once to indicate the opposite.

Example 2: as a function

 # -- Check if file is missing /or empty -- # Globals: None # Arguments: file name # Returns: Bool # -- is_file_empty_or_missing() { [[ ! -f "${1}" || ! -s "${1}" ]] && return 0 || return 1 } 
+1
Apr 29 '18 at 18:27
source share
 [[ -f filename && ! -s filename ]] && echo "filename exists and is empty" 
+1
May 14 '19 at 13:25
source share

To check if a file is empty or has only spaces , you can use grep:

 if [[ -z $(grep '[^[:space:]]' $filename) ]] ; then echo "Empty file" ... fi 
+1
Aug 08 '19 at 15:52
source share

I came here looking for how to delete empty __init__.py files since they are implicit in Python 3. 3+, and ended up using:

 find -depth '(' -type f -name __init__.py ')' -print0 | while IFS= read -d '' -r file; do if [[ ! -s $file ]]; then rm $file; fi; done 

Also (at least in zsh) using $ path as a variable also violates your $ PATH environment and thus destroys your open shell. Anyway, I thought that I would share!

0
Jan 23 '19 at 16:17
source share



All Articles