Linux ls Command File Size Formatting

The “ls” command is utilized to list out the files & subdirectories in a current directory. By default, ls displays the files and directories in a directory in a list format, with each file or directory on a new line.

This guide explains all possible ways to display the file size in different formats using the “ls” command. The roadmap of this guideline is mentioned-below:

Let’s start the “ls” command with a Long format.

How to Show File Sizes in Long Format?

To display the directories and files in a “long” format, the “-l” option is used. It shows additional information about each file or directory (such as its size, permissions, and modification time). To do so, execute the below script:

$ ls -l

The output shows the size of each file in a bytes format with the creation date and time.

How to Show File Sizes in “Human-Readable” Format?

Users can use the option “-h” to display the sizes of files in “readable” form. It means that the file sizes are displayed in a more easily readable format, such as in kilobytes (KB), megabytes (MB), or gigabytes (GB).

To display all content in the current directory in a long format, execute the following command:

$ ls -lh

It displays all content in the current directory, and the size of all files is displayed in a human-readable representation.

How to Display the File Sizes in Powers of 1000?

You can also use the “–si” option to display the file sizes in “SI” format, which means that the file sizes are displayed in powers of 1000 (e.g., KB, MB, GB) rather than powers of 1024 (which is the default):

$ ls -l --si

The output returns the list of all content in the current directory, and the size of each file is displayed in SI format.

How to Display Customize File Size Format?

Users have the option to display the file by specifying the block size. In our case, the “M” option is used that represents the “Megabyte”:

$ ls -l --block-size=M

The output returns the file sizes in “Megabyte” format.

Note: Users can use different block sizes such as “G for GigaBytes”, “M for Megabytes” and “T for Terabytes”.

How to Combine Multiple Format Options?

You can combine these options to display the files and directories in a long format, with the file sizes displayed in human-readable or SI format:

$ ls -lh --si

This would display the list of directories and files in the current directory, with each file or directory on a new line, and the size of each file displayed in understandable SI format.

Note: Click here to read our detailed tutorial on ls command usage in Linux.

Conclusion

In Linux, the “ls” command works with “-l” and “-h” options to display the list of file sizes in a long and human-readable format, respectively. Additionally, users can utilize the “block size” option by specifying kilobytes (KB), megabytes (MB), or gigabytes (GB). This article has explained all possible ways to list the file in different formats.