ubuntu how to list subfolder and their file size
- User
ubuntu how to list subfolder and their file size
- Assistant
In Ubuntu (or any Linux-based system), you can list subfolders and their file sizes using a combination of commands. The
du(disk usage) command is particularly useful for this purpose. Here's a step-by-step guide on how to achieve this:Using
du:-
Open Terminal: You can open Terminal by searching for it in the applications menu or using the keyboard shortcut
Ctrl + Alt + T. -
Navigate to the desired directory: Use the
cdcommand to navigate to the directory you want to inspect. For example:bashcd /path/to/your/directory -
Run the
ducommand:To list all subdirectories and their sizes in a human-readable format, use the following command:
bashdu -h --max-depth=1-hstands for human-readable, which displays sizes in a more readable format (e.g., K, M, G).--max-depth=1limits the depth of directories to be listed to just one level (i.e., subdirectories directly under the current directory).
-
Example Output:
An example output might look like this:
plaintext4.0K ./subdir1 20M ./subdir2 256K ./subdir3 100M .
Using
duwithsort:If you want to sort the subdirectories by their size, you can use
sort. For example, to sort them in descending order:bashdu -h --max-depth=1 | sort -hrExplanation:
sort -hrsorts the output in human-readable format and reverses the order (largest first).
Script for more detailed output:
If you want a more detailed script that lists each subdirectory and the size of files within them recursively, you can create a small script. Here’s an example using a recursive approach:
Script:
bash -