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

General support questions
Post Reply
bodisha
Posts: 40
Joined: 2015/06/06 23:55:29

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

Post by bodisha » 2019/08/20 13:40:18

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!
Last edited by bodisha on 2019/08/20 14:19:33, edited 1 time in total.

tunk
Posts: 588
Joined: 2017/02/22 15:08:17

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

Post by tunk » 2019/08/20 13:47:49

1M = 1024*1024

User avatar
TrevorH
Forum Moderator
Posts: 26590
Joined: 2009/09/24 10:40:56
Location: Brighton, UK

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

Post by TrevorH » 2019/08/20 14:00:12

251284320/1024/1024 = 239.64
CentOS 5 died in March 2017 - migrate NOW!
CentOS 6 goes EOL sooner rather than later, get upgrading!
Full time Geek, part time moderator. Use the FAQ Luke

bodisha
Posts: 40
Joined: 2015/06/06 23:55:29

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

Post by bodisha » 2019/08/20 14:26:12

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?

tunk
Posts: 588
Joined: 2017/02/22 15:08:17

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

Post by tunk » 2019/08/20 14:53:53

Not quite:
1k = 1024
1M = 1024*1024
1G = 1024*1024*1024
etc.

User avatar
TrevorH
Forum Moderator
Posts: 26590
Joined: 2009/09/24 10:40:56
Location: Brighton, UK

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

Post by TrevorH » 2019/08/20 16:35:10

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.
CentOS 5 died in March 2017 - migrate NOW!
CentOS 6 goes EOL sooner rather than later, get upgrading!
Full time Geek, part time moderator. Use the FAQ Luke

George99
Posts: 53
Joined: 2013/09/08 17:01:05
Location: Germany

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

Post by George99 » 2019/08/20 17:30:44

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.

Post Reply

Return to “CentOS 7 - General Support”