List the most-used file test operators.
BeginnerConceptArrays, conditionals and loops
Answer
| Test | True if |
|---|---|
-e f |
exists (any type) |
-f f |
regular file |
-d f |
directory |
-L f / -h f |
symlink |
-s f |
exists and size > 0 |
-r / -w / -x f |
readable / writable / executable by you |
f1 -nt f2 |
f1 newer than f2 (mtime) |
-S f / -p f |
socket / named pipe |
[[ -s /var/backups/latest.tar.gz ]] || die "backup missing or empty"
[[ -x /usr/local/bin/aws ]] || die "aws CLI not installed"
-e follows symlinks, so a broken symlink fails -e but passes -L.
More on Arrays, conditionals and loops
How do you declare, populate and iterate an indexed array?BeginnerWhat is the difference between `"${arr[@]}"` and `"${arr[*]}"`?IntermediateHow do associative arrays work and what are they good for in DevOps scripts?IntermediateHow do you read a command's output into an array correctly?IntermediateWhat is the difference between `[ ]`, `[[ ]]` and `(( ))`?BeginnerWhy does `[ $var = "prod" ]` fail when `var` is empty or contains spaces?Intermediate