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:

$ alias F="echo '$0'"      
$ F

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:

alias F="find -type f -size +1k -exec ls -gGsSh1 {} + | awk 'NR<=10 {print \$0}'"

However, as this $0 is useless, you can just remove it from the awk statement.

find -type f -size +10M -exec ls -gGsSh1 {} + | awk 'NR<=10 {print}'

You can go further and remove the whole block as printing the selected line is awk default behavior anyway:

find -type f -size +10M -exec ls -gGsSh1 {} + | awk 'NR<=10'

Alternatively, you can just keep using head and discard the error message:

find -type f -size +10M -exec ls -gGsSh1 {} + 2>&1 | head
  • Works like a charm, thanks! Also, TIL a new awk trick :D (not that I knew many, though :P) – waldyrious May 6 '13 at 12:00
  • 1
    @jlliagre - actually $0 here is not a shell name as it is protected by ' and passed verbatim to awk. It denotes the whole line in shell (Change it to $1 and you'll see first record of line not first argument to shell). – Maciej Piechotka May 6 '13 at 14:25
  • @MaciejPiechotka You are missing the point. $0 is definitely replaced by the shell name when the command is used as an alias. – jlliagre May 6 '13 at 15:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.