问题
I am wanting to use the ls command to output the files in a directory however I need the file size in bytes.
Is this possible with the ls command?
on similar questions i've found this ls -l --block-size=M
which outputs the file size in megabytes however I cannot seem to get it to work with just bytes.
回答1:
If you are looking for statistics about files, then you want to use stat
rather than ls
. eg:
stat --format=%n:%s *
回答2:
$ ls -l foo.tar.gz
-rw-r--r-- 1 tex tex 68964464 Mar 12 2014 xfer.tar.gz
On my ls (GNU coreutils) 8.26
:
$ ls -s --block-size=1 xfer.tar.gz
68972544 foo.tar.gz
回答3:
ls -l | tr -s " " | cut -d " " -f5,9-
tr -s " "
replaces multiple spaces in sequence with one space
cut -d " " -f5,9-
sets the delimeter to a space, and prints the fields 5 and 9 onwards. The 9 onwards is because filenames can have spaces in them, and just printing field 9 would on;y get the first word
Bonus: If you want to switch the output order ($filename $bytes), you can add on a
| sed 's_\([0-9]*\) \(.*\)_\2 \1_'
来源:https://stackoverflow.com/questions/47821353/bash-how-to-list-files-with-size-in-bytes