ubuntu how to list subfolder and their file size with du , sort by file size and filter…
- User
ubuntu how to list subfolder and their file size with du , sort by file size and filter on those larger than 100MB
- Assistant
To list subfolders and their file sizes, sort them by file size, and filter those larger than 100MB in Ubuntu using the
ducommand, you can follow these steps:-
Open your terminal.
-
Use the following command to achieve the desired result:
bashdu -h --max-depth=1 | sort -hr | awk '$1 ~ /[0-9]+[MGT]/ && substr($1,1,length($1)-1)+0 > 100'Let's break down this command:
-
du -h --max-depth=1: Lists the sizes of subfolders (and files) in the current directory, with human-readable sizes (-h), and limits the depth of recursion to 1 (--max-depth=1). -
sort -hr: Sorts the results by size in human-readable form and in reverse order (largest sizes first). -
awk '$1 ~ /[0-9]+[MGT]/ && substr($1,1,length($1)-1)+0 > 100': Filters the results to only include those larger than 100MB. It works as follows:$1 ~ /[0-9]+[MGT]/: Matches sizes with MB (M), GB (G), or TB (T).substr($1,1,length($1)-1)+0 > 100: Extracts the numeric part of the size, converts it to a number, and checks if it is greater than 100.
If you want to directly test this command, you can run it from within a directory that contains several subdirectories and files. Replace
.with the target directory if you want to execute it in another location.Here's an example for a hypothetical directory structure:
bashcd /path/to/your/directory du -h --max-depth=1 | sort -hr | awk '$1 ~ /[0-9]+[MGT]/ && substr($1,1,length($1)-1)+0 > 100'This will list all subfolders in
/path/to/your/directorythat have a size larger than 100MB, sorted by size. -