Reputation: 475
I would like to customize the output from:
du -hs *
E.g output:
23G Test1
1.2M Folder With Spaces
12G Another Folder With Spaces
The problem is that I can capture the first column but since the second column may contain spaces the output only captures the first word. Is there a way to capture the second column spaces included or perhaps return the remaining content for that line?
du -hs * | awk '{print $1 " " $2;}'
The above returns this:
23G Test1
1.2M Folder
12G Another
EDIT: The Solution is to add the -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 would never be an issue.
Upvotes: 4
Views: 6348
Reputation: 701
As du
uses tabs while your filenames shouldn't contain tab or new lines, you can simply use cut
(with the default delimiter being 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)
Unless you need awk
for further processing, this should be enough.
Upvotes: 2
Reputation: 113964
For my du
(GNU coreutils), the size and file name are separated by a tab. So, the name can be retrieved 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 file names contain newline characters. Depending on what operating system you are using, there may be ways around this limitation. For example, on linux (GNU tools), du
can produce NUL-separated records which GNU awk (gawk) can read and interpret:
du -0hs * | awk -v RS='\0' '{size=$1; name=$0; sub(/[^\t]*\t/, "", name); print "NAME="name}'
Upvotes: 3