简体   繁体   中英

Bash script to print file names and their sizes in the current directory

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?

du -h * | awk -v FS='\t' '{ print $2, $1 }'

它通过获取du的输出并将其发送到内联awk脚本来工作,该脚本的内容如下:对于输入中给awk每一行,打印第二列,然后打印第一列。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM