How to view file size/details from ls command in Unix

The ls command at its most basic form displays the files and directories located in your current directory:

$ ls
Desktop    Downloads  my_script  Public     test_file
Documents  Music      Pictures   Templates  Videos
$ 

I always find it difficult to digest the filesize from the ‘ls -al’ command. For instance, after ls -al, the output gives me filesize in bytes. Gosh, then I have to start calculating it by taking the last 4 digits, slowly counting upwards like 1K, 10K, 100K, 1MB, 10MB, 100MB, and so on and so forth.

For instance, this output:

-rw-r--r-- 1 walrus dba 137207094 Jul 8 23:12 config.2008032519.s

137207094 is how much? going with my method of counting upwards, it gives me 137MB roughly. Is it correct? WRONG. Hell wrong. The above are bits only. Bear in mind, 1 KB = 1024 bits, 1 MB = 1024 KB, and so on and so forth.

1 bit = a 1 or 0 (b)
4 bits = 1 nybble (?)
8 bits = 1 byte (B)
1024 bytes = 1 Kilobyte (KB)
1024 Kilobytes = 1 Megabyte (MB)
1024 Megabytes = 1 Gigabyte (GB)
1024 Gigabytes = 1 Terabyte (TB)

The correct calculation is:

137207094 / 1024 (bits) / 1024 (KB) = 130.8 MB

But we have an option in ls now which can give us the file size directly in a human-readable format. For instance, consider the example given below of old and new ways of running the ls command:

Old Way

In old days, we use the command ‘ls -al’ to list the files with their sizes.

$ ls -al
total 270388
drwxr-xr-x 2 walrus dba 1024 Jul 8 23:14 .
drwxr-xr-x 11 walrus dba 512 Jun 17 01:49 ..
-rw-r--r-- 1 walrus dba 137207094 Jul 8 23:12 config.2008032519.s
-rw-r--r-- 1 walrus dba 451989 Jul 8 23:12 config.2008032519.split0.bz

cons: hard to read filesize and output distorted.

New Way

In new way we can list the files with human readable sizes, as shown below:

$ ls -alh
total 269060
drwxr-xr-x 2 flexpm dba 1.0K Jul 8 23:12 .
drwxr-xr-x 11 flexpm dba 512 Jun 17 01:49 ..
-rw-r--r-- 1 flexpm dba 131M Jul 8 23:12 config.2008032519.s
-rw-r--r-- 1 flexpm dba 441K Jul 8 23:12 config.2008032519.split0.sm.gz

pros:

  • more readable format in terms of file size
  • contents are properly aligned.

cons:
– need to type extra ‘h’ at the end of ls command

Related Post