Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

How to Check File Size in Ubuntu Terminal

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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 cd and listing files with ls.
  • No special permissions are required for checking file sizes in your own directories. For system directories, you may need sudo to see all files.

Step-by-Step

  1. Open the terminal. Press Ctrl+Alt+T on your keyboard, or search for "Terminal" in the activities overview.
  2. Navigate to the directory containing the file. Use the cd command. For example, if your file is in ~/Documents, type: cd ~/Documents and press Enter.
  3. List files with sizes using ls -lh. The ls command lists files. The -l option gives a long format (which includes size), and -h makes the size human-readable (e.g., 4.2K, 1.5M). Type ls -lh and press Enter. You'll see a column labeled "Size" showing the size of each file.
  4. Check a single file's size with ls -lh filename. Replace filename with the actual name. For example: ls -lh report.pdf will show only that file's details.
  5. Get the exact size in bytes with ls -l. If you need the precise byte count (not rounded), omit the -h option. Type ls -l and look at the "Size" column—it will show the number of bytes.
  6. Use stat for detailed information. The stat command shows file size, modification time, permissions, and more. Type stat filename and press Enter. Look for the "Size:" line; it gives the size in bytes.
  7. Check directory sizes with du. The du (disk usage) command is used for directories. To see the total size of a directory and its subdirectories, type du -sh directoryname. The -s flag summarizes the total, and -h makes it human-readable. For example: du -sh ~/Documents.
  8. List all files with sizes in a directory using du -ah. To see the size of every file inside a directory, use du -ah directoryname. This shows each file and subdirectory with its size.
  9. Check disk space with df -h. If you want to see how much space is used and available on your entire filesystem, use df -h. This is useful for understanding overall disk usage.
  10. Sort files by size. To see the largest files first, combine ls with sort: ls -lS (capital S sorts by size, descending). For human-readable sizes, use ls -lSh.

Common Mistakes

  • Confusing file size with disk usage. ls shows the logical size of a file (the length of the data), while du shows the actual disk space allocated, which can be larger due to filesystem block sizes.
  • Forgetting the -h flag. 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 du without -s on large directories. Running du on a big directory without -s will list every subdirectory and file, producing a huge output. Use -s for a summary.
  • Assuming ls -lh shows directory sizes. ls -lh on a directory shows the size of the directory entry (usually 4K), not the total size of its contents. Use du for 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 .bashrc file, such as alias ll='ls -lh' to make listing with human-readable sizes faster.
  • Combine with grep to filter. To find files larger than a certain size, use find: find . -type f -size +100M lists all files larger than 100 MB in the current directory.
  • Use du with --max-depth. To limit the depth of directory listing, use du -h --max-depth=1 to show only the current directory's subdirectories and their totals.
  • Human-readable units. The -h flag uses powers of 1024 (KiB, MiB, GiB). If you prefer SI units (KB, MB, GB) based on 1000, use --si with du or ls (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 du or stat, they are part of the coreutils package. Install them with sudo apt install coreutils.
  • Permission denied. When checking files in protected directories (like /root), you may need to prefix with sudo. For example: sudo du -sh /root.
  • Output is too long. If du -ah produces too many lines, pipe it to less to scroll: du -ah /var | less.
  • Size shows 0 for directories. This is normal with ls -l; directories have a small entry size. Use du to 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 sync to 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.