17

I am following the snippet here to improve my terminal command in Mac OSX.

It sets the default value of ls results to human readable by exporting alias ls='ls -GFh' to bash profile file. This is very handy, but occasionally I want to see exact size of a file in bytes (in order to compare it with another file).

How can I do that? is there a command I can use ls with to force it show results in bytes?

Is there other command I can use to get file size?

I thought of du -s but it would give me just an estimation of used disk space for that file and also minimum size is kilobyte blocks.

3 Answers 3

22

Is there other command I can use to get file size?

Use one of the following:

wc -c file

-c prints the byte count.

\ls -ln file

\ escapes the ls alias.

Linux:

stat --format="%s" file

OS X:

stat -f "%z bytes" file

See Stack Overflow question Portable way to get file size (in bytes) in shell? for other alternatives.

2
  • With OS X El Capitan, stat --format="%s" file didn't work for me. Did you mean stat -f '%z' file?
    – creidhne
    Aug 6, 2016 at 9:39
  • 1
    @creidhne Just looked it up. stat -f "%z bytes" file is what you need. Answer updated.
    – DavidPostill
    Aug 6, 2016 at 9:44
0

For people reaching this topic and willing to stick to ls tool, I would suggest:

\ls -lb

-n (in ls -ln) would be too numerical for my eyes, I'd rather keep user/group readable with -b.

Or more conveniently:

alias lsb='command ls --color -lb' # where "command ls" is a synonym of "\ls"

0

Running "ls" through the full path will show you the file sizes in bytes:

/bin/ls -l

Verified on macOS 10.15 with Bash.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .