Using Linux Commands to Find File and Directory Sizes

Avatar

By squashlabs, Last Updated: October 20, 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 Find Files Based On Wildcard In Linux

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

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

Related Article: How to Copy a Folder from Remote to Local Using Scp in Linux

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

Tutorial: Using Unzip Command in Linux

This article provides a detailed guide on how to use the unzip command in Linux for file extraction. It covers various topics such as basic usage, listing files in a zip... read more

Tutorial on Linux User Management: How to Create a User

User management is a crucial aspect of the Linux operating system, and this article provides a guide on creating and managing users. From adding users to groups and... read more

How to Apply Chmod 777 to a Folder and its Contents in Linux

Applying Chmod 777 permissions to a folder and its contents in Linux can be done easily by following a few simple steps. This step-by-step guide will walk you through... read more

How to Sync Local and Remote Directories with Rsync

Learn how to use Rsync to synchronize local and remote directories in Linux with this step-by-step tutorial. Discover the installation and configuration process, as well... read more

How to Alter the Echo Output Colors in Linux

This article provides a simple guide on modifying the output color of echo in Linux using bash color. It covers two methods: using ANSI escape sequences and using tput.... read more

How To Stop A Process Running On A Specific Port In Linux

Guide on terminating a process running on a particular port in Linux. Learn how to stop a process using the lsof and fuser commands. Additionally, find some useful notes... read more