Check File Size in Linux Command Line

Learn a thing or two about checking the file size in the Linux command line.
Warp Terminal

How do you check the size of a file in Linux command line?

The simplest way is to use the ls command with -lh option.

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 -h.

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 -h option.

Check file size in Linux with the ls comma
πŸ’‘
Did you notice, I used KiB, MiB, GiB, not KB, MB and GB? These days proper binary notation are KiB (=1024 Bytes), MiB (=1024 KiB) and decimal notation are KB (=1000 Bytes), MB (=1000KB). The ls command shows you the classic binary notation.

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.

Check file size in Linux command line with ls command

Personally, I have always preferred using the long listing option -l. It's more commonly used and I have one less option to remember.

πŸ’‘
You can display file sizes for more than one file at a time. Just use the ls command on the directory instead of the file name.

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 -h anymore.

If you want, GiB, use --block-size=G.

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.

Forcing file sizes in MB or GB have consequences
Changing the block size is not always a good idea

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.

ls command cannot show directory size

To get the directory size, you use the du command (disk utilization) in the following way:

du -sh dirname
Get directory size in Linux

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.

About the author
Abhishek Prakash

Abhishek Prakash

Created It's FOSS 11 years ago to share my Linux adventures. Have a Master's degree in Engineering and years of IT industry experience. Huge fan of Agatha Christie detective mysteries πŸ•΅οΈβ€β™‚οΈ

Become a Better Linux User

With the FOSS Weekly Newsletter, you learn useful Linux tips, discover applications, explore new distros and stay updated with the latest from Linux world

itsfoss happy penguin

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to It's FOSS.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.