check if a file is empty in shell script
A shell script (isEmpty.sh) example to check if a file is empty or not.
#!/bin/sh f='afile.txt' hasData () { echo "$f has data." ls -l $f cat $f } noData() { echo "$f is empty." ls -l $f } if [[ -s $f ]] then hasData else noData fi
The text file afile.txt
hello world
The output when the file has data
afile.txt has data. -rw-r--r--+ 1 username 941854322 12 Jan 17 11:31 afile.txt hello world
The output when the file is empty.
afile.txt is empty. -rw-r--r--+ 1 username 941854322 0 Jan 17 11:37 afile.txt
The run the script.
chmod +x isEmpty.sh ./isEmpty.sh
Search within Codexpedia
Custom Search
Search the entire web
Custom Search
Related Posts