Using Linux Commands to Find File and Directory Sizes

Avatar

By squashlabs, Last Updated: Oct. 21, 2023

Using Linux Commands to Find File and Directory Sizes

Finding the sizes of files and directories is a common task when working with Linux systems. Fortunately, Linux provides several commands that can help you accomplish this. In this guide, we will explore some of the most useful Linux commands for finding file and directory sizes.

Using the du Command

One of the most commonly used commands for finding file and directory sizes in Linux is the du command. The du command (short for "disk usage") displays the disk space used by files and directories.

To use the du command, open a terminal and navigate to the directory you want to check. Then, simply run the following command:

du -sh <directory>

The -s option tells du to display only the total size of the specified directory, rather than a detailed breakdown of each file and subdirectory within it. The -h option makes the output human-readable, using units like "K" for kilobytes, "M" for megabytes, and "G" for gigabytes.

For example, to find the size of the /home/myuser/documents directory, you would run the following command:

du -sh /home/myuser/documents

The output will show the total size of the directory in a human-readable format.

Related Article: How to Use If-Else Statements in Shell Scripts

Using the ls Command

Another way to find the sizes of files and directories in Linux is by using the ls command. The ls command lists files and directories in a directory, and with the right options, it can display their sizes as well.

To use the ls command to display sizes, open a terminal and navigate to the directory you want to check. Then, run the following command:

ls -lh <file_or_directory>

The -l option tells ls to use the long listing format, which includes additional information such as file permissions, ownership, and size. The -h option makes the output human-readable, using units like "K" for kilobytes, "M" for megabytes, and "G" for gigabytes.

For example, to find the size of a file named myfile.txt, you would run the following command:

ls -lh myfile.txt

The output will display the size of the file in a human-readable format.

To find the size of a directory, you can use the ls command with the -ld options, like this:

ls -ld <directory>

This will display the size of the directory itself, rather than its contents. Note that the size of a directory represents the space it occupies on the disk, which may not be the sum of the sizes of its files and subdirectories.

Best Practices and Tips

Related Article: Bash Scripting Handbook for Linux SysAdmins

When using the du or ls commands to find file and directory sizes in Linux, consider the following best practices and tips:

- If you want to find the sizes of all files and directories within a directory, you can omit the specific file or directory name and run the command directly on the parent directory. For example, to find the sizes of all files and directories within the current directory, run du -sh or ls -lh.

- To sort the output of the du or ls command by size, you can combine it with other commands such as sort or awk. For example, to display the largest files or directories first, you can pipe the output of du or ls to the sort command, like this: du -sh | sort -h.

- By default, the du and ls commands will display sizes in bytes if they are less than a kilobyte. To always display sizes in human-readable format, use the -h option.

- If you want to find the size of a specific file or directory on a remote Linux server, you can use SSH to connect to the server and run the du or ls command remotely. For example, to find the size of a directory named mydir on a remote server with IP address 192.168.0.100, you can run ssh user@192.168.0.100 du -sh mydir or ssh user@192.168.0.100 ls -lh mydir.

These are just a few examples of how you can use Linux commands to find file and directory sizes. The du and ls commands offer useful and straightforward ways to get the information you need. Experiment with different options and combinations to suit your specific needs.

For more information about the du and ls commands, you can refer to the official documentation:

- du command documentation: https://man7.org/linux/man-pages/man1/du.1.html

- ls command documentation: https://man7.org/linux/man-pages/man1/ls.1.html

More Articles from the The Linux Guide: From Basics to Advanced Concepts series:

Running a Script within a Bash Script in Linux

Learn how to execute a script within a Bash script in a Linux environment. Discover the syntax for calling a script within a script and how to chain … read more

How To Send A Header Using A HTTP Request With cURL

Including headers in a cURL call to send HTTP requests in Linux is a crucial skill for software engineers. This article provides a concise guide on h… read more

Parent Variable Accessibility in Bash Scripts

This tutorial offers an in-depth exploration of how a bash script in Linux can access its parent's variables. The article covers topics such as acces… read more

Indentation Importance in Bash Scripts on Linux

Correct indentation is crucial in bash scripts on Linux. It plays a significant role in script readability, execution, and maintainability. This arti… read more

Executing Scripts in Linux Without Bash Command Line

Executing scripts in Linux without using the bash command line is a topic that software engineers often encounter. This article explores various alte… read more

How to Make a Bash Script Continue to Run After an Error

Bash scripts are a powerful tool for automating tasks in Linux systems. However, when errors occur, scripts often stop running, causing delays and in… read more

How to Post JSON Data with Curl in Linux

Posting JSON data with Curl in a Linux environment is made easy with this simple guide. From installing Curl to handling the response, this article p… read more

Formatting and Displaying Dates with Bash Scripts in Linux

Learn how to format and display dates using bash scripts in Linux. This article covers basic and advanced formatting options, manipulating dates, ext… read more

Executing Bash Scripts Without Permissions in Linux

This guide explores various methods to run bash scripts in a Linux system without requiring permissions. The article covers topics such as executing … read more

Locating Largest Memory in Bash Script on Linux

When working on Linux, it is important to be able to identify the largest memory in a bash script. This article will guide you through the process of… read more