How do you check the size of a file in Linux command line?
The simplest way is to use the ls command with
ls -lh filename
Here's an example:
abhishek@itsfoss:~$ ls -lh sample.txt -rw-rw-r-- 1 abhishek abhishek 14K Oct 12 11:38 sample.txt
As you can see in the example above, the size of the
sample.txt file is 14K.
Let's see it a bit more in detail.
Get file size with ls command
The ls command lists the contents of a directory. But with the long listing option
-l, it shows the file properties as well, file size being one of them.
But by default, the file size is in bytes and it's not easy to understand that. This is why you should combine with the human-readable option
ls -lh filename
It will start showing file sizes in proper units like KiB, MiB, GiB etc.
In the example below, you can see that the file size was first displayed as 13506 with
-l option only and 14K with the
The dedicated size option for ls command (but does anyone use it?)
Actually, the ls command has a dedicated option
-s for showing the file size in blocks. You can combine it with human-readable option
-h of course.
ls -sh filename
In this case, it will only show the file size with the filename.
Personally, I have always preferred using the long listing option
-l. It's more commonly used and I have one less option to remember.
Force ls command to show file size in KB, MB or GB (not recommended)
First, it's not KB, MB or GB but KiB, MiB and GiB. I explained it above.
You can force the ls command to show file size in your favorite unit in this way:
ls -l --block-size=M
You don't need the human-readable option
If you want, GiB, use
There is a major problem with this approach. It works fine for smaller units (file size in GB but you want it in MB) but not for smaller file size and bigger unit.
In the example below, the sample.txt file of size 16K is shown as 1G if the block-size is changed to G.
That's because the ls command calculates the size based on block sizes. Since you defined the minimum unity as 1G, it will show the file size as 1G at the least.
What about the directory size?
The ls command cannot (correctly) show you the size of a folder. It always displays as 4K (block size). That's because, technically, a directory is a file that has information on the location of other files in the memory.
To get the directory size, you use the du command (disk utilization) in the following way:
du -sh dirname
You may also use the stat command to get the file size but somehow I feel more comfortable using the ls command.
I hope this basic Linux command tip helped you check file sizes in Linux.