How to Display File Size in Human Readable Format (KB, MB, GB) in Linux Terminal
Quick tip to display file size in Linux using the ls command.
You probably already know that you can use ls command with long listing option
-l to show file size in Linux.
But unfortunately, the long listing shows the file size in blocks and that's not of much use to us humans.
The good thing is that you can combine the option
-h to show the file size in a human-readable format.
As you can see, it is better to display file size in a human-readable format.
As you can see, file sizes are now displayed in K (for KB), M for (MB). If the file size is in Bytes, it is not displayed with any suffix. In the above example,
char.sh is 140 Bytes in size.
Did you notice the size of new_dir directory? It is 4 KB. If you use
ls -lh command on directories, it always shows the size of directory as 4.0 K.
You'll have to use du command to get the real size of a directory in Linux.
By default, the block size in most Linux system is 4096 Bytes or 4 KB. A directory in Linux is simply a file with the information about the memory location of all the files in it.
You can force ls command to display file size in MB with the
ls -l --block-size=M
The problem with this approach is that all the files with a size of less than 1 MB will also be displayed with file size of 1 MB.
The ls command also has
-s option to display size. You should combine with
-h to show the file size in human readable form.
Here's the output:
abhishek@handbook:~/tutorial$ ls -sh
4.0K char.sh 4.0K hello.sh 319M wp_ghost_export.zip
4.0K file.txt 4.0K new_dir
4.0K filetype.sh 5.5M wp_ghost_export.json
You can use the -S option of the ls command to sort files by size. This is also helpful in knowing which files take the most space on your system.
You can also use the stat command in Linux to check the file size.
I hope you find this quick tip helpful in seeing the file size in Linux.