Obtaining file information on a regular basis is critical because, before assigning extra space, you must determine which files are using up the most space and which files are unnecessary and taking up a lot of space. In Linux, we have a variety of tools to check this. In this article, we'll look at a few different ways to verify the file size in Linux using the command line.
Quick Answer: Linux Commands to Check File Size
If you just need the command and you're out the door, here's what you came for:
ls -lh file— human-readable file size (KB, MB, GB)stat -c %s file— exact size in bytesdu -sh file_or_directory— disk usage in a friendly formatdu -h --max-depth=1 directory— size of each subfolder, one level deep
One small but important note: ls and stat report the file's logical size, while du reports the actual disk usage. Those two numbers aren't always the same, and I'll explain why further down.
What is File and File System in Linux
A file is a container for keeping any data in a computer system on a hard disk. Computer files have many of the same characteristics as paper documents in the library and office folders. There are many distinct sorts of files, including data files, text files, media files, and binary files, all of which hold different data types. Files can be saved on optical discs, hard drives, or other kinds of feasible storage media in a computer operating system.
If you're new to the operating system itself, start with what is Linux to understand the foundation.
The Linux file system has a ranked file structure because it has a root directory and subdirectories. The root directory has all of the other directories. Normally, a partition has just one file system, but it might have many. A file system is constructed in such a way that it can handle and store non-volatile data. Every file system needs a namespace, which is the technology and architecture that will be used to store the files. The namespace specifies the naming procedure, file name length, or a subset of characters that can be used in the file name. It also shows how files on a memory segment are organized logically, such as using directories to organize certain files. Once a namespace has been formed, a metadata description for that specific file must be defined. An API (application programming interface) is required to communicate with file system components such as files and directories. Creating, deleting, and copying files are all made easier using API.
Different ways to get file size in Linux
ls command — Quick File Listing and Size Check
The 'ls' command is perhaps one of the most often used commands on the command line in Linux. It means "to list," as in "to list the files and folders from my current location." The man page for 'ls' will provide you with a wide variety of options that you can use with this command.
For a deeper dive into all the flags and formatting tricks, check the ls command in Linux guide.
ls -l file
The -l option is used to get the size of the specified file.
ls -l *
The -l option is used to get the size of all the files in the current directory.
ls -al *
The -al option is used to get the size of all the files, including hidden files in the current directory.
ls -h -l file
The -h option prints human-readable sizes of the files — automatically choosing KB, MB, or GB.
Output looks like this:
-rw-r--r-- 1 sam sam 1.4M Nov 12 09:22 report.log
The fifth column is the size — 1.4M here. Without -h, you'd see 1468291.
stat command — Exact File Size in Bytes
stat reads the file's inode metadata. It's the most precise tool when you need an exact byte count — for scripts, transfer verification, or matching checksums.
stat report.log
The plain stat output gives you the size, blocks allocated, inode number, permissions, and the access/modify/change timestamps.
stat -c %s report.log
When you need just the byte count and nothing else. Output: 1468291. Perfect for scripting.
You can also format multiple fields:
stat -c '%n %s bytes' report.log
That returns report.log 1468291 bytes. Clean, scriptable, and unambiguous.
du command — Disk Usage of Files and Directories
The command du is used to obtain information about the disc utilization of specific files and folders. It works best with certain folders and has a lot of options for customizing the output to match your requirements. It usually gives the detail of sizes in terms of blocks.
If you want broader storage analysis, see our guide on Linux disk usage commands. For working with compressed archives, you may also find our guide on how to unzip and extract files in Linux helpful.
du -h path
Print file sizes in human-readable form (K, M, G).
du -s path
Get memory allocated summary of the file or the directory.
Getting file sizes in blocks
The block parameter can be used to provide a block size for du for the current operation. To determine the actual sizes of the directories and files using a one-byte block size, use the following command:
du --block=1
If you wish to utilize a one-megabyte block size, use the below command:
du -m
Controlling depth with du
du -d depth
Using the -d (max depth) option and a depth value as an input, you may instruct du to list the directory tree to a specific depth. This prints directories and files' details in tree form starting from the root directory.
du -h --max-depth=1 /home/sam
When a directory is huge and you want to know which subfolder is to blame, this gives you a tidy list — each immediate subdirectory with its total. Crank --max-depth=2 to go one level deeper.
du -ah /etc | sort -hr | head
By default, du totals directories. Add -a and it lists individual files too. This one-liner shows the largest files and folders inside /etc, sorted descending.
How to Check Directory Size in Linux
Here's where beginners trip up. Running ls -lh on a directory shows the size of the directory entry itself (usually 4K), not the total of what's inside. For real folder totals, you want du.
du -sh /var/log
The -s means "summary" (one line), -h is human-readable. Output:
238M /var/log
That's the total disk usage of everything inside /var/log, hidden files included.
How to Show File Size in Bytes, KB, MB, or GB
Different jobs need different units. Here's how to control the output.
du -b file— bytes (exact)du -k file— kilobytesdu -m file— megabytesdu -h file— auto-formatted (K/M/G)
ls -lh picks the appropriate unit automatically. If you want powers of 1000 instead of 1024, use ls -l --si report.log.
How to Find the Largest Files in Linux
When a server runs out of disk and you need to know what's eating it, these are the commands. If you're looking for a specific file, check our guide on how to find a file in Ubuntu.
find . -type f -exec du -h {} + | sort -hr | head -10
That command scans the current directory, lists every file with its size, sorts descending by human-readable size, and shows the top 10. Want a tour of the search syntax itself? The find command in Linux guide breaks it down.
find . -type f -printf '%s %p\n' | sort -n | tail -10
If you prefer raw bytes (GNU/Linux only), this prints size path for every file, sorts numerically, and shows the last 10 — which are the biggest.
Sorting options:
sort -n— numeric ascending (smallest first)sort -nr— numeric descendingsort -h— human-numeric (understands "1K", "2M", "3G")sort -hr— human-numeric descending
File Size vs Disk Usage in Linux
Now here's the thing that confuses pretty much everyone the first time: ls says a file is 100 bytes, but du says it uses 4 KB. Who's lying? Neither. They're measuring different things.
The logical size (what ls and stat show) is the file's actual content length in bytes. The allocated size (what du shows) is how many disk blocks the filesystem reserved for it. Filesystems allocate space in fixed-size chunks called blocks — usually 4 KB on ext4. So a 100-byte file still consumes one 4 KB block.
Sparse files go the other direction. They have "holes" — regions of zero that aren't actually written to disk. A virtual machine disk image might report 50 GB logical size but only use 8 GB on disk. ls shows 50 GB, du shows 8 GB. Both are correct.
You can check your filesystem's block size with:
stat -fc %s /
Most modern Linux filesystems use 4096 bytes (4 KB). That's the smallest unit any file occupies.
Common Errors and Tips
ls vs du vs stat output differences
If du and ls disagree, it's almost always because of block allocation or sparse files. Not a bug. If you really need consistency, pick one tool and stick to it for that workflow.
Why hidden files affect totals
du -sh ~ includes hidden files (anything starting with a dot). But ls won't show them unless you add -a. So if your manual count of files doesn't match du, hidden files are usually the culprit. To total only hidden items at the top level:
du -sh .[!.]* 2>/dev/null
Permission denied
Running du on system directories as a regular user spits out Permission denied for everything it can't read — and skews the total downward. Either run with sudo, or silence the errors:
du -sh /etc 2>/dev/null
Just remember: silencing errors hides the fact that your total is incomplete.
Path and wildcard issues
Wildcards are expanded by the shell, not by the command. So du -sh *.log will fail if there are no .log files in the current directory. Always check your working directory.
If you're new to navigating the shell, our roundup of basic Linux command line commands is a good starting point.
A Note on Testing and Sources
Every command in this guide was tested on Ubuntu 22.04 and AlmaLinux 9 with GNU coreutils. Behavior is essentially identical on Debian, CentOS Stream, and Rocky Linux. If you're on macOS or BSD, the BSD versions of ls, du, and stat use different flags — particularly stat -f instead of stat -c. When in doubt, the man page is your friend: man du, man ls, man stat.
Conclusion
In this article, we looked at how to get the file size in UNIX-like operating systems using a variety of command-line utilities. We went through the ls command and its different parameters before moving on to the stat command for exact byte counts, and the du command, which is used to determine the disc usage of certain files or directories.
Three commands cover 95% of what you'll ever need to do. ls -lh for a quick peek, stat when you need exact bytes, du -sh for anything involving directories or disk pressure. The find + sort combo handles the rest — finding the biggest offenders when storage runs short.
If you remember nothing else: ls and stat show file size, du shows disk usage. That single distinction will save you hours of confusion the next time the numbers don't match. Ready to put these commands into practice? Check out our Linux VPS plans for a full environment to test and manage your files, or explore our Linux hosting solutions for production-ready deployments.
People also read:
I'm fascinated by the IT world and how the 1's and 0's work. While I venture into the world of Technology, I try to share what I know in the simplest way with you. Not a fan of coffee, a travel addict, and a self-accredited 'master chef'.