Determining a file’s size in Linux is as simple as using the `du` command, but there are many other ways to check file sizes depending on what you need. If you’ve ever wondered “how to check size of file in linux” for a single document or a whole folder, you’ll find several built-in tools that make it easy. This guide covers all the common methods, from quick terminal commands to graphical options, so you can pick the best one for your task.
Linux offers multiple ways to view file sizes, each with its own strengths. Whether you prefer the command line or a graphical interface, you’ll find a method that works for you. Let’s start with the most straightforward commands and then explore more advanced techniques.
Using The `Ls` Command To Check File Size
The `ls` command is one of the first tools you learn in Linux. It lists files and directories, and with the right flags, it shows file sizes clearly.
Basic `Ls -L` For File Sizes
Run `ls -l` in your terminal. This gives a long listing that includes the file size in bytes. For example:
ls -l myfile.txt
-rw-r--r-- 1 user user 1024 Jan 15 10:30 myfile.txt
The number 1024 is the file size in bytes. This is the most basic way to see how big a file is.
Using `Ls -Lh` For Human-Readable Sizes
Bytes can be hard to read for large files. Add the `-h` flag to get sizes in kilobytes (K), megabytes (M), or gigabytes (G).
ls -lh myfile.txt
-rw-r--r-- 1 user user 1.0K Jan 15 10:30 myfile.txt
Now you see 1.0K instead of 1024. This makes it much easier to understand file sizes at a glance.
Listing Multiple Files With Sizes
To check sizes of all files in a directory, use:
ls -lh /path/to/directory
This shows each file’s size in a human-readable format. You can also sort by size using `ls -lhS` to see the largest files first.
The `Du` Command: Disk Usage For Files And Directories
The `du` command stands for “disk usage.” It’s perfect for checking how much space a file or folder actually uses on your disk. This is often more accurate than `ls` for directories because it sums up all contents.
Checking A Single File With `Du`
To check a single file’s size, run:
du -h myfile.txt
1.0K myfile.txt
The `-h` flag gives human-readable output. Without it, you get sizes in kilobytes by default.
Checking A Directory Size
For a directory, `du` shows the total size of everything inside it:
du -sh /path/to/directory
50M /path/to/directory
The `-s` flag means “summarize,” so you only see the total, not each subfolder.
Listing Sizes Of Subdirectories
To see sizes of all subdirectories inside a folder, use:
du -h /path/to/directory
This lists each subdirectory and its size. Add `–max-depth=1` to limit the output to one level:
du -h --max-depth=1 /path/to/directory
How To Check Size Of File In Linux With The `Stat` Command
The `stat` command gives detailed information about a file, including its size in bytes. It’s useful when you need exact numbers.
Basic Usage Of `Stat`
Run:
stat myfile.txt
This shows a lot of info: file size, blocks, permissions, timestamps, and more. Look for the line that says “Size:” followed by the number of bytes.
Getting Only The Size With `Stat`
To extract just the file size, use the `–format` option:
stat --format=%s myfile.txt
1024
This outputs only the size in bytes, which is handy for scripting or automation.
Using The `Find` Command To Check File Sizes
The `find` command is great for locating files by size. You can search for files larger or smaller than a specific threshold.
Finding Files By Size
To find all files larger than 10 MB in a directory:
find /path/to/search -type f -size +10M
This lists files bigger than 10 megabytes. Use `-size -10M` for files smaller than 10 MB.
Displaying Sizes With `Find`
Combine `find` with `ls` or `du` to see sizes:
find /path/to/search -type f -size +10M -exec ls -lh {} \;
This finds large files and shows their sizes in human-readable format.
Graphical Methods To Check File Sizes
If you prefer a graphical interface, Linux file managers also show file sizes. Here are the most common ones.
Using Nautilus (GNOME)
Open the Files app. Right-click a file and select “Properties.” The size appears in the “Basic” tab. For folders, it shows the total size after calculating.
Using Dolphin (KDE)
In Dolphin, right-click a file and choose “Properties.” The size is listed under “General.” You can also view sizes in the file list by enabling the “Size” column.
Using Thunar (XFCE)
Right-click a file and select “Properties.” The size is displayed in the “Basic” tab. Thunar also shows sizes in the file manager window by default.
Checking File Sizes With `Wc -C`
The `wc` command counts words, lines, and characters. With the `-c` flag, it counts bytes, which is the file size.
Using `Wc -C` For A Single File
Run:
wc -c myfile.txt
1024 myfile.txt
This outputs the byte count followed by the filename. It’s a quick way to get the exact size.
Using `Wc -C` With Multiple Files
You can check several files at once:
wc -c file1.txt file2.txt
1024 file1.txt
2048 file2.txt
3072 total
The last line shows the total of all files.
Understanding File Size Units In Linux
Linux uses binary units by default, but you might see decimal units sometimes. Here’s a quick breakdown.
Binary Vs Decimal Units
- Binary (base-2): 1 KiB = 1024 bytes, 1 MiB = 1024 KiB, etc. This is what Linux commands like `ls -lh` use.
- Decimal (base-10): 1 KB = 1000 bytes, 1 MB = 1000 KB, etc. Some tools might use these, but it’s less common.
Most Linux commands show binary units. If you see “K” or “M” in output, it usually means KiB or MiB.
Common Size Units In Linux Commands
- c or bytes – raw bytes
- k or K – kibibytes (1024 bytes)
- M – mebibytes (1024 KiB)
- G – gibibytes (1024 MiB)
- T – tebibytes (1024 GiB)
Remember that `ls -lh` shows sizes in binary units, so 1.0K means 1024 bytes.
How To Check Size Of File In Linux For Large Directories
When dealing with large directories, you need efficient methods to avoid long wait times. Here are some tips.
Using `Du` With `–Max-depth`
Limit the depth of `du` to speed things up:
du -h --max-depth=1 /path/to/large/directory
This shows only the top-level folders and their sizes, not every subfolder.
Using `Ncdu` For Interactive Browsing
Install `ncdu` (NCurses Disk Usage) for a visual, interactive tool:
sudo apt install ncdu # Debian/Ubuntu
sudo dnf install ncdu # Fedora
Then run:
ncdu /path/to/directory
This shows a navigable list of folders sorted by size. You can browse into folders and see which files take up the most space.
Using `Du` With Sorting
Sort the output by size to find the largest folders:
du -h /path/to/directory | sort -rh
The `sort -rh` flag sorts human-readable sizes in reverse order, so the biggest appears first.
Common Mistakes When Checking File Sizes
Even experienced users make errors. Here are some pitfalls to avoid.
Confusing File Size With Disk Usage
File size and disk usage can differ. A file might show 100 bytes in `ls -l` but use 4096 bytes on disk due to block size. Use `du` to see actual disk usage.
Forgetting The `-H` Flag
Without `-h`, you get sizes in bytes or kilobytes. This can be confusing for large files. Always add `-h` for human-readable output.
Using `Ls` For Directory Sizes
The `ls -l` command shows the size of the directory entry itself, not its contents. For a folder’s total size, use `du -sh`.
Automating File Size Checks With Scripts
You can write simple scripts to check file sizes regularly. Here’s a basic example.
A Simple Bash Script To Log File Sizes
#!/bin/bash
# Log sizes of files in a directory
logfile="sizes.log"
echo "File sizes for $(date)" >> $logfile
ls -lh /path/to/directory >> $logfile
echo "---" >> $logfile
Run this script with cron to monitor file sizes over time.
Using `Du` In A Script
To check a specific file’s size and act on it:
#!/bin/bash
file="/path/to/file"
size=$(stat --format=%s "$file")
if [ $size -gt 1000000 ]; then
echo "File is larger than 1 MB"
fi
This script checks if a file exceeds 1 MB and prints a message.
Comparing File Sizes Across Systems
If you work with multiple Linux systems, you might need to compare file sizes. Here’s how.
Using `Rsync` With `–Dry-run`
The `rsync` command can show size differences without copying:
rsync -av --dry-run --stats /source/ /destination/
This shows total sizes and number of files, helping you compare directories.
Using `Diff` With File Sizes
You can’t directly compare sizes with `diff`, but you can use `ls` output:
diff <(ls -l /path1) <(ls -l /path2)
This shows differences in file listings, including sizes.
Frequently Asked Questions (FAQ)
How Do I Check The Size Of A File In Linux Using The Terminal?
Use `ls -lh filename` or `du -h filename` to see the file size in human-readable format. Both commands work from the terminal.
What Is The Difference Between `Ls -L` And `Du` For File Sizes?
`ls -l` shows the file's logical size in bytes, while `du` shows actual disk usage, which may be larger due to block allocation.
Can I Check The Size Of Multiple Files At Once In Linux?
Yes, use `ls -lh` with a wildcard like `ls -lh *.txt` or `du -h file1 file2` to see sizes of multiple files.
How Do I Find The Largest Files In A Directory?
Use `du -ah /path | sort -rh | head -10` to list the top 10 largest files or directories.
Is There A Graphical Way To Check File Sizes In Linux?
Yes, file managers like Nautilus, Dolphin, or Thunar show file sizes in their properties or list views.
Final Tips For Checking File Sizes Efficiently
Now you know several ways to check file sizes in Linux. For quick checks, use `ls -lh`. For directories, `du -sh` is your best friend. If you need exact bytes, `stat` or `wc -c` work well. And for finding large files, combine `find` with `du`.
Practice these commands to get comfortable. Soon, checking file sizes will become second nature. Remember to use the `-h` flag for readability, and always verify whether you need file size or disk usage. With these tools, you can manage your storage effectively and avoid surprises.