How to show the disk usage of each subdirectory in linux?

Member

by rylan , in category: General Help , a year ago

How to show the disk usage of each subdirectory in linux?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by cathrine_goyette , a year ago

@rylan 

To show the disk usage of each subdirectory in Linux, you can use the du command with the -h (human-readable) and -d (depth) options.


The basic syntax of the command is as follows:

1
du -hd1 <directory_path>


Here, <directory_path> specifies the path to the parent directory that you want to analyze. The -h option makes the output human-readable by displaying sizes in kilobytes (K), megabytes (M), and gigabytes (G). The -d1 option limits the depth of the output to only the immediate subdirectories (depth 1).


Example: Let's assume you want to check the disk usage of each subdirectory in the current working directory. You can use the command:

1
du -hd1 .


This will display the disk usage of each subdirectory in the current directory. The output will show the size and the name of each subdirectory.

by buster.gaylord , a year ago

@rylan 

For a more detailed output, you can use the tree command along with the du command by piping the output. The tree command organizes the output in a hierarchical manner.


1


tree -h <directory_path> | du --max-depth=1 -h --total


In this command, <directory_path> represents the path to the parent directory you want to analyze. The tree command is used to create a visual representation of the directory structure, and the du command is used to calculate the disk usage of each subdirectory. The --max-depth=1 option limits the depth of the output to only the immediate subdirectories, and the --total option shows the total disk usage of all the subdirectories.


For example, to show the disk usage of each subdirectory in the current working directory using the tree and du commands:


1


tree -h . | du --max-depth=1 -h --total


This will display the disk usage of each subdirectory in the current directory, along with the total disk usage of all the subdirectories. The output will be presented in a tree-like format.