You probably already know that you can use the ls command with the -l long-list option to show the file size on Linux.
Unfortunately, the long list shows the size of the file in chunks, and for us humans this is of little use.
The good news is that you can combine the lc -h option to show the file size in a readable format.
As you can see, the file size is best displayed in a 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 a suffix. In the above example, char.sh is 140 bytes in size.
Have you noticed the size of the new_dir directory? This is 4KB. If you use the ls -lh command for directories, it always shows the directory size as 4.0K.
You will need to use the du command to get the actual size of the directory on Linux.
The default block size on most Linux systems is 4096 bytes, or 4KB. A directory in Linux is simply a file with information about the location in memory of all the files in it.
You can force the ls command to display the file size in MB using the –block-size flag.
ls -l --block-size=M
The problem with this approach is that all files less than 1 MB in size will also be displayed with a file size of 1 MB.
The ls command can also -s display size. You must combine with, -h to show the file size in human readable form.
Here’s the result:
[email protected]:~/tutorial$ ls -sh total 324M 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 also use stat command on Linux to check file size.
We hope this quick tip helps you see file size on Linux.