du -h * | awk -v FS='\t' '{ print $2, $1 }'
It works by taking the output from du
and sending it to an inline awk
script, which reads as follows: for every line given to awk
in input, print the 2nd and then 1st column.
Pregunta
I just wrote a bash script to print the size and the name of all the files in a directory. It's quite simple:
du -h *
It prints first the size of the file and then the name. But now I wonder how can I reverse the order of the output: print first the name and then the size of the file.
Any ideas?
Solución
du -h * | awk -v FS='\t' '{ print $2, $1 }'
It works by taking the output from du
and sending it to an inline awk
script, which reads as follows: for every line given to awk
in input, print the 2nd and then 1st column.
Otros consejos
Here is a Perl based solution:
du -h *|perl -pe'/\s(.+)/&&{$_="$1\t$`\n"}'
It works properly for both spaces and tabs inside of file names.