How to Check File Size in Ubuntu Terminal
In this article
Checking file sizes in the Ubuntu terminal is a fundamental skill for anyone who works with files on a Linux system. This guide shows you how to display the size of individual files and directories, using both human-readable and exact byte formats, and how to interpret the output correctly. You'll learn the essential commands—ls, stat, du, and df—and how to combine them with options to get exactly the information you need.
What You'll Need
- A computer running Ubuntu (or any Debian-based Linux distribution).
- Access to a terminal window (Ctrl+Alt+T).
- Basic familiarity with navigating directories using
cdand listing files withls. - No special permissions are required for checking file sizes in your own directories. For system directories, you may need
sudoto see all files.
Step-by-Step
- Open the terminal. Press
Ctrl+Alt+Ton your keyboard, or search for "Terminal" in the activities overview. - Navigate to the directory containing the file. Use the
cdcommand. For example, if your file is in~/Documents, type:cd ~/Documentsand press Enter. - List files with sizes using
ls -lh. Thelscommand lists files. The-loption gives a long format (which includes size), and-hmakes the size human-readable (e.g., 4.2K, 1.5M). Typels -lhand press Enter. You'll see a column labeled "Size" showing the size of each file. - Check a single file's size with
ls -lh filename. Replacefilenamewith the actual name. For example:ls -lh report.pdfwill show only that file's details. - Get the exact size in bytes with
ls -l. If you need the precise byte count (not rounded), omit the-hoption. Typels -land look at the "Size" column—it will show the number of bytes. - Use
statfor detailed information. Thestatcommand shows file size, modification time, permissions, and more. Typestat filenameand press Enter. Look for the "Size:" line; it gives the size in bytes. - Check directory sizes with
du. Thedu(disk usage) command is used for directories. To see the total size of a directory and its subdirectories, typedu -sh directoryname. The-sflag summarizes the total, and-hmakes it human-readable. For example:du -sh ~/Documents. - List all files with sizes in a directory using
du -ah. To see the size of every file inside a directory, usedu -ah directoryname. This shows each file and subdirectory with its size. - Check disk space with
df -h. If you want to see how much space is used and available on your entire filesystem, usedf -h. This is useful for understanding overall disk usage. - Sort files by size. To see the largest files first, combine
lswithsort:ls -lS(capital S sorts by size, descending). For human-readable sizes, usels -lSh.
Common Mistakes
- Confusing file size with disk usage.
lsshows the logical size of a file (the length of the data), whiledushows the actual disk space allocated, which can be larger due to filesystem block sizes. - Forgetting the
-hflag. Without-h, sizes are shown in bytes, which can be hard to read for large files (e.g., 1073741824 bytes instead of 1.0G). - Using
duwithout-son large directories. Runningduon a big directory without-swill list every subdirectory and file, producing a huge output. Use-sfor a summary. - Assuming
ls -lhshows directory sizes.ls -lhon a directory shows the size of the directory entry (usually 4K), not the total size of its contents. Usedufor that. - Not using quotes for filenames with spaces. If a file name contains spaces, enclose it in quotes:
ls -lh "my file.txt".
Tips
- Use aliases for convenience. You can add aliases to your
.bashrcfile, such asalias ll='ls -lh'to make listing with human-readable sizes faster. - Combine with
grepto filter. To find files larger than a certain size, usefind:find . -type f -size +100Mlists all files larger than 100 MB in the current directory. - Use
duwith--max-depth. To limit the depth of directory listing, usedu -h --max-depth=1to show only the current directory's subdirectories and their totals. - Human-readable units. The
-hflag uses powers of 1024 (KiB, MiB, GiB). If you prefer SI units (KB, MB, GB) based on 1000, use--siwithduorls(e.g.,ls --si -l). - Check block size. To see the block size of your filesystem, use
stat -f -c '%S' /path/to/file. This helps understand why disk usage may differ from file size.
Troubleshooting
- Command not found. If you get "command not found" for
duorstat, they are part of the coreutils package. Install them withsudo apt install coreutils. - Permission denied. When checking files in protected directories (like
/root), you may need to prefix withsudo. For example:sudo du -sh /root. - Output is too long. If
du -ahproduces too many lines, pipe it tolessto scroll:du -ah /var | less. - Size shows 0 for directories. This is normal with
ls -l; directories have a small entry size. Useduto see the total contents. - File size is not updating. If you modify a file and its size doesn't change, the file system might be mounted with caching. Wait a few seconds or use
syncto flush writes.
Frequently Asked Questions
What is the difference between ls -lh and du -sh?
ls -lh shows the logical size of individual files (the size of the data), while du -sh shows the actual disk space used, which includes filesystem overhead. For most files, they are similar, but for sparse files or files with many hard links, they can differ.
How do I see the size of all files in a directory, including hidden ones?
Use ls -lah to include hidden files (those starting with a dot). The -a flag shows all files.
Can I check file size in megabytes only?
Yes, use ls -l --block-size=M to show sizes in megabytes (rounded up). For example, ls -l --block-size=M filename.
Why does du show a larger size than ls?
This is usually because the file system allocates space in blocks (e.g., 4KB). A small file might take up a whole block, so du reports the block size, while ls reports the actual byte count.
How do I check the size of a file without entering its directory?
You can specify the full path: ls -lh /path/to/file or stat /path/to/file.
Is there a way to check file size in a GUI?
Yes, you can use the Files (Nautilus) file manager. Right-click a file and select "Properties" to see its size. But the terminal is often faster and more scriptable.