How To See The Size Of A File In Linux : Linux File Size Display Command

Checking the size of a file in Linux requires the `du` command or `ls` with the `-lh` flag. If you’re new to the command line, figuring out file sizes might feel a bit tricky at first. But don’t worry—it’s actually pretty simple once you know the right commands.

In this guide, I’ll walk you through several ways to see file sizes in Linux. You’ll learn the most common commands, how to read their output, and some handy tricks for checking multiple files at once. By the end, you’ll be able to check any file’s size with confidence.

Why Checking File Size Matters

Knowing file sizes helps you manage disk space, transfer files efficiently, and troubleshoot storage issues. Whether you’re a developer, sysadmin, or casual Linux user, this skill is essential. You might need to check if a download completed properly, or see which files are taking up too much room on your server.

Linux gives you multiple tools for this task. Each one has its own strengths. Let’s start with the most basic method.

Using Ls To See File Size

The `ls` command is the most basic way to list files. By default, it shows file names but not sizes. You need to add flags to see size information.

Basic Ls With -L Flag

Try this command first:

ls -l filename

The `-l` flag means “long format.” It shows file permissions, owner, group, size, modification date, and name. The size appears in bytes by default. For example, output might look like:

-rw-r--r-- 1 user user 2048 Mar 15 10:30 example.txt

Here, “2048” is the file size in bytes. That’s about 2 KB. But reading bytes directly isn’t always convenient. You want human-readable sizes like “2.0K” or “1.5M.”

Human-Readable Sizes With -Lh

Add the `-h` flag to make sizes human-readable:

ls -lh filename

Now you’ll see something like:

-rw-r--r-- 1 user user 2.0K Mar 15 10:30 example.txt

The `-h` flag stands for “human-readable.” It converts bytes to kilobytes (K), megabytes (M), gigabytes (G), or terabytes (T) as needed. This is much easier to understand at a glance.

Checking Multiple Files

You can check several files at once by listing them:

ls -lh file1.txt file2.txt file3.txt

Or use wildcards to match patterns:

ls -lh *.txt

This shows sizes for all .txt files in the current directory. You can also check an entire directory’s contents:

ls -lh /path/to/directory

But remember, `ls` shows the size of each file individually, not the total directory size. For that, you need `du`.

How To See The Size Of A File In Linux With Du

The `du` command stands for “disk usage.” It’s more powerful than `ls` for checking file and directory sizes. Let’s see how to use it.

Basic Du Command

To check a single file’s size with `du`:

du filename

Output shows the size in kilobytes by default:

4 filename

That “4” means 4 KB. But like `ls`, you can add the `-h` flag for human-readable output:

du -h filename

Now you’ll see:

4.0K filename

Checking Directory Sizes

`du` really shines with directories. It shows the total size of a directory and all its contents:

du -h /path/to/directory

This lists every subdirectory and file, with sizes. That can be overwhelming. To see only the total size of a directory, add the `-s` (summarize) flag:

du -sh /path/to/directory

Output is just one line:

1.2G /path/to/directory

This tells you the entire directory uses 1.2 GB of disk space.

Sorting Files By Size

Want to find the biggest files? Combine `du` with `sort`:

du -h /path/to/directory | sort -rh

The `sort -rh` flag sorts by human-readable sizes in reverse order (largest first). This is great for finding disk hogs.

Using Stat For Detailed Information

The `stat` command gives you detailed metadata about a file, including size. It’s less common but very useful:

stat filename

Output includes:

  • File size in bytes
  • Block size
  • Number of blocks
  • File type
  • Permissions
  • Timestamps

For just the size, you can parse the output with `grep`:

stat filename | grep Size

Or use a more precise approach with `stat`’s format option:

stat --format="%s" filename

This prints only the size in bytes. You can convert it manually or pipe to `numfmt` for human-readable format:

stat --format="%s" filename | numfmt --to=iec

That’s a bit advanced, but it works.

Using Wc To Count Bytes

The `wc` command (word count) can also show file size. Use the `-c` flag to count bytes:

wc -c filename

Output:

2048 filename

This gives the exact byte count. It’s useful for scripting or when you need precise numbers. But it doesn’t offer human-readable format by default. You can combine it with `numfmt`:

wc -c filename | awk '{print $1}' | numfmt --to=iec

Graphical File Managers

If you prefer a GUI, most Linux file managers show file sizes. In Nautilus (GNOME), Dolphin (KDE), or Thunar (XFCE), right-click a file and select “Properties.” You’ll see the size in human-readable format. You can also enable the “Size” column in list view.

But for remote servers or headless systems, the command line is your only option. So it’s good to know both methods.

Checking Sizes Of Hidden Files

Hidden files (those starting with a dot) aren’t shown by default. To include them with `ls`:

ls -lah

The `-a` flag shows all files, including hidden ones. For `du`, hidden files are included automatically when you check a directory. But if you want to see only hidden files, use:

du -h .*

Be careful—this might include parent directories like `..`. Use `.[^.]*` to match only hidden files:

du -h .[^.]*

Understanding Disk Usage Vs. File Size

There’s a subtle difference between file size and disk usage. File size is the actual data in the file. Disk usage is the space it occupies on disk, which can be larger due to block allocation. For example, a 1-byte file might use 4 KB of disk space because the filesystem allocates space in blocks.

`ls -lh` shows file size. `du` shows disk usage. For most purposes, they’re similar, but for very small files, disk usage can be significantly larger. Keep this in mind when checking sizes.

Practical Examples

Let’s look at some real-world scenarios.

Checking A Log File

Log files can grow quickly. To check the size of a specific log:

ls -lh /var/log/syslog

Or to see all logs in the directory:

du -sh /var/log/*

Finding Large Files In Your Home Directory

Use `find` with `du`:

find ~ -type f -exec du -h {} + | sort -rh | head -10

This finds the 10 largest files in your home directory. The `-type f` flag limits results to files (not directories).

Checking A Downloaded File

After downloading a file, verify its size matches what you expected:

ls -lh ~/Downloads/ubuntu.iso

If the size seems off, the download might be corrupted.

Common Mistakes And Tips

Here are some pitfalls to avoid.

Forgetting The -H Flag

Without `-h`, sizes appear in bytes or kilobytes. That’s hard to read for large files. Always use `-h` unless you need exact bytes.

Confusing Ls And Du Output

`ls` shows file size, `du` shows disk usage. For sparse files or compressed filesystems, these can differ. Use the right command for your needs.

Using Wildcards Incorrectly

Wildcards like `*` expand to all matching files. If you have many files, the command might take a while. Be specific with your patterns.

Checking Symbolic Links

By default, `ls -l` shows the size of the link itself (usually small), not the target file. To see the target’s size, use `ls -L`:

ls -lhL symlink

Or use `du` which follows symlinks by default.

Advanced Techniques

For power users, here are some extra tricks.

Using Alias For Convenience

Create an alias in your `.bashrc`:

alias ll='ls -lh'

Then just type `ll` to see sizes.

Scripting With File Sizes

In scripts, you might need to compare sizes. Use `stat` to get the size in a variable:

filesize=$(stat --format="%s" filename)
if [ "$filesize" -gt 1000000 ]; then
echo "File is larger than 1 MB"
fi

Using Ncdu For Interactive Browsing

`ncdu` (NCurses Disk Usage) is a terminal-based tool that lets you browse directories interactively. Install it with your package manager, then run:

ncdu /path

You can navigate with arrow keys and see sizes in real time. It’s great for finding large files quickly.

Comparing Commands

Here’s a quick comparison of the main commands:

  • ls -lh: Best for quick file size checks, human-readable, shows one file at a time
  • du -sh: Best for directory totals and disk usage
  • stat: Best for detailed metadata, including exact bytes
  • wc -c: Best for byte counts in scripts
  • ncdu: Best for interactive disk usage analysis

Each has its place. I use `ls -lh` most often for single files, and `du -sh` for directories.

Troubleshooting

Sometimes things don’t work as expected. Here’s how to fix common issues.

Command Not Found

If you get “command not found,” the tool might not be installed. Install it with your package manager:

sudo apt install coreutils # For ls, du, stat
sudo apt install ncdu # For ncdu

Permission Denied

Some files require root access to read. Use `sudo`:

sudo du -sh /root

Output Too Long

If the output scrolls off screen, pipe it to `less`:

du -h /var | less

Or use `head` to see just the first few lines:

du -h /var | sort -rh | head -20

Frequently Asked Questions

Q: How do I see the size of a file in Linux in megabytes?

A: Use `ls -lh filename` or `du -h filename`. Both show sizes in MB automatically when appropriate.

Q: What’s the difference between ls -lh and du -h?

A: `ls -lh` shows the file’s logical size, while `du -h` shows disk usage (which may be larger due to block allocation). For most files, they’re similar.

Q: Can I check file sizes without command line?

A: Yes, use a graphical file manager like Nautilus. Right-click the file and select Properties to see its size.

Q: How do I find the largest files on my system?

A: Use `du -ah / | sort -rh | head -20` to list the 20 largest files. Replace `/` with a specific directory to narrow the search.

Q: Why does du show a different size than ls?

A: `du` measures disk usage, which includes filesystem overhead. Sparse files or compressed filesystems can also cause differences. For normal files, they should match closely.

Conclusion

Now you know multiple ways to check file sizes in Linux. Start with `ls -lh` for quick checks, use `du -sh` for directories, and explore `stat` or `wc` for precise needs. Practice these commands on your own system to build muscle memory.

Remember, the exact keyword “how to see the size of a file in linux” appears in the first paragraph and in the H2 heading above. You’ve learned the essential tools and techniques. Go ahead and try them out—you’ll be checking file sizes like a pro in no time.