If I run the command du -ah | sort -nr
is it possible to make it show which line in the output is a file and which folder? Something like the command ls -l
shows which is file and folder with d
and -
in front.
If I run the command du -ah | sort -nr
is it possible to make it show which line in the output is a file and which folder? Something like the command ls -l
shows which is file and folder with d
and -
in front.
It is sort of possible, but not without aid of another command. Specifically, here we're using GNU awk ( that's important, so check your awk version )
Of course this approach is slightly flawed since it seems to fail with filenames that contain spaces (still working on improving it )
As alternative, we can also use
find
command to find all the files and runfile
anddu
commands to do our bidding via-exec
flag, but that looks slightly ugly (see the edit history). We could, however, usefind
's-printf
flag to print filetype with%y
format for much prettier output.Obviously here
f
is for regular file andd
is for directory. Seefind
's manual for more info on flags and filetype lettersAs Serg says,
du
can't do this by itself. To process filenames safely, the best way is to separate them with the ASCII nul character (\0
), anddu
can do that. So, using that, along withsort
andxargs
' ability to handle null-delimited input:The
-0
,-z
and-0
options telldu
,sort
andxargs
to use ASCII nul as the separator.Then,
s=${i%[[:space:]]*}
to get the start of the line until whitespace (which is the size), andf=${i#*[[:space:]]}
to get everything else (the filename). Then we getls
to print the filename with decoration.Example:
Because I used
ls --color
, I also get nice colour output: