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)
Best Answer
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: