Linux View Directory Size: Command to Check the Size of a Directory

Share On

Introduction

When working with Linux systems, it is often necessary to check the size of a directory. This information can be useful for various reasons, such as identifying large directories that are taking up a significant amount of disk space or monitoring the growth of specific directories over time. In this article, we will explore various commands that can be used to view the size of a directory in Linux.

What is the command to view the size of a directory in Linux?

In Linux, the “du” command is commonly used to view the size of a directory. The “du” command stands for “disk usage” and provides information about the disk space used by files and directories. By default, the “du” command displays the size of each directory and its subdirectories in bytes.

Using the “du” command

The “du” command offers several options that can be used to customize the output and provide more detailed information about the size of a directory. Let’s explore some of the most commonly used options:

1. du

The simplest form of the “du” command is just “du” followed by the name of the directory you want to check. For example:

du /path/to/directory

This will display the size of the specified directory and its subdirectories in bytes.

2. du -h

If you prefer a more human-readable output, you can use the “-h” option. This will display the sizes in a more understandable format, such as kilobytes (KB), megabytes (MB), or gigabytes (GB). For example:

du -h /path/to/directory

This will display the size of the specified directory and its subdirectories in a human-readable format.

3. du -sh

If you only want to see the total size of the directory without the detailed breakdown of subdirectories, you can use the “-s” option. This option stands for “summarize” and provides a summary of the disk usage. For example:

du -sh /path/to/directory

This will display the total size of the specified directory without the detailed breakdown.

4. du -ch

If you want to see the total size of the directory along with the sizes of individual subdirectories, you can use the “-c” option. This option stands for “total” and provides a grand total of the disk usage. For example:

du -ch /path/to/directory

This will display the total size of the specified directory along with the sizes of individual subdirectories.

5. du -ah

If you want to see the sizes of all files and directories within the specified directory, including hidden files, you can use the “-a” option. This option stands for “all” and includes all files and directories in the output. For example:

du -ah /path/to/directory

This will display the sizes of all files and directories within the specified directory, including hidden files.

6. du -ah –max-depth=1

If you want to limit the depth of the directory tree displayed in the output, you can use the “–max-depth” option. This option allows you to specify the maximum depth of the directory tree to be displayed. For example:

du -ah --max-depth=1 /path/to/directory

This will display the sizes of all files and directories within the specified directory, up to a maximum depth of 1.

7. du -ah –max-depth=1 | sort -hr

If you want to sort the output by size in descending order, you can use the “sort” command in conjunction with the “du” command. The “-r” option is used to sort in reverse order, and the “-h” option is used to sort human-readable numbers. For example:

du -ah --max-depth=1 | sort -hr

This will display the sizes of all files and directories within the specified directory, sorted by size in descending order.

8. du -ah –max-depth=1 | sort -h

If you want to sort the output by size in ascending order, you can use the “sort” command with the “-h” option. For example:

du -ah --max-depth=1 | sort -h

This will display the sizes of all files and directories within the specified directory, sorted by size in ascending order.

9. du -ah –max-depth=1 | sort -k1,1h

If you want to sort the output by the first column (size) in ascending order, you can use the “sort” command with the “-k” option. The “-k1,1” option specifies that the sorting should be done based on the first column. For example:

du -ah --max-depth=1 | sort -k1,1h

This will display the sizes of all files and directories within the specified directory, sorted by size in ascending order based on the first column.

10. du -ah –max-depth=1 | sort -k1,1hr

If you want to sort the output by the first column (size) in descending order, you can use the “sort” command with the “-k” option and the “-r” option. For example:

du -ah --max-depth=1 | sort -k1,1hr

This will display the sizes of all files and directories within the specified directory, sorted by size in descending order based on the first column.

11. du -ah –max-depth=1 | sort -k1,1h | head -n 10

If you only want to see the top N largest files or directories, you can use the “head” command in conjunction with the “du” and “sort” commands. The “-n” option is used to specify the number of lines to display. For example:

du -ah --max-depth=1 | sort -k1,1h | head -n 10

This will display the top 10 largest files or directories within the specified directory, sorted by size in ascending order.

12. du -ah –max-depth=1 | sort -k1,1h | tail -n 10

If you want to see the bottom N smallest files or directories, you can use the “tail” command in conjunction with the “du” and “sort” commands. For example:

du -ah --max-depth=1 | sort -k1,1h | tail -n 10

This will display the bottom 10 smallest files or directories within the specified directory, sorted by size in ascending order.

13. du -ah –max-depth=1 | sort -k1,1h | tail -n 10 | awk ‘{print $2}’

If you only want to see the names of the top N largest files or directories, you can use the “awk” command in conjunction with the “du”, “sort”, and “tail” commands. The “awk” command is used to extract the second column (name) from the output. For example:

du -ah --max-depth=1 | sort -k1,1h | tail -n 10 | awk '{print $2}'

This will display the names of the top 10 largest files or directories within the specified directory, sorted by size in ascending order.

14. du -ah –max-depth=1 | sort -k1,1h | tail -n 10 | awk ‘{print $2}’ | xargs du -sh

If you want to see the sizes of the top N largest files or directories along with their names, you can use the “xargs” command in conjunction with the “du”, “sort”, “tail”, and “awk” commands. The “xargs” command is used to pass the names of the files or directories as arguments to the “du” command. For example:

du -ah --max-depth=1 | sort -k1,1h | tail -n 10 | awk '{print $2}' | xargs du -sh

This will display the sizes of the top 10 largest files or directories within the specified directory, along with their names.

15. du -ah –max-depth=1 | sort -k1,1h | tail -n 10 | awk ‘{print $2}’ | xargs du -sh | sort -hr

If you want to sort the output by size in descending order after displaying the sizes of the top N largest files or directories, you can use the “sort” command again. For example:

du -ah --max-depth=1 | sort -k1,1h | tail -n 10 | awk '{print $2}' | xargs du -sh | sort -hr

This will display the sizes of the top 10 largest files or directories within the specified directory, along with their names, sorted by size in descending order.

16. du -ah –max-depth=1 | sort -k1,1h | tail -n 10 | awk ‘{print $2}’ | xargs du -sh | sort -h

If you want to sort the output by size in ascending order after displaying the sizes of the top N largest files or directories, you can use the “sort” command with the “-h” option. For example:

du -ah --max-depth=1 | sort -k1,1h | tail -n 10 | awk '{print $2}' | xargs du -sh | sort -h

This will display the sizes of the top 10 largest files or directories within the specified directory, along with their names, sorted by size in ascending order.

17. du -ah –max-depth=1 | sort -k1,1h | tail -n 10 | awk ‘{print $2}’ | xargs du -sh | sort -k1,1h

If you want to sort the output by size in ascending order based on the first column after displaying the sizes of the top N largest files or directories, you can use the “sort” command with the “-k” option. For example:

du -ah --max-depth=1 | sort -k1,1h | tail -n 10 | awk '{print $2}' | xargs du -sh | sort -k1,1h

This will display the sizes of the top 10 largest files or directories within the specified directory, along with their names, sorted by size in ascending order based on the first column.

18. du -ah –max-depth=1 | sort -k1,1h | tail -n 10 | awk ‘{print $2}’ | xargs du -sh | sort -k1,1hr

If you want to sort the output by size in descending order based on the first column after displaying the sizes of the top N largest files or directories, you can use the “sort” command with the “-k” option and the “-r” option. For example:

du -ah --max-depth=1 | sort -k1,1h | tail -n 10 | awk '{print $2}' | xargs du -sh | sort -k1,1hr

This will display the sizes of the top 10 largest files or directories within the specified directory, along with their names, sorted by size in descending order based on the first column.

19. du -ah –max-depth=1 | sort -k1,1h | tail -n 10 | awk ‘{print $2}’ | xargs du -sh | sort -k1,1h | head -n 5

If you only want to see the top N largest files or directories, you can combine the “head” command with the “sort” command. For example:

du -ah --max-depth=1 | sort -k1,1h | tail -n 10 | awk '{print $2}' | xargs du -sh | sort -k1,1h | head -n 5

This will display the sizes of the top 5 largest files or directories within the specified directory, along with their names, sorted by size in ascending order based on the first column.

20. du -ah –max-depth=1 | sort -k1,1h | tail -n 10 | awk ‘{print $2}’ | xargs du -sh | sort -k1,1h | tail -n 5

If you want to see the bottom N smallest files or directories, you can combine the “tail” command with the “sort” command. For example:

du -ah --max-depth=1 | sort -k1,1h | tail -n 10 | awk '{print $2}' | xargs du -sh | sort -k1,1h | tail -n 5

This will display the sizes of the bottom 5 smallest files or directories within the specified directory, along with their names, sorted by size in ascending order based on the first column.

Conclusion

Checking the size of a directory in Linux is a common task that can be easily accomplished using the “du” command. By using various options and combining it with other commands like “sort”, “head”, and “tail”, you can obtain detailed information about the disk usage of a directory and its subdirectories. Whether you need a simple summary or a more detailed breakdown, the “du” command provides the flexibility to meet your needs.

FAQs

1. Can I use the “du” command to check the size of a remote directory?

Yes, you can use the “du” command to check the size of a remote directory by specifying the remote path in the command. For example:

du -sh user@remote:/path/to/directory

This will display the total size of the remote directory without the detailed breakdown.

2. How can I exclude certain directories from the “du” command?

You can exclude certain directories from the “du” command by using the “–exclude” option followed by the path of the directory you want to exclude. For example:

du -sh --exclude=/path/to/exclude /path/to/directory

This will display the total size of the directory excluding the specified directory.

3. Can I use the “du” command to check the size of a directory on a specific filesystem?

Yes, you can use the “du” command to check the size of a directory on a specific filesystem by specifying the mount point of the filesystem in the command. For example:

du -sh --inodes /path/to/directory

This will display the total size of the directory on the specified filesystem.

Similar Posts

Leave a Reply

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