Finding the size of a file in Linux helps you manage disk space and identify large files. If you are wondering how to check size of a file in linux, you have come to the right place. This guide covers every major command and trick you need to know.
Linux offers several ways to check file sizes, from simple commands to detailed disk usage reports. Whether you are a beginner or a seasoned sysadmin, these methods will help you stay on top of your storage.
Why File Size Matters In Linux
Knowing file sizes is essential for system maintenance. Large log files, downloads, or cache data can fill up your disk quickly. By checking sizes regularly, you avoid running out of space and keep your system fast.
It also helps when transfering files over a network or deciding what to archive. Every Linux user should know at least two or three ways to check file sizes.
How To Check Size Of A File In Linux
This section explains the most common commands for checking file sizes. Each method has its own strengths, so pick the one that fits your task.
Using The Ls Command
The ls command is the simplest way to see file sizes. Open your terminal and type:
ls -lh filename
The -l flag gives a long listing, and -h makes sizes human-readable (like KB, MB, GB). For example:
ls -lh myfile.txt
Output will show something like -rw-r--r-- 1 user user 1.2M Mar 15 10:30 myfile.txt. The size is right there.
To list all files in a directory with sizes, use:
ls -lh /path/to/directory
This works for both files and folders. For directories, it shows the size of the directory entry, not its contents. That is a common confusion point.
Using The Stat Command
The stat command gives detailed information about a file, including its exact size in bytes. Run:
stat filename
Look for the line that says Size:. It shows the size in bytes. You can also use --format to get just the size:
stat --format=%s filename
This returns only the number of bytes. It is useful for scripts or when you need precise values.
Using The Du Command
The du (disk usage) command is best for checking the actual disk space used by a file or folder. For a single file:
du -h filename
The -h flag again gives human-readable output. For directories, du sums up all contents:
du -sh /path/to/directory
The -s flag summarizes the total. This is perfect for finding large folders.
Using The Find Command
The find command can locate files based on size. To find all files larger than 100MB:
find /path -type f -size +100M -exec ls -lh {} \;
This searches recursively and shows sizes. You can also use -size -10M for files smaller than 10MB. It is a powerful way to identify space hogs.
Using Graphical File Managers
If you prefer a GUI, most Linux file managers show file sizes in the properties window. Right-click a file and select “Properties” or “Info.” The size is displayed there.
Tools like Nautilus (GNOME), Dolphin (KDE), and Thunar (XFCE) all support this. It is the easiest method for visual users.
Checking Sizes Of Multiple Files
Sometimes you need to check several files at once. Here are efficient ways to do that.
Using Ls With Wildcards
List all .log files with sizes:
ls -lh *.log
This shows sizes for every matching file in the current directory.
Using Du With A Directory
To see sizes of all files inside a folder:
du -ah /path/to/directory
The -a flag includes files, not just directories. Output is sorted by the order files are found.
Sorting Files By Size
To find the largest files, sort the output:
ls -lhS
The -S flag sorts by size, largest first. Combine with -r for reverse order.
Understanding File Size Units
Linux uses binary units by default. Here is a quick reference:
- 1 KiB = 1024 bytes
- 1 MiB = 1024 KiB
- 1 GiB = 1024 MiB
Some commands like ls -h use these units. Others might show decimal (SI) units. Always check the man page if unsure.
Common Mistakes And Pitfalls
New users often confuse file size with disk usage. A file might show 1MB in ls but use 4KB on disk due to block size. The du command reflects actual disk usage.
Another mistake is forgetting the -h flag. Without it, sizes appear in bytes, which is hard to read. Always use -h for human-friendly output.
Also, be careful with symbolic links. ls -l shows the link size, not the target file size. Use stat or du on the target path.
Automating File Size Checks
You can script file size checks for monitoring. Here is a simple bash script:
#!/bin/bash
for file in /var/log/*.log; do
size=$(stat --format=%s "$file")
if [ $size -gt 1048576 ]; then
echo "Large file: $file ($size bytes)"
fi
done
This checks all log files and reports those larger than 1MB. You can run it via cron for regular monitoring.
Using The Ncdu Tool
If you want an interactive way to explore disk usage, install ncdu (NCurses Disk Usage). Run:
ncdu /path
It shows a navigable list of files and folders sorted by size. You can delete files directly from the interface. It is a must-have for disk cleanup.
Checking File Sizes Over SSH
When managing remote servers, you can use the same commands via SSH. For example:
ssh user@server 'du -sh /home/user'
This runs the command remotely and returns the size. You can also use ls or stat the same way.
Using The Tree Command
The tree command shows directory structures with file sizes. Install it first, then run:
tree -h /path
It displays a tree view with human-readable sizes. Great for visual overviews.
Comparing File Sizes
To compare sizes of two files, use:
stat --format=%s file1 file2
Or simply run ls -lh file1 file2. For scripts, use test or [ ] to compare numeric values.
Handling Compressed Files
Compressed files like .tar.gz show their compressed size. To see the original size inside an archive, use:
tar -tvf archive.tar.gz | head
This lists contents with original sizes. For .zip files, use unzip -l.
Using The Lsblk Command For Block Devices
If you need sizes of entire disks or partitions, use lsblk:
lsblk -o NAME,SIZE
This shows block devices and their sizes. Not directly for files, but useful for context.
Practical Examples
Here are real-world scenarios:
- Check the size of a downloaded ISO:
ls -lh ubuntu.iso - Find the largest file in /var/log:
ls -lhS /var/log | head -5 - Get total size of a project folder:
du -sh ~/projects - List all files over 500MB:
find / -type f -size +500M -exec ls -lh {} \;
When To Use Each Command
Choose based on your need:
- Quick glance:
ls -lh - Exact bytes:
stat - Disk usage:
du -sh - Search by size:
find - Interactive exploration:
ncdu
Frequently Asked Questions
What Is The Difference Between Ls And Du For File Sizes?
ls shows the logical file size, while du shows actual disk usage. For most files they are similar, but sparse files or compressed filesystems can differ.
How Can I Check The Size Of A Hidden File In Linux?
Use ls -lah .hiddenfile or du -sh .hiddenfile. The -a flag in ls shows hidden files.
Can I Check File Sizes Without Using The Terminal?
Yes, most file managers show sizes in their properties window. Right-click the file and select “Properties” or “Get Info.”
How Do I Check The Size Of A Directory In Linux?
Use du -sh directory_name. This gives the total size of all contents inside the directory.
What Command Shows File Sizes In Megabytes By Default?
Use ls -lh or du -h. The -h flag automatically chooses the best unit (KB, MB, GB).
Final Tips For Managing File Sizes
Regularly check your home directory and /var/log for large files. Set up a cron job to alert you when disk usage exceeds a threshold. Use ncdu for quick cleanups.
Remember that the how to check size of a file in linux skill is fundamental. With these commands, you will never be caught off guard by a full disk again.
Practice each command in your terminal. Start with ls -lh on a few files, then try du -sh on a folder. Once comfortable, explore find and ncdu for advanced tasks.
Linux gives you many ways to check file sizes. Choose the one that fits your workflow and keep your system tidy.