How to Display Human Readable File Size in Linux [5 Proven Methods]

Written by

Reviewed by

Last updated: June 8, 2024

Expert verified

SVG Image

TL;DR

To display human readable file size in Linux, you can try these methods:

  1. ls Command: Use ls -lh in the terminal to list files and directories with sizes displayed in a human-readable format.
  2. du Command: Run du -h in the terminal to estimate file and directory space usage with sizes shown in a human-readable format.

Read the guide below to learn how to display file size in Linux in human-readable format. Also learn about the best practices and common errors with possible solutions.

Understanding file sizes in a Linux environment is crucial for developers, sysadmins, and power users. With the vast amounts of data you handle on a daily basis, being able to display file size in Linux in a human-readable format can greatly enhance your productivity and decision-making. In this comprehensive guide, I’ll explore various methods to display human readable file size in Linux. I will also discover some common errors and best practices to follow when checking file size.

How to Display Human Readable File Size in Linux

To display Linux human readable file size, use the ls and du commands with the -h option. For example, running ls -lh will list files with sizes in KB, MB, or GB, making them easier to understand. Similarly, du -h will display directory sizes in a human-readable format. These options ensure that file sizes are presented in a clear and comprehensible way.

Here is the step-by-step guide for four different methods to linux list file size human readable format:

1. ls Command

The ls command is a fundamental tool for listing files and directories in the Linux Terminal. Displaying file sizes in a human-readable format is effortless with ls. To display file sizes in a human-readable format, follow these steps:

  1. Open your Terminal.
opening terminal 32
  1. Navigate to the directory containing the files you want to inspect.
navigating to downloads directory 2
  1. Run the following command:
ls -lh
  1. This command will list the files and directories in the current directory with sizes displayed in a human-readable format. The -lh option stands for long format and human-readable.
displaying size of files using ls command

2. du Command

The du command, short for disk usage, is used to estimate file and directory space usage in Linux. It presents file sizes in a format that is easily comprehensible, facilitating efficient file management and data analysis. To display file sizes in a human-readable format using du, follow these steps:

  1. Access your Terminal window and navigate to the directory you want to analyze.
  2. Now run the following command to Linux list files with human readable size:
du -h
  1. The -h option instructs du to display file sizes in a human-readable format.
displaying size of files using du command

3. awk Command

The awk command is a powerful text processing tool that can be used in conjunction with other commands to manipulate data. This method provides flexibility and control over how file sizes are presented, tailoring the output to meet your specific needs. To display file sizes in a specific format, follow these steps:

  1. Launch your command prompt.
  2. Use the appropriate command (ls or du) to generate the file size output you want to format. Pipe the output to awk with the desired formatting. For example:
ls -lh | awk '{ print $5, $9 }'
  1. This command combines the ls output with awk to display file sizes and corresponding filenames in a specific format.
displaying size of files using awk command

4. Third-Party Tools

In addition to the built-in commands, several third-party tools are available that provide advanced features to display file size in linux in a human-readable format. Whether you prefer an interactive interface, sorting and filtering capabilities, or integrated file size display within a file manager, these tools provide valuable functionalities that enhance your file management experience. Here are a few popular options:

  1. Launch your command window and first of all update your system by running the command:
sudo apt update
  1. The command will update all system packages of the system.
updating system package list 15
  1. Now install ncdu by running the following command:
sudo apt install ncdu
  1. The command will install the ncdu tool on your system after the execution.
installing ncdu tool on debian
  1. Now launch ncdu by running the following command:
ncdu
launching ncdu
  1. Now you can view the file sizes in human readable format using ncdu.
viewing size of files using ncdu

5. Bash Scripts

Bash scripts allow you to automate repetitive tasks, including file size conversions. Using bash scripts for file size conversions streamlines your workflow and ensures consistent and easily understandable representations, saving you time and effort in your file management endeavors. Here’s an example of a simple bash script to convert file sizes:

  1. Open the text editor and write the following script:
#!/bin/bash
filesize=$1
if [[ $filesize -lt 1024 ]]; then
 echo "${filesize} bytes"
elif [[ $filesize -lt 1048576 ]]; then
  echo "$((filesize / 1024)) KB"
elif [[ $filesize -lt 1073741824 ]]; then
 echo "$((filesize / 1048576)) MB"
else
  echo "$((filesize / 1073741824)) GB"
fi
  1. The script will convert the file size in human readable format.
writing script code to analyze file size
  1. Save the script in a file, e.g., filesize.sh
saving script file
  1. Make the script executable using the command 
chmod +x filesize.sh
  1. The command will make the script executable.
making script file executable
  1. Then, you can use it like this:
./filesize.sh 5242880
  1. This will output 5 MB for a file size of 5 megabytes.
executing script file to analyze size

3 Best Practices for Displaying File Size in Linux

When it comes to display file size in Linux, following best practices ensures accurate and meaningful representations.By implementing these practices, you can improve your file management workflows and make informed decisions based on accurate file size information. Here are three best practices to follow:

  • 🛠️ Use the Right Command Options: To display file sizes in a human-readable format, include the -h option with the ls and du commands. This ensures that file and directory sizes are shown in a clear and easily understandable format, providing consistent and accurate size representations.
  • 🧠 Know Binary and Decimal Prefixes: Understanding binary (KiB, MiB, GiB) and decimal (KB, MB, GB) prefixes is crucial. Binary prefixes are based on powers of 2, while decimal prefixes are based on powers of 10. Misunderstanding these can lead to inaccurate conversions and confusion in file size displays.
  • 🔄 Automate and Customize: Enhance efficiency by automating file size displays with bash scripts or aliases. This allows you to automate repetitive tasks and customize display preferences to meet specific needs, ensuring consistent and convenient file size representations in your workflow.

3 Common Errors When Displaying File Size in Linux

Accurately displaying file sizes in Linux is crucial for effective file management. However, there are common errors that can lead to incorrect or misleading results. By being aware of these common errors and taking care to avoid them, you can ensure that your file size representations in Linux are accurate, consistent, and conducive to effective file management. Here are three common errors that can occur:

  • Using the Wrong Unit Prefix: A common mistake is using kilobytes (KB) instead of kibibytes (KiB), leading to incorrect calculations and confusion. Understand the difference between binary and decimal prefixes for accurate file size measurements.
  • 🚫 Misinterpreting Decimal vs. Binary Prefixes: Confusing decimal (KB, MB) with binary prefixes (KiB, MiB) leads to inaccuracies. Decimal prefixes use powers of 10, while binary prefixes use powers of 2. Always use the correct prefix to avoid errors in file management.
  • 📌 Neglecting File Size Display Options: Not using the -h option in commands like ls -lh results in file sizes shown in bytes, making them harder to read. Always include options that display sizes in a human-readable format for easier management.

Linux Human Readable File Size: Wrapping it Up

In this article, I’ve covered various methods to display human readable file size in Linux using commands like ls, du, and awk, as well as third-party tools like ncdu and automation through bash scripts.

For further learning, I recommend exploring:

  • Using the find command for advanced file searching, which helps locate files based on specific criteria, improving file organization.
  • Techniques for sorting files with the sort command, which enhance your ability to organize and retrieve data efficiently.
  • Lastly, an article on how to clear a terminal screen in Linux will teach you to manage your workspace better, making it easier to focus on file management tasks.

Frequently Asked Questions

Can I customize the output format to include additional information along with file sizes?

Absolutely! Several commands and tools in Linux provide flexible options to customize the output format when displaying file sizes. For instance, the ls command offers various flags like -l (long format) and -h (human-readable) that show additional information like permissions, owner, modification date, and file sizes in human-readable units. Moreover, you can leverage the power of awk or sed to further customize the output and extract specific details you need. This level of customization allows you to tailor the output to your specific requirements, making it easier to analyze and manage files efficiently.

How can I compare file sizes between directories or different systems?

Comparing file sizes between directories or different systems is made simple with tools like rsync or other file synchronization utilities. The rsync command, combined with its dry-run option, allows you to preview the changes and size differences before executing the synchronization. Additionally, you can utilize the diff command, which compares the contents of files between directories, helping you identify any variations in size or content. These powerful tools enable you to efficiently compare and synchronize files, ensuring data consistency and aiding in data management across various locations.

What if I encounter extremely large or small file sizes that don’t fit the standard units?

When dealing with file sizes that fall outside the standard units (KB, MB, GB), scientific notation offers a practical solution. Scientific notation represents these exceptionally large or small sizes using powers of 10, making it easier to comprehend and compare. For example, if you encounter a file size of 1.5 × 10^9 bytes, it can be equivalently expressed as 1.5 GB using scientific notation. Embracing this notation simplifies the handling of unusually large or small file sizes, allowing for more straightforward calculations and a better grasp of data scale across various contexts.

Ojash

Author

Ojash is a skilled Linux expert and tech writer with over a decade of experience. He has extensive knowledge of Linux's file system, command-line interface, and software installations. Ojash is also an expert in shell scripting and automation, with experience in Bash, Python, and Perl. He has published numerous articles on Linux in various online publications, making him a valuable resource for both seasoned Linux users and beginners. Ojash is also an active member of the Linux community and participates in Linux forums.

Akshat

Reviewer

Akshat is a software engineer, product designer and the co-founder of Scrutify. He's an experienced Linux professional and the senior editor of this blog. He is also an open-source contributor to many projects on Github and has written several technical guides on Linux. Apart from that, he’s also actively sharing his ideas and tutorials on Medium and Attirer. As the editor of this blog, Akshat brings his wealth of knowledge and experience to provide readers with valuable insights and advice on a wide range of Linux-related topics.

Share this article
Shareable URL
Prev Post

How to Install GNOME on Ubuntu [8 Easy Steps]

Next Post

How to Update Debian Linux [4 Effective Methods]

Leave a Reply

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

Read next