A system administrator needs to write a shell script that will output 'Large file detected' if a specified file size exceeds 1024 kilobytes. Which of the following shell script code blocks is the BEST to accomplish this?
if [ $(stat -c%s "file.txt") / 1024 -gt 1024 ]; then echo 'Large file detected'; fi
if [[ $(stat -c%s "file.txt") -gt 1024 ]]; then echo 'Large file detected'; fi
if [ $(stat -c%s "file.txt") -gt 1048576 ]; then echo 'Large file detected'; fi
if [ $(stat -c%s "file.txt") -gt 1024 ]; then echo 'Large file detected'; fi
The correct code block uses the -gt operator to check if the file size is greater than 1024 kilobytes. Remember that stat -c%s filename retrieves the file size in bytes, so you must divide by 1024 to convert bytes to kilobytes. The if statement then evaluates this condition and prints 'Large file detected' if the condition is true. Other comparisons or the lack of size conversion can result in incorrect behavior or syntax errors.
Ask Bash
Bash is our AI bot, trained to help you pass your exam. AI Generated Content may display inaccurate information, always double-check anything important.
What does the `stat -c%s` command do?
Open an interactive chat with Bash
What does the `-gt` operator signify in shell scripting?
Open an interactive chat with Bash
Why do we divide file size by 1024 when checking in kilobytes?