How To Find File Size In Linux : Using Ls Lh Command Syntax

Linux provides several built-in commands to check how much disk space a file occupies. If you’re new to the system or just need a quick refresher, learning how to find file size in linux is a fundamental skill that helps you manage storage and avoid surprises. This guide walks you through every practical method, from simple one-liners to advanced options for directories and hidden files.

Whether you’re troubleshooting a full disk or just curious about a single document, these commands will give you clear, human-readable output. Let’s start with the most common tools and build up to more powerful techniques.

How To Find File Size In Linux

The ls command is the first thing most users type. It lists files and folders, but by default it shows sizes in bytes. That’s not always helpful when you’re dealing with large files. You can make it more readable with the -lh flag.

Open your terminal and navigate to the directory containing the file. Then run:

ls -lh filename.txt

This displays the file size in kilobytes (K), megabytes (M), or gigabytes (G). The -l flag gives a long listing format, and -h makes it human-readable. For example, you might see -rw-r--r-- 1 user user 1.2M Mar 15 10:30 filename.txt. The size is right there, easy to read.

If you want to see sizes for all files in the current directory, just use ls -lh without a filename. You’ll get a neat table with permissions, owner, size, and modification date. This is perfect for a quick overview.

One small tip: if you’re looking at a directory, ls -lh shows the directory’s metadata size (usually 4K), not its contents. For actual folder sizes, you’ll need a different command—we’ll cover that soon.

Using The Stat Command For Detailed Info

The stat command gives you more than just file size. It shows timestamps, inode number, block count, and more. It’s great when you need precise details for scripting or troubleshooting.

Run this to check a single file:

stat filename.txt

Look for the line that says Size:. It will show the exact number of bytes. For example, Size: 1234567 means about 1.2 MB. If you want human-readable output, add the --format option:

stat --format='%s' filename.txt

That prints only the size in bytes. Combine it with numfmt to convert to KB or MB:

stat --format='%s' filename.txt | numfmt --to=iec

This gives you something like 1.2M. It’s a bit more typing, but very precise. I use stat when I need to verify file integrity or check block sizes.

The Du Command For File And Folder Sizes

du stands for “disk usage.” It’s the go-to command for finding the size of directories, but it works on individual files too. The basic syntax is:

du -h filename.txt

This prints the file size in human-readable format. For a directory, it shows the total size of all contents recursively. For example:

du -sh /home/user/Documents

The -s flag summarizes the total, so you don’t see every subfolder. The -h flag makes it readable. This is perfect for checking how much space a project folder uses.

You can also use du to find the largest files in a directory. Combine it with sort and head:

du -ah /home/user | sort -rh | head -10

This lists the top 10 largest files and folders. The -a flag includes files, not just directories. The -r flag sorts in reverse order (largest first). It’s a lifesaver when cleaning up disk space.

Using The Wc Command For File Size

The wc command is usually for counting lines, words, and characters, but it can also show file size. Use the -c flag to get the byte count:

wc -c filename.txt

This prints something like 1234567 filename.txt. The number is the size in bytes. It’s not human-readable by default, but you can pipe it to numfmt:

wc -c < filename.txt | numfmt --to=iec

This gives you a clean output like 1.2M. I find wc useful in scripts because it’s fast and simple. It doesn’t require extra options for basic size checks.

One downside: wc doesn’t work on directories. If you try wc -c myfolder, it will give an error or count the directory entry itself (usually 4096 bytes). Stick to files with this command.

Finding File Sizes With Find Command

The find command is powerful for searching files by size. You can locate all files larger than a certain threshold or within a range. This is great for system administration or cleanup tasks.

To find files larger than 100 MB in your home directory:

find /home/user -type f -size +100M -exec ls -lh {} \;

This lists each file with its human-readable size. The -type f ensures you only get files, not directories. The -size +100M means “greater than 100 megabytes.” You can use k, M, or G for units.

To find files between 50 MB and 200 MB:

find /home/user -type f -size +50M -size -200M -exec ls -lh {} \;

This is incredibly usefull when you’re trying to identify space hogs. You can also use -exec du -h {} \; for a different output format. The find command is a bit verbose, but it’s worth mastering.

Graphical Tools For File Size

If you prefer a visual interface, Linux has several GUI tools. They’re not commands, but they can show file sizes quickly. Tools like Baobab (Disk Usage Analyzer) or QDirStat give you a tree map or pie chart of your disk usage.

To install Baobab on Ubuntu or Debian:

sudo apt install baobab

Then launch it from the applications menu. It scans your home directory and shows which folders are largest. You can click into any folder to see file sizes. It’s intuitive and requires no terminal knowledge.

For a more lightweight option, try ncdu (NCurses Disk Usage). It runs in the terminal but has a interactive interface:

sudo apt install ncdu

Then run ncdu /home/user. You can navigate with arrow keys and delete files directly. It’s fast and doesn’t need a desktop environment. I use it on servers all the time.

Checking Hidden Files And Dot Directories

Hidden files (those starting with a dot) are often overlooked. They can take up significant space, especially in your home directory. Commands like ls -lh don’t show them by default. Use ls -lha to include hidden files:

ls -lha

The -a flag shows all files, including . and ... You’ll see sizes for .bashrc, .config, and other dot files. For directory sizes, use du -sh .*:

du -sh .*

This shows the size of each hidden directory. Be careful: .* includes .. (parent directory), which might cause duplicate counting. A safer pattern is .[!.]* to exclude . and ..:

du -sh .[!.]*

This gives you a clean list of hidden folders. I’ve seen users reclaim gigabytes just by cleaning out old .cache or .thumbnails directories.

Using The Lsblk And Df Commands For Block Devices

Sometimes you need to check the size of a partition or disk, not just a file. The df command shows disk space usage for mounted filesystems:

df -h

This lists all mounted partitions with their total size, used space, and available space. It’s great for a system-wide view. For block device details, use lsblk:

lsblk

This shows devices like /dev/sda1 with their sizes in a tree format. Add -o SIZE for more detail. These commands don’t show individual file sizes, but they help you understand where space is going at the partition level.

If you’re investigating a full disk, start with df -h to see which partition is full. Then drill down with du or find to locate the culprit files.

Scripting File Size Checks

You can automate file size checks with simple bash scripts. For example, to list all files in a directory larger than 1 MB:

#!/bin/bash
for file in /home/user/*; do
    if [ -f "$file" ]; then
        size=$(stat --format='%s' "$file")
        if [ "$size" -gt 1048576 ]; then
            ls -lh "$file"
        fi
    fi
done

This script loops through each file, checks its size in bytes, and prints it if larger than 1 MB (1048576 bytes). You can modify the threshold or output format. Save it as check_size.sh, make it executable with chmod +x check_size.sh, and run it.

For a more advanced approach, use find with -exec in a one-liner. But scripts give you more control over logging and error handling. I use them for weekly disk usage reports.

Common Mistakes And Troubleshooting

One common error is confusing file size with disk usage. The du command shows actual disk usage, which may be larger than the file size due to block allocation. For example, a 1-byte file might use 4 KB of disk space. Use ls -l for file size and du -h for disk usage.

Another mistake is forgetting the -h flag. Without it, sizes are in bytes, which is hard to read. Always add -h unless you need raw numbers for scripting.

If you get a “permission denied” error, use sudo to access protected directories. For example, sudo du -sh /root requires root privileges. Be careful with sudo—you can accidentally delete system files.

Sometimes ls -lh shows a directory size of 4K, which is just the directory entry. To see the actual contents size, use du -sh directory_name. This is a common point of confusion for beginners.

Comparing File Sizes Across Systems

If you’re working with multiple Linux machines, you can compare file sizes remotely using ssh. For example:

ssh user@remote-server "du -sh /path/to/file"

This runs the command on the remote server and prints the result locally. You can also use rsync to transfer files and see size differences. The --dry-run flag shows what would be transferred without actually doing it.

For local comparisons, use diff or md5sum to check if files are identical. Size alone isn’t enough—two files can have the same size but different content. Always verify with checksums for critical data.

Performance Tips For Large Directories

Scanning huge directories (like /var/log or /usr) can take time. Use du with the --max-depth option to limit recursion:

du -h --max-depth=1 /var

This shows sizes for immediate subdirectories only, not their contents. It’s much faster. For even quicker results, use ncdu which has a optimized scanning algorithm.

If you’re checking a network filesystem (NFS or SMB), commands may be slower due to latency. Consider using df -h for a quick overview instead of recursive scans.

Understanding Output Formats

Different commands use different units. ls -lh uses base-10 (KB = 1000 bytes), while du -h uses base-2 (KiB = 1024 bytes) by default. This can cause small discrepancies. For consistency, use the --si flag with du to force base-10:

du -h --si filename.txt

Or use --block-size=1 to get bytes. I prefer base-2 for accuracy, but base-10 is common in marketing (e.g., hard drive sizes). Choose based on your context.

Some commands like stat always output bytes unless you format them. Use numfmt or awk to convert. For example:

stat --format='%s' file | awk '{printf "%.2f MB\n", $1/1024/1024}'

This gives you a custom format. It’s overkill for daily use but handy in scripts.

Frequently Asked Questions

What Is The Easiest Way To Check File Size In Linux?

The easiest method is using ls -lh filename. It shows the size in a human-readable format like 1.2M or 500K. No extra tools needed.

How Do I Find The Size Of A Folder In Linux?

Use du -sh foldername. The -s flag summarizes the total size, and -h makes it readable. For example, du -sh /home/user/Documents.

Can I Check File Size Without Using The Terminal?

Yes, you can use graphical tools like Baobab or Nautilus file manager. Right-click a file and select “Properties” to see its size. For folders, use Disk Usage Analyzer.

Why Does ls -Lh Show A Different Size Than du -H?

ls -lh shows the logical file size, while du -h shows actual disk usage. Due to block allocation, disk usage can be larger. Also, du uses base-2 units by default, while ls uses base-10.

How Do I Find The Largest Files On My Linux System?

Use find / -type f -size +100M -exec ls -lh {} \; to find files over 100 MB. Or use du -ah / | sort -rh | head -20 for a top 20 list. Both require root for system directories.

Now you have a full toolkit for checking file sizes in Linux. From simple ls commands to