Glindra
Documentation Index Download
Command Line File Handling and ASCII Tools

Byte Sizes

Parsing Byte Sizes

The directory command has three options called -min_size, -max_size, and -resolution. Each of these accepts a size in bytes as value.

Example
> d '***' -min_size=10mb

Such file sizes can be specified either as a plain number, or with one of these units:
Keyword Description

byte Bytes
kb kilobyte Kilobyte = 1,000 bytes
mb megabyte Megabyte = 1,000,000 bytes
gb gigabyte Gigabyte = 1,000,000,000 bytes
tb terabyte Terabyte = 1,000,000,000,000 bytes
pb petabyte Petabyte = 1,000,000,000,000,000 bytes
eb exabyte Exabyte = 1,000,000,000,000,000,000 bytes


The byte unit keywords may be abbreviated as long as they are unique, typically to two characters.

The default unit is byte, so if you just give a number without any unit, it will be interpreted as that number of bytes.

Note that the size units have their proper scientific base 10 meanings, so that a kilobyte is 1000 bytes, and not 1024 bytes. For example, the option -min_size=10mb specifies files that have a size of 10,000,000 bytes or more.

There must be at least one digit before the size unit, so 1 megabyte is written as 1mb.

Examples (Windows)
> d *** -min=0.5gb
> d *** -min_size=(500 megabyte)
List all files that are 500,000,000 bytes or more in size. The parentheses are required in the second example, because there is a (superfluous) space between the number and the size unit.


> d *** -max=0 -nodir
> d *** -max_size=0 -nodirectory
List all files that are empty (excluding directory files). This can be a useful command.


> d *** -min=1
List all files that are non-empty. Directory files are considered to have a size of 0, so they would not be included in the list.


> d *** -min=987243 -max=987243
List all files that are exactly 987,243 bytes in size.

Printing Byte Sizes

By default, the d directory program prints file sizes in an appropriate byte unit, such as kb, Mb, or Gb.

Which unit is selected depends on how large each file is. This makes the directory listing easier to read.

Example
Directory of e:\work\test\

        2 kb   25-Mar-2004 12:37:14   g13_create.cpp
     1.6 Mb    23-Sep-2004 19:30:11   g13_create.exe
           0   23-Sep-2004 19:30:02   g13_create.lis
         123   23-Sep-2004 19:30:12   g13_create.out
  ----------
     1.6 Mb    total, 4 files

Note how large file sizes extend more to the left than small, just like if the sizes had been output as ordinary numbers. This makes the big files stand out better in a long directory listing. It also makes them sortable with a standard sort program.


We can see this by comparing with how the sizes would look as ordinary numbers:

        2 kb   25-Mar-2004 12:37:14   g13_create.cpp
        1476   25-Mar-2004 12:37:14   g13_create.cpp

     1.6 Mb    23-Sep-2004 19:30:11   g13_create.exe
     1579160   23-Sep-2004 19:30:11   g13_create.exe

           0   23-Sep-2004 19:30:02   g13_create.lis
           0   23-Sep-2004 19:30:02   g13_create.lis

         123   23-Sep-2004 19:30:12   g13_create.out
         123   23-Sep-2004 19:30:12   g13_create.out

Syntax Options For Printing Byte Sizes

These options are used for specifying exactly how file sizes should be written out, when the default format is unsuitable.

In the d directory program, they appear as suboptions to the -size option.

Example
> d -size=nobyte_unit
Suboptions
byte_unit [= keyword] Express file sizes in kb, Mb etc. With -nobyte_unit, they are expressed as plain numbers instead.
radix = n Radix (base) for the number. Default is 10.
binary Radix 2.
octal Radix 8.
hexadecimal Radix 16.
zero_fill Fills out the number with leading zeros
A character like ',' to get numbers written like 1,234
width = n Minimum width in characters. 0 means left adjusted.
hide Hide the item.

byte_unit [= keyword]

Example
> d -size=nobyte_unit


File sizes are written using the special byte size syntax. Sizes above 1 kb are expressed in the appropriate unit, like kb, Mb, Gb etc.

To suppress this, specify nobyte_unit. The suboptions radix, zero_fill, and group_separator implicitly mean nobyte_unit.

Sizes in bytes, or as ordinary numbers

By default, file sizes are printed in an appropriate unit, such as kb or Mb. This is a more readable format than giving the file sizes as ordinary numbers, especially for big files, where it might be difficult to determine the magnitude of the file at a glance.

But sometimes this format is not suitable.

If we are producing a list of files that should be the input to another program, for example, we might want the file sizes as ordinary numbers instead. To achieve this, we use the -size=nobyte option to the directory command.

Example (Windows)

To get the list with file sizes as ordinary numbers:

> d g13* -files -size=nobyte 
        1476   e:\work\test\g13_create.cpp
     1579160   e:\work\test\g13_create.exe
           0   e:\work\test\g13_create.lis
         123   e:\work\test\g13_create.out


We can compare this with the default format:

> d g13* -files -size
        2 kb   e:\work\test\g13_create.cpp
     1.6 Mb    e:\work\test\g13_create.exe
           0   e:\work\test\g13_create.lis
         123   e:\work\test\g13_create.out

The -size option without any keyword adds the file sizes written on the byte size format to the -files listing.

Specifying a smallest unit

You can write byte_unit=keyword to specify the smallest unit that should be used. The valid keywords are the ones listed under Parsing Byte Sizes, like mb or gigabyte.


Example (Windows)

With file sizes in megabytes...

> d /size=(byte_unit=megabyte) 

Directory of e:\work\html\

0.1 Mb 23-Sep-2004 12:36:01 filespec.html
0.7 Mb 23-Sep-2004 12:36:37 old_glindra.html
0.1 Mb 23-Sep-2004 12:36:58 old_shell.html
----------
0.8 Mb total, 3 files


...and with file sizes in gigabytes:

> d /size=(byte=gig) 

Directory of e:\work\html\

0.1 Gb 23-Sep-2004 12:36:01 filespec.html
0.1 Gb 23-Sep-2004 12:36:37 old_glindra.html
0.1 Gb 23-Sep-2004 12:36:58 old_shell.html
----------
0.1 Gb total, 3 files
Note how all file sizes are always rounded upwards, but how the total is calculated on the actual numbers before rounding.

Other Suboptions

See Num Syntax Options for a full description of the remaining suboptions.

If you specify one of the numeric options radix, zero_fill, or group_separator, this implicitly means nobyte_unit. The file sizes will be printed as regular numbers.

Examples (Windows)

Print file sizes as ordinary numbers, with a comma between every third digit.

> d /size=group

Directory of e:\work\

       1,604   24-Nov-2004 19:43:49   glindra.cpp
       3,950   27-Nov-2004 13:55:29   glindra.hpp
   1,694,240    1-Dec-2004 00:10:59   libglindra.a
  ----------
   1,699,794   total, 3 files


Print file sizes as hexadecimal numbers, with a dash between every third hexadecimal digit.

> d -size=(hex group_separator='-')

Directory of e:\work\

         644   24-Nov-2004 19:43:49   glindra.cpp
         f6e   27-Nov-2004 13:55:29   glindra.hpp
     19d-a20    1-Dec-2004 00:10:59   libglindra.a
  ----------
     19e-fd2   total, 3 files


(Author's note: If you are wondering why anybody would want to do that, or any other weird format, my answer is that probably nobody does. But the flexibility in how numbers can be written came for free with the package that was used in the implementation, so it's available in case there are in fact some real world uses for it.)