I've been using the following command to list the ten largest files, at any depth, under the current directory:
find -type f -size +10M -exec ls -gGsSh1 {} + | awk 'NR<=10 {print $0}'
(I use awk
instead of head
to prevent a broken pipe error when the list is too long)
It works well, but it's evidently hard to remember and type every time. I tried putting this in .bash_aliases, but it doesn't work then (it prints 10 empty lines).
If I use less
instead of awk
, the output is presented correctly, but that means showing the whole list of files bigger than 10MB (my lower threshold), sorted by size, rather than only the top ones.
So, what's making awk
choke as part of an aliased command here (assuming that's indeed the problem), and what can I do to fix it?
When using this command as an alias,
$0
is no more protected by the single quotes so is converted to your shell name. These commands demonstrate it:The shell name (presumably
bash
) happen to be an unset awk variable name, leading to the empty lines.One way to protect
$0
from shell expansion would be to use something like:However, as this
$0
is useless, you can just remove it from theawk
statement.You can go further and remove the whole block as printing the selected line is
awk
default behavior anyway:Alternatively, you can just keep using
head
and discard the error message: