blog.minskio.co.uk

Content and theme behind minskio.co.uk
Log | Files | Refs

calculating-relative-average-subdirectory-filesizes.md (1346B)


      1 ---
      2 title: "Calculating relative average subdirectory filesizes"
      3 date: 2020-08-21T12:37:00
      4 tags: [ "Guides", "Linux", "Media", "Snippets", "Software" ]
      5 ---
      6 
      7 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.
      8 
      9 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:
     10 ```
     11 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
     12 ```
     13 
     14 What this does is:
     15 - Loops every item in the folder using `for`
     16 - Calculates the size `$s` using `du`
     17 - Counts the items in the folder `$c` using `find` ignoring small files, like subtitles
     18 - Divides the two to get the average
     19 - Prints the list, then reverse sorts it
     20 
     21 Giving you your results. Ta~da!