I have a file called diff.txt. I Want to check whether it is empty.
I wrote a bash script something like below, but I couldn't get it work.
if [ -s diff.txt ] then touch empty.txt rm full.txt else touch full.txt rm emtpy.txt fi 411 Answers
Misspellings are irritating, aren't they? Check your spelling of empty, but then also try this:
#!/bin/bash -e if [ -s diff.txt ]; then # The file is not-empty. rm -f empty.txt touch full.txt else # The file is empty. rm -f full.txt touch empty.txt fi I like shell scripting a lot, but one disadvantage of it is that the shell cannot help you when you misspell, whereas a compiler like your C++ compiler can help you.
Notice incidentally that I have swapped the roles of empty.txt and full.txt, as @Matthias suggests.
[ -s file.name ] || echo "file is empty" 3[ -s file ] # Checks if file has size greater than 0
[ -s diff.txt ] && echo "file has something" || echo "file is empty" If needed, this checks all the *.txt files in the current directory; and reports all the empty file:
for file in *.txt; do if [ ! -s $file ]; then echo $file; fi; done 2While the other answers are correct, using the "-s" option will also show the file is empty even if the file does not exist.
By adding this additional check "-f" to see if the file exists first, we ensure 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 0To check if file is empty or has only white spaces, you can use grep:
if [[ -z $(grep '[^[:space:]]' $filename) ]] ; then echo "Empty file" ... fi 1Easiest way for checking if file is empty or not:
if [ -s /path-to-file/filename.txt ] then echo "File is not empty" else echo "File is empty" fi You can also write it on single line:
[ -s /path-to-file/filename.txt ] && echo "File is not empty" || echo "File is empty" @geedoubleya answer is my favorite.
However, I do 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 [[ -f filename && ! -s filename ]] && echo "filename exists and is empty" Many of the answers are correct but I feel like they could be more complete / simplistic etc. for example :
Example 1 : 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 there stop the show with exit. More than once I have had misread the top answer here to mean 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 } I came here looking for how to delete empty __init__.py files as 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 the variable also breaks your $PATH env and so it'll break your open shell. Anyway, thought I'd share!
Similar to @noam-manos's grep-based answer, I solved this using cat. For me, -s wasn't working because my "empty" file had >0 bytes.
if [[ ! -z $(cat diff.txt) ]] ; then echo "diff.txt is not empty" else echo "diff.txt is empty" fi 0