With zsh
:
set -- *(.om[1]) *(N.L+2097152om[1])
if [[ $1 = $2 ]]; then
printf '%s\n' "The apparent size of the newest non-hidden regular file in the current" \
"directory ($1) is strictly greater than 2MiB."
fi
If you want to include hidden directories, add D
to both glob qualifiers. If you want to consider non-regular files (directories, symlinks, devices...), remove .
The idea is to expand both these globs:
- the list of non-hidden regular (
.
) files, o
rdered by m
odification time, limited to one ([1]
).
- the same but limited to files whose
L
ength is strictly greater (+
) than 2097152
(but enabling N
ullGlob so it's not a fatal error if there's no match).
And our condition is meant if both globs expand to the same file.
Note that ls -s
, doesn't report the size of files but their disk usage (in number of 512-byte units, or KiB or other depending on the ls
implementation and/or the environment). ls
reports the file size in it long output format (ls -l
or ls -n
(or -o
/-g
with some implementations)).
Another option is to use zsh
's stat
builtin to get the size (or disk usage) of the newest file:
zmodload zsh/stat
if
stat -LH s -- *(.om[1]) &&
((s[size] > 2097152))
then
printf '%s\n' "The apparent size of the newest non-hidden regular file in the current" \
"directory ($1) is strictly greater than 2MiB."
fi
Or:
zmodload zsh/stat
if
stat -LH s -- *(.om[1]) &&
((s[blocks] > 2097152))
then
printf '%s\n' "The newest non-hidden regular file in the current directory" \
"($1) uses more than 2097152 512-byte units of disk space."
fi
(in other words, its disk usage is more than 1GiB)
ls -s
doesn't report the size but the disk usage.