Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have 100s of sibling directories on a BSD/Unix system, each with a collection of files in it. I'd like to delete all but the N largest files in each directory. (N is 5 for me.)

Assuming that I can use some 'find' magic for this, but not quite sure how.

share|improve this question

1 Answer 1

up vote 4 down vote accepted

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.

share|improve this answer
    
Getting close. It looks like this treats the whole lot as one sort. It ends up deleting everything but the 5 biggest files across all directories. I need the five biggest files in each directory. –  Laizer Aug 10 '11 at 19:24
    
Seems like you might have to wrap this in a bash "for" loop to do each of your sibling directories. Suggest replacing "rm -f" with "echo" until you're 100% happy too! Seems from your comment, which I missed, you are putting /path/to/dir as your top dir. Scott meant that to be each sibling, so that line needs to be executed once per sibling, eg: for i in master/* do find $i -type f.... ; done –  Tom Newton Aug 10 '11 at 19:26
    
Could replace awk ... by tail -n +6. –  jfgagne Aug 10 '11 at 19:28
    
@jfgagne Since the du is of the form "size filename" (needed for sorting) we still need the awk to strip out the size column. Otherwise you would need to replace it with a combination of tail and cut. –  Scott Pack Aug 10 '11 at 19:40
    
@Scott: true, I did not see the cut involved in awk . Using cut and tail makes the solution easier to understand, don't you think ? –  jfgagne Aug 10 '11 at 19:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.