How To See Size Of File In Linux : Linux File Size Check Command

Linux users check file sizes using the `ls -lh` command for a human-readable output. If you’re wondering how to see size of file in linux, you’ve come to the right place. This guide covers all the essential commands and techniques, from basic to advanced, so you can manage your filesystem like a pro.

File size checking is a fundamental skill for any Linux user. Whether you’re managing disk space, troubleshooting, or just curious, knowing the exact size of a file helps you make informed decisions. Let’s dive into the most common and effective methods.

How To See Size Of File In Linux

The simplest way to check file size is using the `ls` command with the `-l` option. This shows you a long listing format that includes file size in bytes. But for everyday use, you’ll want the `-h` flag to make it human-readable.

Here’s the basic command:

ls -lh filename

This will output something like `-rw-r–r– 1 user user 1.2M Mar 15 10:30 filename`. The file size appears right before the date. The `-h` flag converts bytes into kilobytes (K), megabytes (M), or gigabytes (G) automatically.

Using Ls With Multiple Files

You can check sizes of multiple files at once. Just list them all:

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

Or use wildcards to match patterns:

ls -lh *.txt

This shows all text files in the current directory with their sizes. For a directory listing with sizes for everything inside, use:

ls -lh /path/to/directory

Sorting Files By Size

To see the largest files first, add the `-S` flag:

ls -lhS

This sorts by size in descending order. Combine with `-r` for ascending:

ls -lhSr

Including Hidden Files

Hidden files (those starting with a dot) are excluded by default. Use the `-a` flag to include them:

ls -lha

This shows all files including `.bashrc`, `.config`, etc. The `-a` flag stands for “all”.

Using The Stat Command

The `stat` command gives you detailed information about a file, including its size in bytes and other blocks. It’s more verbose than `ls`.

stat filename

Output includes:

  • File size in bytes
  • Blocks allocated
  • IO block size
  • File type and permissions
  • Access, modify, and change timestamps

To get just the size, you can parse the output:

stat --format="%s" filename

This returns only the size in bytes. For human-readable format, you’ll need to convert it yourself or use `numfmt`.

Stat With Human-Readable Size

Combine `stat` with `numfmt` to get a friendly output:

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

This gives you something like `1.2M`. The `–to=iec` option uses binary prefixes (KiB, MiB, GiB).

Using The Du Command

The `du` command is primarily for disk usage, but it works great for individual files too. It shows the actual disk space used, which can differ from the file size due to sparse files or compression.

du -h filename

This outputs the size in human-readable format. For multiple files:

du -h file1.txt file2.txt

To see sizes of all files in a directory recursively:

du -h /path/to/directory

Du With Summary

Use the `-s` flag to show only a total for a directory:

du -sh /path/to/directory

This gives you the total disk usage of that directory and its contents. It’s great for checking how much space a folder uses.

Finding Largest Files With Du

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

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

The `-h` flag in `sort` understands human-readable sizes. This lists all items sorted by size, largest first.

Using The Find Command

The `find` command is powerful for searching files based on size. You can locate files larger than a certain threshold.

find /path -type f -size +100M

This finds all regular files larger than 100 MB. The `+` means “greater than”. You can also use `-` for “less than”.

Find With Human-Readable Output

To see sizes in the results, use `-exec ls -lh`:

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

This executes `ls -lh` on each found file. The `{}` is a placeholder for the filename, and `\;` ends the command.

Find Files By Exact Size

To find files of a specific size, omit the `+` or `-`:

find /path -type f -size 1024k

This finds files exactly 1 MB (1024 kilobytes). Note that `find` uses 512-byte blocks by default, but you can specify units like `k`, `M`, `G`.

Using The Wc Command

The `wc` command counts words, lines, and characters, but it can also show file size in bytes.

wc -c filename

This outputs the byte count. For human-readable, pipe to `numfmt`:

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

Note the `<` redirect. This gives you the size in a friendly format. However, `wc` is less commonly used for size checking compared to `ls` or `du`.

Graphical File Managers

If you're using a desktop environment, file managers like Nautilus (GNOME), Dolphin (KDE), or Thunar (XFCE) show file sizes in their properties dialog. Right-click a file and select "Properties" or "Info".

These tools often display size in both bytes and human-readable format. They also show disk usage, which can be helpful for directories.

Checking Size Of A Directory

For directories, `du` is your best friend. Use `du -sh directoryname` to get the total size. To see sizes of subdirectories, use `du -h --max-depth=1`.

du -h --max-depth=1 /path/to/directory

This shows sizes of immediate subdirectories and files. Increase the depth number to go deeper.

Using Ncdu For Interactive Browsing

If you need an interactive tool, install `ncdu` (NCurses Disk Usage). It provides a text-based interface to browse directories and see sizes.

ncdu /path/to/directory

Navigate with arrow keys, press `d` to delete, and `q` to quit. It's excellent for finding large files quickly.

Understanding File Size Vs Disk Usage

File size and disk usage are not always the same. File size is the logical size of the data, while disk usage is the actual space used on the storage device. Differences occur due to:

  • Block size: Files are stored in blocks (typically 4KB). A 1-byte file uses 4KB of disk space.
  • Sparse files: Files with holes that don't occupy physical blocks.
  • Compression: Some filesystems compress data transparently.

The `ls` command shows file size, while `du` shows disk usage. For most purposes, file size is what you care about.

Using Aliases For Convenience

To speed up your workflow, create an alias in your `.bashrc` or `.zshrc`:

alias ll='ls -lh'
alias la='ls -lha'

Then you can just type `ll` or `la` to see file sizes. This saves keystrokes and makes checking sizes effortless.

Scripting File Size Checks

You can automate size checks with shell scripts. Here's a simple example that checks if a file is larger than 1GB:

#!/bin/bash
file="largefile.bin"
size=$(stat --format="%s" "$file")
if [ $size -gt 1073741824 ]; then
    echo "File is larger than 1GB"
else
    echo "File is smaller"
fi

This uses `stat` to get the size in bytes and compares it. You can adapt this for monitoring or cleanup tasks.

Common Pitfalls

Watch out for these common mistakes:

  • Forgetting the `-h` flag: You'll get sizes in bytes, which are hard to read for large files.
  • Using `ls` on directories: `ls -lh dir` shows the directory's metadata, not its contents. Use `ls -lh dir/` or `du -sh dir`.
  • Confusing file size with disk usage: Remember that `du` shows actual space used.

Conclusion

Now you know multiple ways to check file sizes in Linux. The `ls -lh` command is the quickest for individual files, while `du` is best for directories. For advanced searches, `find` with size filters is powerful. Practice these commands to become efficient at managing your filesystem.

Remember, the key to mastering Linux is consistent practice. Try these commands on your own files and see which ones you prefer. With time, checking file sizes will become second nature.

Frequently Asked Questions

How Do I See The Size Of A File In Linux Using The Command Line?

Use the `ls -lh filename` command. The `-l` gives a long listing, and `-h` makes the size human-readable. For example, `ls -lh myfile.txt` shows the size in K, M, or G.

What Is The Difference Between File Size And Disk Usage In Linux?

File size is the logical size of the data, while disk usage is the actual space occupied on the disk. Disk usage can be larger due to block allocation or smaller due to sparse files or compression.

Can I Check File Sizes Recursively In A Directory?

Yes, use `du -h /path/to/directory` to see sizes of all files and subdirectories recursively. For a summary, add the `-s` flag: `du -sh /path/to/directory`.

How Do I Find Files Larger Than A Specific Size In Linux?

Use the `find` command: `find /path -type f -size +100M`. This finds files larger than 100 MB. Replace `+100M` with your desired size and unit (K, M, G).

What Command Shows File Size In Bytes Only?

The `stat --format="%s" filename` command returns the size in bytes. Alternatively, `wc -c < filename` also gives the byte count.

These methods cover all your needs for checking file sizes in Linux. Whether you prefer the simplicity of `ls` or the power of `find`, you now have the tools to manage your files effectively. Happy Linux-ing!