Search code examples
bashsortinggrepfindxargs

How do I sort result of find command by file size, and display the file sizes (in MB)?


As an auxiliary question, it'd be nice to know how to make the output look neat as well (does bash have some form of string formatting?) Anyways, I have a find . command returning all files over a certain size, and I want to sort those results in descending order and pipe them to a mailer. I have the mailing part down, but I'm not sure how to sort them and display the file sizes in an organized manner. Thank you for the help!


Solution

  • This will find all files > 1000 bytes and print out the filename and size (in bytes):

    $ find tmp/ -size +1000c -printf '%p %s\n' | sort -k2 -n
    

    Which would give you output like:

    tmp/gitwork/integration/.git/hooks/pre-rebase.sample 4951
    tmp/gitwork/repo1/.git/hooks/pre-rebase.sample 4951
    tmp/gitwork/repo2/.git/hooks/pre-rebase.sample 4951
    tmp/gitwork/upstream1/hooks/pre-rebase.sample 4951
    tmp/gitwork/upstream2/hooks/pre-rebase.sample 4951
    tmp/constraints.dot 7035
    tmp/constraints.svg 41657
    tmp/so31567373/KBFI.xml 375557
    tmp/overflow.tar 399360
    

    If you want to make this "pretty", you could add some field-width specifiers to that printf directive:

    $ find tmp/ -size +1000c -printf '%-60p %s\n' | sort -k2 -n
    

    Which would get you:

    tmp/gitwork/integration/.git/hooks/pre-rebase.sample         4951
    tmp/gitwork/repo1/.git/hooks/pre-rebase.sample               4951
    tmp/gitwork/repo2/.git/hooks/pre-rebase.sample               4951
    tmp/gitwork/upstream1/hooks/pre-rebase.sample                4951
    tmp/gitwork/upstream2/hooks/pre-rebase.sample                4951
    tmp/constraints.dot                                          7035
    tmp/constraints.svg                                          41657
    tmp/so31567373/KBFI.xml                                      375557
    tmp/overflow.tar                                             399360
    

    If your definition of "pretty" requires something more complex, you may want to investigate piping the output through awk.