Page 1 of 1

Why does the "ls -lh" option round down a file size so much?

Posted: 2019/08/20 13:40:18
by bodisha
If I look at the file /usr/sbin/mysqld with "ls -l" or include the options "-s" or "-si" on CentOS 7... it returns with the size of

Code: Select all

[root@centos7lab ~]# ls -l /usr/sbin/mysqld
-rwxr-xr-x. 1 root root 251284320 Dec 21  2018 /usr/sbin/mysqld
[root@centos7lab ~]# ls -ls /usr/sbin/mysqld
245396 -rwxr-xr-x. 1 root root 251284320 Dec 21  2018 /usr/sbin/mysqld
[root@centos7lab ~]# ls -lsi /usr/sbin/mysqld
610480 245396 -rwxr-xr-x. 1 root root 251284320 Dec 21  2018 /usr/sbin/mysqld
[root@centos7lab ~]# ls -lh /usr/sbin/mysqld
-rwxr-xr-x. 1 root root 240M Dec 21  2018 /usr/sbin/mysqld

But when I include the "-h" option to return it in human readable form... It rounds the size down by 10Mb... Which seems like a pretty large discrepancy!

Code: Select all

-rwxr-xr-x. 1 root root 240M Dec 21  2018 /usr/sbin/mysqld
Can someone explain why there's such a big difference? I understand the default block size of the "ls" command is 1000 bytes and the human readable option is 1024 bytes... But even that doesn't explain the difference

THANKS!

Re: Why does the "ls -lh" option round down a file size so much?

Posted: 2019/08/20 13:47:49
by tunk
1M = 1024*1024

Re: Why does the "ls -lh" option round down a file size so much?

Posted: 2019/08/20 14:00:12
by TrevorH
251284320/1024/1024 = 239.64

Re: Why does the "ls -lh" option round down a file size so much?

Posted: 2019/08/20 14:26:12
by bodisha
Thanks for the replies! Just to make sure I'm clear on what you're saying

block size=1024
number of blocks =1024

block size * # of blocks = 1M

Am I understanding this correctly?

Re: Why does the "ls -lh" option round down a file size so much?

Posted: 2019/08/20 14:53:53
by tunk
Not quite:
1k = 1024
1M = 1024*1024
1G = 1024*1024*1024
etc.

Re: Why does the "ls -lh" option round down a file size so much?

Posted: 2019/08/20 16:35:10
by TrevorH
You asked it to show the size in megabytes. It did. But the megabytes it uses are 1024*1024 each (1048675) so your 251 million bytes becomes 240MB.

Re: Why does the "ls -lh" option round down a file size so much?

Posted: 2019/08/20 17:30:44
by George99
It's getting a bit more confusing by the fact the Gnome does use 1M =1000 * 1000 so the results will be different from what ls -lh reports.