Knowing the exact file size in Linux helps you manage disk space and transfer limits efficiently. If you are new to the command line, learning how to check the file size in linux is one of the first skills you need. It is simple, fast, and gives you control over your storage.
This guide will show you multiple ways to get file sizes. You will learn commands for single files, directories, and even human-readable formats. By the end, you will handle disk usage like a pro.
How To Check The File Size In Linux
The most common command to check file size is ls -lh. It stands for “list” with “long” format and “human-readable” sizes. Open your terminal and type ls -lh filename. The output will show the size in kilobytes (K), megabytes (M), or gigabytes (G).
For example, if you have a file named report.pdf, run ls -lh report.pdf. You will see something like -rw-r--r-- 1 user user 2.3M Apr 10 10:15 report.pdf. The size is right there, 2.3 megabytes.
But ls is just the start. There are better tools for different situations. Let us explore them step by step.
Using The Ls Command For Quick Checks
The ls command works for any file. It is the fastest way to get a size estimate. Use these variations:
ls -lshows size in bytes (raw number).ls -lhshows size in human-readable format (K, M, G).ls -lahincludes hidden files (those starting with a dot).
To check multiple files at once, just list them: ls -lh file1.txt file2.txt. The terminal will show each file’s size on a separate line.
Getting File Size With The Stat Command
The stat command gives you detailed information. It shows size, permissions, timestamps, and more. Run stat filename to see the full output. Look for the line that says “Size:” followed by bytes.
For example, stat report.pdf might return Size: 2412345. That is the exact size in bytes. If you want human-readable output, use stat --format=%s filename to get just the bytes, then convert manually.
Stat is useful when you need precise byte counts for scripting or debugging. It is less common for everyday use but very accurate.
Using Du For Directory Sizes
To check the size of a folder, use du (disk usage). The command du -sh foldername shows the total size of that directory. The -s flag means summary, and -h makes it human-readable.
For example, du -sh Documents/ might output 1.2G Documents/. That tells you the entire folder uses 1.2 gigabytes. You can also check subdirectories with du -h foldername to see each folder’s size.
Du is essential for finding large directories that eat up disk space. Combine it with sort -h to rank folders by size: du -sh * | sort -h.
Checking File Size With The Df Command
The df command shows disk space usage for entire file systems. It is not for single files, but it helps you understand overall storage. Run df -h to see all mounted partitions and their available space.
For a specific partition, use df -h /home. This tells you how much space is used and free on the /home partition. It is useful when you wonder why your disk is full.
Using The Find Command To Locate Large Files
The find command can search for files based on size. For instance, find . -type f -size +100M finds all files larger than 100 megabytes in the current directory and subdirectories. Add -exec ls -lh {} \; to display sizes.
Example: find . -type f -size +500M -exec ls -lh {} \; shows files over 500 MB with human-readable sizes. This is perfect for cleaning up disk space.
You can also search for files smaller than a certain size: find . -type f -size -1M finds files under 1 megabyte. The + and - signs indicate “greater than” and “less than.”
Using The Wc Command For Byte Counts
The wc command is typically used for word counts, but it can show file size too. Run wc -c filename to get the byte count. For example, wc -c report.pdf returns 2412345 report.pdf.
To get a human-readable size, you would need to convert manually. Wc is fast and reliable for scripts where you need raw bytes.
Checking File Size In Graphical Interface
If you prefer a GUI, most Linux file managers show file sizes. Open Nautilus (GNOME), Dolphin (KDE), or Thunar (XFCE). Right-click a file and select “Properties.” The size appears in the dialog box.
For directories, you can also right-click and choose “Properties” to see the total size. This method is intuitive but slower for multiple files.
Using The Ncdu Tool For Interactive Analysis
Ncdu (NCurses Disk Usage) is a terminal-based tool that gives an interactive view of disk usage. Install it with sudo apt install ncdu (Debian/Ubuntu) or sudo yum install ncdu (RHEL/CentOS).
Run ncdu /path/to/directory. It scans the directory and shows a sorted list of folders and files by size. You can navigate with arrow keys and delete files directly. It is much faster than manual checking.
Checking File Size In Bytes Vs Human-Readable
Sometimes you need exact bytes for programming. Use stat --format=%s filename or wc -c < filename (note the redirect). Both give raw byte counts.
For human-readable, stick with ls -lh or du -sh. These commands automatically convert bytes to K, M, or G. They are easier to read at a glance.
Checking File Size For Multiple Files At Once
To see sizes of all files in a directory, use ls -lh without a filename. It lists everything. For specific patterns, use wildcards: ls -lh *.txt shows all text files.
You can also use du -ah to show sizes of all files and subdirectories. The -a flag includes files, not just directories.
Using The Xargs Command With Size Checks
Combine find with xargs to process many files. For example, find . -type f -name "*.log" | xargs ls -lh lists sizes of all log files. This is efficient for large numbers of files.
You can also pipe to sort -k5 -h to sort by size: ls -lh | sort -k5 -h. The fifth column is the size field.
Checking File Size In Different Units
By default, ls -lh uses binary units (KiB, MiB, GiB). Some commands like du --si use decimal units (KB, MB, GB). Use du -sh --si foldername for decimal output.
Binary units are more accurate for storage calculations, while decimal units match hard drive marketing. Choose based on your need.
Common Mistakes When Checking File Sizes
One mistake is forgetting the -h flag. Without it, you get raw bytes, which are hard to read. Another is using du on a single file—it works but ls is simpler.
Also, be careful with symbolic links. ls -lh shows the size of the link itself, not the target file. Use ls -lhL to follow links and show the target's size.
Automating File Size Checks With Scripts
You can write a bash script to check sizes regularly. For example:
#!/bin/bash
for file in "$@"; do
size=$(stat --format=%s "$file")
echo "$file: $size bytes"
done
Save it as checksize.sh, make it executable with chmod +x checksize.sh, and run it with filenames as arguments.
Checking File Size On Remote Servers
If you are SSH'd into a remote server, all these commands work the same. Use ssh user@server 'ls -lh /path/to/file' to check remotely without logging in.
For multiple files, consider using rsync with --list-only to see sizes before transferring.
Using The Lsblk Command For Block Devices
While not for files, lsblk shows sizes of disks and partitions. Run lsblk -o NAME,SIZE to see device sizes. This helps when you want to know total disk capacity.
Checking File Size With Python Or Other Languages
In Python, use os.path.getsize('filename') to get bytes. For human-readable, use a custom function. This is useful for automation or web interfaces.
Example: python3 -c "import os; print(os.path.getsize('report.pdf'))" returns bytes.
Understanding File Size Vs Disk Usage
File size is the actual data in the file. Disk usage is the space it occupies on the disk, which can be larger due to block size. Use du for disk usage and ls for file size.
For sparse files (files with empty holes), disk usage may be smaller than file size. The du command shows actual disk consumption.
Checking File Size Of Compressed Archives
For .zip or .tar.gz files, use ls -lh to see the compressed size. To see contents inside without extracting, use unzip -l archive.zip or tar -tzf archive.tar.gz. These show individual file sizes inside.
Using The Lsof Command For Open Files
lsof lists open files and their sizes. Run lsof -i to see network connections, but for file sizes, use lsof -p PID to see files opened by a process. The size column shows current file size.
Checking File Size In Different Directories
You can check sizes across the whole system with du -sh /* (requires root). This shows sizes of top-level directories. Be careful, as it may take time on large systems.
Using The Baobab Tool (Disk Usage Analyzer)
Baobab is a graphical tool for GNOME. Install it with sudo apt install baobab. It scans directories and shows a visual pie chart of sizes. It is user-friendly for beginners.
Checking File Size With The File Manager CLI
Some file managers like Ranger (terminal-based) show file sizes in their interface. Install Ranger with sudo apt install ranger and navigate to files. The size is displayed in the right column.
Common Questions About File Sizes
How do I check file size in Linux with MB?
Use ls -lh filename. The output will show megabytes (M) if the file is large enough. For exact MB, use du -h filename.
What is the command to check file size in Linux?
The most common command is ls -lh. Other options include stat, du, and wc -c.
How to check file size in Linux in KB?
Use ls -lh; it shows kilobytes (K) for small files. For exact KB, divide bytes by 1024.
Can I check file size in Linux without terminal?
Yes, use your file manager's properties dialog. Right-click the file and select "Properties."
How to check size of all files in a directory?
Use du -sh directoryname for total size, or ls -lh for individual file sizes.
Conclusion
Now you know multiple ways to check file sizes in Linux. Start with ls -lh for quick checks, use du for directories, and find for large files. Each command has its strength, so choose based on your task.
Practice these commands in your terminal. Over time, they will become second nature. Managing disk space becomes easy when you master file size checking.
Remember to use the -h flag for human-readable output. It saves you from mental math. And always double-check units—binary vs decimal can cause confusion.
With these tools, you can keep your Linux system organized and avoid running out of space. Happy file managing!