How To See File Size In Linux – Checking Directory Storage Usage

Linux offers simple terminal commands to check the exact size of any file on your system. If you are wondering how to see file size in linux, you have come to the right place. This guide will walk you through multiple methods, from basic commands to advanced options, so you can quickly find file sizes for any need.

Knowing file sizes is essential for managing disk space, transferring files, or debugging scripts. The Linux command line gives you precise control, but there are also graphical ways if you prefer a visual approach. Let us start with the most common and powerful commands.

How To See File Size In Linux

The most straightforward command to check file size is ls -lh. This lists files in a directory with human-readable sizes. For example, ls -lh shows sizes in kilobytes (K), megabytes (M), or gigabytes (G).

But there is more to it. You can check a single file, multiple files, or even directories. Below, we break down each method step by step.

Using The Ls Command For File Sizes

The ls command is the default tool for listing directory contents. To see file sizes, add the -l (long format) and -h (human-readable) flags.

  1. Open your terminal.
  2. Type ls -lh and press Enter.
  3. Look at the fifth column – that is the file size.

If you want to see hidden files (those starting with a dot), use ls -lah. The -a flag shows all files.

For a specific file, run ls -lh filename.txt. Replace filename.txt with your actual file name. This command shows only that file’s details.

Using The Du Command For Accurate Sizes

The du command (disk usage) gives you the actual disk space used by a file or directory. It is more accurate than ls because it accounts for file system overhead.

To check a single file, type du -h filename.txt. The -h flag makes the output human-readable. For example, du -h report.pdf might show 2.3M report.pdf.

To see sizes of all files in a directory, use du -h without a file name. Add --max-depth=1 to avoid diving into subdirectories: du -h --max-depth=1.

Using The Stat Command For Detailed Info

The stat command provides comprehensive file information, including size in bytes. This is useful for scripting or when you need exact byte counts.

Run stat filename.txt. Look for the line that says “Size:” – it shows the file size in bytes. For example, Size: 4096 means 4 kilobytes.

You can also use stat -c %s filename.txt to output only the size in bytes. This is handy for automation.

Using The Find Command For Batch Checks

If you need to find files of a specific size, the find command is your friend. It searches directories and can filter by size.

To find files larger than 100 MB, run: find /path/to/search -type f -size +100M. Replace /path/to/search with your directory.

To see sizes of all found files, add -exec ls -lh {} \; at the end: find /path -type f -size +100M -exec ls -lh {} \;.

Using The Wc Command For Quick Byte Count

The wc command (word count) can also show file size. Use wc -c filename.txt to get the byte count. This is simple but only gives bytes, not human-readable format.

For a more readable output, combine with numfmt: wc -c filename.txt | numfmt --to=iec. This converts bytes to KB, MB, etc.

Checking File Sizes In Graphical Mode

If you prefer a GUI, most Linux file managers show file sizes by default. Here is how to check in common desktops.

Using Nautilus (Gnome)

Open Nautilus (Files). Navigate to your file. Right-click it and select “Properties.” The size appears in the “Size” field. You can also enable the “Size” column in list view by clicking the gear icon and selecting “Visible Columns.”

Using Dolphin (KDE)

In Dolphin, the file size is shown in the “Size” column by default. If not, go to “View” > “Show Columns” and check “Size.” Right-click a file and choose “Properties” for more details.

Using Thunar (Xfce)

Thunar shows file sizes in list view. Right-click a file and select “Properties” to see the exact size. You can also adjust column visibility under “View” > “Configure Columns.”

Understanding File Size Units

Linux uses binary units by default in most commands. Here is a quick reference:

  • 1 KiB = 1024 bytes
  • 1 MiB = 1024 KiB
  • 1 GiB = 1024 MiB

Some commands like ls -lh use binary units but label them as KB, MB, GB. This can be confusing. For true decimal units, use --si flag with ls: ls -lh --si.

Checking Sizes Of Directories

Directories themselves have a size, but the important number is the total size of their contents. Use du -sh directory_name to see the total disk usage.

For example, du -sh Documents shows the combined size of all files in the Documents folder. The -s flag summarizes, and -h makes it human-readable.

To see sizes of subdirectories, use du -h --max-depth=1. This lists each subdirectory’s size without going deeper.

Comparing File Sizes With Different Commands

Sometimes ls and du give different results. This is normal. ls shows the logical file size, while du shows the actual disk usage. Sparse files or compressed files can cause discrepancies.

For example, a sparse file might show 1 GB with ls but only 10 MB with du. Always use du for real disk usage.

Using Aliases For Faster Access

If you check file sizes often, create an alias. Add this line to your ~/.bashrc file:

alias siz='ls -lh'

Then run source ~/.bashrc. Now typing siz will show file sizes with human-readable format.

You can also create an alias for du -sh: alias dus='du -sh'.

Scripting File Size Checks

For advanced users, scripting can automate size checks. Here is a simple bash script to list files larger than a given threshold:

#!/bin/bash
echo "Files larger than $1 MB:"
find . -type f -size +${1}M -exec ls -lh {} \;

Save it as bigfiles.sh, make it executable with chmod +x bigfiles.sh, and run ./bigfiles.sh 100 to find files over 100 MB.

Common Mistakes And Troubleshooting

Here are a few pitfalls and how to avoid them:

  • Forgetting the -h flag: Without it, sizes appear in bytes, which is hard to read.
  • Using ls on directories: ls -lh directory shows the directory’s metadata size, not its contents. Use du instead.
  • Confusing binary and decimal units: Check if you need --si for decimal units.
  • Not escaping spaces in file names: Use quotes: ls -lh "my file.txt".

Frequently Asked Questions

How Do I See File Size In Linux In Megabytes?

Use ls -lh or du -h. Both show sizes in MB automatically. For example, ls -lh file.txt displays 1.2M if the file is 1.2 megabytes.

What Is The Difference Between Ls And Du For File Sizes?

ls shows the logical size of the file, while du shows the actual disk space used. For most files, they are similar, but for sparse or compressed files, du is more accurate.

How Can I Check File Size In Linux Without Terminal?

Use your file manager. Right-click the file and select “Properties” or “Info.” The size is displayed in the dialog box. This works in Nautilus, Dolphin, Thunar, and others.

How Do I See The Size Of All Files In A Directory In Linux?

Run ls -lh inside the directory to list all files with sizes. For a total size, use du -sh. For sizes of subdirectories, use du -h --max-depth=1.

Why Does Ls Show A Different Size Than Du?

This happens because ls reports the file’s apparent size, while du reports the actual blocks used on disk. Sparse files, compression, or file system overhead can cause differences.

Conclusion

Now you know multiple ways to check file sizes in Linux. The ls -lh command is the quickest for everyday use, while du -sh is best for directories. For scripting, stat and find offer precision and automation.

Practice these commands in your terminal. Over time, they will become second nature. If you ever forget, just remember: ls -lh is your go-to for file sizes. For disk usage, rely on du. With these tools, managing your filesystem becomes easy and efficient.

Remember to use the -h flag for human-readable output. That simple addition saves you from counting zeros. And if you prefer a graphical interface, your file manager has you covered.

File size management is a fundamental skill for any Linux user. Whether you are a beginner or a seasoned admin, these commands will serve you well. Happy file checking!