How to check if a file contains only zeros in a Linux shell?
If you're using bash, you can use read -n 1
to exit early if a non-NUL
character has been found:
<your_file tr -d '\0' | read -n 1 || echo "All zeroes."
where you substitute the actual filename for your_file
.
How to check if a file is empty in Bash?
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.
Find all zero-byte files in directory and subdirectories
To print the names of all files in and below $dir of size 0:
find "$dir" -size 0
Note that not all implementations of find
will produce output by default, so you may need to do:
find "$dir" -size 0 -print
Two comments on the final loop in the question:
Rather than iterating over every other word in a string and seeing if the alternate values are zero, you can partially eliminate the issue you're having with whitespace by iterating over lines. eg:
printf '1 f1\n0 f 2\n10 f3\n' | while read size path; do
test "$size" -eq 0 && echo "$path"; done
Note that this will fail in your case if any of the paths output by ls contain newlines, and this reinforces 2 points: don't parse ls
, and have a sane naming policy that doesn't allow whitespace in paths.
Secondly, to output the data from the loop, there is no need to store the output in a variable just to echo
it. If you simply let the loop write its output to stdout, you accomplish the same thing but avoid storing it.
How to check if a file contains a specific string using Bash
if grep -q SomeString "$File"; then
Some Actions # SomeString was found
fi
You don't need [[ ]]
here. Just run the command directly. Add -q
option when you don't need the string displayed when it was found.
The grep
command returns 0 or 1 in the exit code depending on
the result of search. 0 if something was found; 1 otherwise.
$ echo hello | grep hi ; echo $?
1
$ echo hello | grep he ; echo $?
hello
0
$ echo hello | grep -q he ; echo $?
0
You can specify commands as an condition of if
. If the command returns 0 in its exitcode that means that the condition is true; otherwise false.
$ if /bin/true; then echo that is true; fi
that is true
$ if /bin/false; then echo that is true; fi
$
As you can see you run here the programs directly. No additional []
or [[]]
.
Unix to verify file has no content and empty lines
Your file might have new line character only.
Try this check:
[[ $(tr -d "\r\n" < file|wc -c) -eq 0 ]] && echo "File has no content"
Related Topics
How to Capitalize First Letter of Each Line in Bash
Flags in Objdump Output of Object File
Getmodulehandle(Null) on Linux
How to Measure the Stack Size of a Process
Objdump and Resolving Linkage of Local Function Calls
Linux Serial Port Buffer Not Empty When Opening Device
How to Count Most Occuring Sequence of 3 Letters Within a Word with a Bash Script
How to Determine the Precise Set of Environment Variables a Systemd Environmentfile Would Set
Shell Script for Process Monitoring
How Do Programs Communicate with Each Other
Linux Get Process Name from Pid Within Kernel
How to Retrieve Advertising Payload from Ibeacon/Ble
Initiating Dynamic Variables (Variable Variables) in Bash Shell Script