How to Check File Size in Ubuntu Terminal: A Complete Guide

Managing disk space is a critical skill for any Linux user. Whether you are cleaning up a server or managing personal documents, you often need to check file size in Ubuntu terminal to see what is taking up space.

Fortunately, Linux provides several built-in commands to help you verify file and directory sizes quickly. Here are the 4 best methods to check file size in Ubuntu terminal.

Here is a quick summary table of the commands covered in the article.

Quick Cheat Sheet: Checking File Sizes in Ubuntu

CommandSyntax / ExampleBest Used ForOutput Format
lsls -lh filenameQuickly checking a single file in a listHuman-readable (KB, MB, GB)
dudu -sh foldernameChecking total size of a directorySummary total (e.g., 500M)
statstat filenameGetting exact byte count & metadataRaw bytes, blocks, & dates
ncduncduAnalyzing disk usage to free up spaceInteractive / Visual UI
sortls -lhSSorting files by size (largest first)Sorted list

1. Use the ls Command (Best for Single Files)

The most common way to check file size in Ubuntu terminal is using the ls (list) command. By default, ls only shows filenames, but adding flags gives you detailed size information.

Command:

ls -lh filename.txt

Explanation:

  • -l (Long listing): Displays permissions, owner, date, and size.
  • -h (Human-readable): Converts bytes into easy-to-read formats like KB, MB, or GB.

Output Example:

-rw-r--r-- 1 user user 5.0M Mar 10 10:00 video.mp4

(The file size is clearly shown as 5.0M).


2. Use the du Command (Best for Directories)

While ls is great for individual files, the du (Disk Usage) command is the best tool to check file size in Ubuntu terminal for both files and entire directories.

Command:

See also: Mastering the Linux Command Line — Your Complete Free Training Guide

du -h filename.txt

To check a specific folder size:

du -sh /path/to/directory

Explanation:

  • -s (Summary): Shows the total size of the folder instead of listing every single file inside it.
  • -h: Displays the size in Human-readable format (K, M, G).

3. Use the stat Command (Best for Precision)

If you need to know the exact byte count of a file, stat is the most precise tool available. It displays detailed metadata about the file.

Command:

stat filename.txt

Output Example:

  File: video.mp4
  Size: 5242880     Blocks: 10240      IO Block: 4096   regular file

This is useful for scripts or when verifying if a file transfer was completed exactly.


4. Use ncdu (Best Visual Tool)

If you want to check file size in Ubuntu terminal interactively to clean up disk space, ncdu is an excellent visual tool. You may need to install it first.

Installation:

sudo apt install ncdu

Usage:

ncdu

This opens a graphical interface inside your terminal, allowing you to navigate folders and see which files are the largest, sorted automatically by size.


FAQ: Quick Answers about File Sizes

Here are 5 common questions on how to check file size in Ubuntu terminal.

  1. How do I check the total size of a directory?
    Run du -sh foldername to see the total summary in MB or GB.
  2. How do I list all files sorted by size?
    Run ls -lhS (the capital S flag sorts files from largest to smallest).
  3. How do I check the size of a file in bytes only?
    Run wc -c filename to output just the byte count.
  4. Does ls show hidden file sizes?
    No, you must add the -a flag: ls -lah to see hidden files and their sizes.
  5. How do I find the top 5 largest files?
    Run du -ah | sort -rh | head -n 5 to list the biggest files in the current folder.

David Cao
David Cao

David is a Cloud & DevOps Enthusiast. He has years of experience as a Linux engineer. He had working experience in AMD, EMC. He likes Linux, Python, bash, and more. He is a technical blogger and a Software Engineer. He enjoys sharing his learning and contributing to open-source.

Articles: 594

Leave a Reply

Your email address will not be published. Required fields are marked *