Dar: sort listing by size

From FVue
Jump to: navigation, search

Problem

I want to sort the output of dar -l by size.

Solution

dar -l archive | sort -k3 -n -t$'\t'

Journal

20060205

Example output of dar -l:

[data ][ EA  ][compr] | permission | user  | group | size  |          date                 |    filename
----------------------+------------+-------+-------+-------+-------------------------------+------------
[Saved]       [-----]   drwxr-xr-x   freddy     users   0       Mon Mar 14 10:01:43 2005        gpg
[Saved]       [-----]   drwxr-xr-x   freddy     users   0       Sun Mar  6 01:22:01 2005        gpg/www

Note that this:

dar -l archive | sort --key=6

does not work, because field 6 is undefined: the EA and compr fields may contain spaces which prevents the sort command to correctly detect the size field.

Fortunately the output contains TABs at key positions ( ):

[data ][ EA  ][compr] | permission | user  | group | size  |          date                 |    filename
----------------------+------------+-------+-------+-------+-------------------------------+------------
[Saved]       [-----]   drwxr-xr-x   freddy     users   0       Mon Mar 14 10:01:43 2005        gpg
[Saved]       [-----]   drwxr-xr-x   freddy     users   0       Sun Mar  6 01:22:01 2005        gpg/www

So if we tell sort to split fields by TAB, we can sort on the third – size – field:

dar -l archive | sort --field-separator=$'\t' --key=3 --numeric
# Or for short
dar -l archive | sort -k3 -n -t$'\t'

Comments

blog comments powered by Disqus