Results 1 to 6 of 6

Thread: Which command rounds better file sizes?

  1. #1
    Join Date
    May 2009
    Beans
    266

    Which command rounds better file sizes?

    Which command rounds better the file size?

    Code:
    me@mypc:~$ ls -l /tmp/Screenshot-Zimbra.jpg 
    -rw-r--r-- 1 me me 173417 2011-01-13 13:49 /tmp/Screenshot-Zimbra.jpg
    
    me@mypc:~$ ls -lh /tmp/Screenshot-Zimbra.jpg 
    -rw-r--r-- 1 me me 170K 2011-01-13 13:49 /tmp/Screenshot-Zimbra.jpg
    
    me@mypc:~$ ls -lk /tmp/Screenshot-Zimbra.jpg 
    -rw-r--r-- 1 me me 170 2011-01-13 13:49 /tmp/Screenshot-Zimbra.jpg
    
    
    me@mypc:~$ du -b /tmp/Screenshot-Zimbra.jpg 
    173417    /tmp/Screenshot-Zimbra.jpg
    
    me@mypc:~$ du -h /tmp/Screenshot-Zimbra.jpg 
    172K    /tmp/Screenshot-Zimbra.jpg
    
    me@mypc:~$ du -k /tmp/Screenshot-Zimbra.jpg 
    172    /tmp/Screenshot-Zimbra.jpg
    What is the reason for the difference?
    Does ls show the apparent size?
    Last edited by HotForLinux; January 13th, 2011 at 02:43 PM.

  2. #2
    Join Date
    Feb 2009
    Location
    USA
    Beans
    3,187

    Re: Which command rounds better file sizes?

    They both agree on the bytes. 1 Kb = 1024b in computer terms or 1Kb = 1000b in some other cases. Depending on the definition of Kilo you mag get a discrepancy.

    (BTW I tries couple of files on my machine and they always gave the same result for both du and ls)

  3. #3
    Join Date
    May 2009
    Beans
    266

    Re: Which command rounds better file sizes?

    Quote Originally Posted by 3Miro View Post
    They both agree on the bytes. 1 Kb = 1024b in computer terms or 1Kb = 1000b in some other cases. Depending on the definition of Kilo you mag get a discrepancy.

    (BTW I tries couple of files on my machine and they always gave the same result for both du and ls)
    I don't understand your reply. Both man pages (ls and du) say:

    Code:
    
    -k      like --block-size=1K
    
    -h, --human-readable
            print sizes in human readable format (e.g., 1K 234M 2G)
    
    --si   like -h, but use powers of 1000 not 1024
    Last edited by HotForLinux; January 25th, 2011 at 01:32 AM.

  4. #4
    Join Date
    Apr 2009
    Location
    Midwest, U.S.A.
    Beans
    1,209
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Which command rounds better file sizes?

    Either way, you are comparing apples to oranges...

    Using 1000k or 1024k , one file will be larger or smaller than the other.
    Laptop: Dell Inspiron 8200 - Fedora 13 - Goddard
    Desktop: Self-Built - [Ku, Lu, Xu, U]buntu - Lucid 10.04.3 (LTS)
    Linux User: 498249 / Ubuntu User: 29241

  5. #5
    Join Date
    May 2009
    Beans
    266

    Re: Which command rounds better file sizes?

    Quote Originally Posted by AlphaLexman View Post
    Either way, you are comparing apples to oranges...

    Using 1000k or 1024k , one file will be larger or smaller than the other.
    There's only one file here: Screenshot-Zimbra.jpg

    173417 bytes = 169.3525390625 KB =

    | = 170 KB rounded up
    |= 169 KB rounded down (nearest, but down)

    (taking 1KB = 1024 bytes; traditional notation)

    173417 bytes = 173.417 KiB =

    | = 174 KiB rounded up
    |= 173 KiB rounded down (nearest, but down)

    (taking 1KiB = 1000 bytes; stupid new notation should be with "i")

    But 172?

    The worst thing of all is that neither the input parameters nor the output specify what units are we talking about.
    A COMPLETE MESS .

    Linux could be much easier if hundreds of thousands of details like this one weren't left unpolished.
    Last edited by HotForLinux; June 4th, 2011 at 08:15 PM.

  6. #6
    Join Date
    Feb 2009
    Location
    Dallas, TX
    Beans
    6,616
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Which command rounds better file sizes?

    Code:
    $ ls -l dummy
    -rw-r--r-- 1 user user 173417 2011-06-04 14:36 dummy
    
    $ du --apparent-size -h dummy
    170K    dummy
    From man du:
    --apparent-size
    print apparent sizes, rather than disk usage; although the
    apparent size is usually smaller, it may be larger due to holes
    in (`sparse') files, internal fragmentation, indirect blocks,
    and the like
    What is happening here is that du reports by default how much space is used in the hard drive to store the file. Data is saved in your hard drive by writing complete blocks. Even if your file has 1 byte of usable data, it will use 1 block of disk space.

    This is an easy example. Here you can see that my hard disk uses 4K blocks:
    Code:
    ~$ ls -l dummy2
    -rw-r--r-- 1 user user  1  2011-06-04 14:58 dummy2
    
    $ du -h dummy2
    4.0K    dummy2
    
    $ du --apparent-size -h dummy2
    1       dummy2
    Hope it helps.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •