Try running this command, I think you'll like it
find /path/to/dir -type f -print0 | xargs -0 du -s | sort -rn | awk 'NR>5 {print $NF}' | xargs rm -f
This will print all files underneath the /path/to/dir
directory, compute the size of each file, sort by size, extract out the names of all (except the top 5) files, and pass that to rm
.
To perform this on each directory individually, you're better off wrapping it in a script, like
#!/bin/bash
for DIR in `find /path -maxdepth 1 -type d`
do
find ${DIR} -type f -print0 | xargs -0 du -s | sort -rn | awk 'NR>5 {print $NF}' | xargs rm -f
done
Where /path
is the parent directory that contains all of your sibling directories. This will accomplish the same thing that @TomNewton describes by individually executing the workflow on each sibling directory.