Formatting du -sh output

I would like to customize the output from:

du -hs *

      

For example, the output is:

23G    Test1
1.2M   Folder With Spaces
12G    Another Folder With Spaces

      

The problem is that I can grab the first column, but since the second column can contain spaces, the output only captures the first word. Is there a way to grab the included spaces of the second column, or perhaps return the remaining content for that row?

du -hs * | awk '{print $1 " " $2;}'

      

The above returns the following:

23G Test1
1.2M Folder
12G Another

      

EDIT . The solution is to add -F and specify the tab delimiter:

du -hs * | awk -F'\t' '{print $1 " " $2;}'

      

Tabs are also valid characters in files / folders. In my case, this will never be a problem.

+3


source to share


2 answers


For mine du

(GNU coreutils), the size and filename are separated by a tab. So the name can be obtained by removing everything up to and including the first tab:

du -hs * | awk '{size=$1; name=$0; sub(/[^\t]*\t/, "", name); print name}'

      



NOTE. The above will fail if the filenames contain newlines. Depending on which operating system you are using, there may be ways to limit this limitation. For example, on linux (GNU tools) du

can create NUL-separated entries that GNU awk (gawk) can read and interpret:

du -0hs * | awk -v RS='\0'  '{size=$1; name=$0; sub(/[^\t]*\t/, "", name); print "NAME="name}'

      

+3


source


Since it du

uses tabs, while your filenames shouldn't contain tab or newlines, you can just use cut

(with default delimiter tab).

du -hs * | cut -f1  # First field
du -hs * | cut -f2  # Second field
du -hs * | cut -f2-  # All fields >= 2 (if there are tabs in the filename)

      



If you need it awk

for further processing, this should be enough.

+2


source







All Articles