Reputation: 153
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.
Upvotes: 15
Views: 30592
Reputation: 212424
If you are looking for statistics about files, then you want to use stat
rather than ls
. eg, with gnu stat:
stat --format=%n:%s *
Upvotes: 23
Reputation: 37424
$ ls -l foo.tar.gz
-rw-r--r-- 1 james james 68964464 Mar 12 2014 foo.tar.gz
On my ls (GNU coreutils) 8.26
:
$ ls -s --block-size=1 foo.tar.gz
68972544 foo.tar.gz
Upvotes: 13
Reputation: 363
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_'
Upvotes: 2