An administrator is writing a bash script that loops through all .log files in /var/log and prints the file name if the file is larger than 100 kilobytes. Which loop correctly implements this?
Trap 1: ls -l /var/log/*.log | awk '$5>100 {print $NF}'
This works for some cases but may fail with spaces in filenames or if ls output changes.
Trap 2: for f in /var/log/*.log; do if [ -s $f -a $(stat -c%s $f) -gt 100…
-s checks if file is non-empty, not size; -gt compares numbers but stat output may cause issues.
Trap 3: for f in /var/log/*.log; do if [ $f -gt 100k ]; then echo $f; fi;…
The test command cannot compare file sizes directly; -gt is for integer comparison.
- A
ls -l /var/log/*.log | awk '$5>100 {print $NF}'
Why it fails: This works for some cases but may fail with spaces in filenames or if ls output changes.
- B
for f in /var/log/*.log; do if [ -s $f -a $(stat -c%s $f) -gt 100 ]; then echo $f; fi; done
Why it fails: -s checks if file is non-empty, not size; -gt compares numbers but stat output may cause issues.
- C
find /var/log -name '*.log' -size +100k -exec echo {} \;
Correct. find with -size +100k matches files larger than 100 kilobytes.
- D
for f in /var/log/*.log; do if [ $f -gt 100k ]; then echo $f; fi; done
Why it fails: The test command cannot compare file sizes directly; -gt is for integer comparison.