for example I have command that shows how much space folder takes
du folder | sort -n
it works great, however I would like to have human readable form
du -h folder
however if I do that than I cannot sort it as numeric.
How to join du folder
and du -h folder
to see output sorted as du folder
, but with first column from du -h folder
P.S. this is just an example. this technique might be very useful for me (if its possible)
Try something like:
Alternatives:
-n
for Numerical sortingNote: the
-h
option of sort only exists in newer versions of Ubuntu.Here is a more general approach. Get the output of
du folder
anddu -h folder
in two different files.The key part is this: concatenate
file1
andfile2
line by line, with a suitable delimiter.(assuming
#
does not appear infile1
andfile2
)Now sort
file3
. Note that this will sort based onfile1
contents and break ties byfile2
contents. Extract the relevant result usingcut
:Also take a look at
man sort
for other options.You may also save this as an alias, for later re-use. To do so, add the following to the bottom of
~/.bashrc
:Then, open a new terminal session and execute your new alias:
This answer is valid for 10.04.4LTS and lower versions of Ubuntu.
Unfortunatly the accurate answer which sorts K M G is difficult and complex:
You can alias the entire du command with one that sorts human readable using this
which I found here
http://www.earthinfo.org/linux-disk-usage-sorted-by-size-and-human-readable/
just cd into the folder you would like to know then duf
you could add this duf alias to the end of your /home/user/.profile to make the duf command semi-permenant
results:
Here is why
du -sch /var/* | sort -n
does not work see the sorting of MKKMMKKMMKCommand GNU
sort
has the following option:So the command would looks like:
For recent versions of Ubuntu, use
du -h directory | sort -h
.I use a form of this all the time for finding out-of-control files.
Open your terminal with Ctrl+Alt+t and type:
The one I found working on all systems regardless of the version (
sort -h
doesn't work for me) isthe
awk '{print $2}'
basically prints file names.This one handles filenames with whitespace or apostrophes, and works on systems which do not support
xargs -d
orsort -h
:which results in: