Forrest logo
back to the [[ tool

shell:warp:98a8e

Check if file exists and has a size greater than zero
$ [[ -s ${file} ]]
try on your machine

The command [[ -s ${file} ]] is a conditional expression in the Bash scripting language. It is used to evaluate whether a file exists and has a non-zero size.

Here's a breakdown of the command:

  • [[ ... ]] is a conditional expression construct in Bash that allows for more advanced tests compared to the traditional [ ... ] (single brackets) construct.

  • -s is an operator within the conditional expression that checks if a file exists and has a size greater than zero. It returns true if the file is non-empty, and false otherwise.

  • ${file} is a variable that represents the name or path of the file being tested. The actual value of the file variable will be substituted in its place during execution.

In summary, the [[ -s ${file} ]] command is used to check if the file specified by the value stored in the file variable exists and has a non-zero size. If the condition evaluates to true, further actions can be taken; otherwise, the script can proceed with an alternative course of action.

This explanation was created by an AI. In most cases those are correct. But please always be careful and never run a command you are not sure if it is safe.
back to the [[ tool