calculating-relative-average-subdirectory-filesizes.md (1827B)
1 --- 2 title: "Calculating relative average subdirectory filesizes" 3 date: 2020-08-21T12:37:00 4 lastmod: 2026-09-30T22:29:24 5 tags: [ "Guides", "Linux", "Media", "Snippets", "Software" ] 6 --- 7 8 During these _unprecedented times_ I've been watching a fair amount of movies and TV shows, and deleting once done, as you do. As a bit of a interesting insight and guiding hand I've been using the excellent [ncdu](https://dev.yorhel.nl/ncdu) and [rclone's ...clone ](https://rclone.org/commands/rclone_ncdu/) which both work excellently for when 1 folder equates to 1 _media_. With TV shows however this is complicated somewhat. In steps calculating average filesizes in a directory so you can sort them revealing the most notorious offenders. 9 10 I had a quick look online but couldn't find anything that really did what I wanted so I wrote it myself in an extremely verbose fashion. Eventually I slimmed it down to the following function: 11 ``` 12 for i in *; do s=$(du -s "$i" | awk '{print $1}') && c=$(find "$i" -type f -size +1M | wc -l) && echo "$((s / c)) $c $s $i"; done | sort -nr 13 ``` 14 15 What this does is: 16 - Loops every item in the folder using `for` 17 - Calculates the size `$s` using `du` 18 - Counts the items in the folder `$c` using `find` ignoring small files, like subtitles 19 - Divides the two to get the average 20 - Prints the list, then reverse sorts it 21 22 Giving you your results. Ta~da! 23 24 25 **Update 2026-09-30** 26 I've pretty much rewritten this whole snippet to: 27 * Fix an issue with files in your working directory breaking the division calculation 28 * Allow you to specify any directory (defaulting to `.`) 29 * Properly render the text in equally spaced columns 30 * Render filesizes as human readable MB, GB, etc. 31 * Integrate all of the above [into my NixOS setup](https://github.com/breadcat/nix-configs/blob/main/scripts/average-relative.nix)