algorithm - Keep only largest/smallest sized versioned file each in a directory? -
in directory thousands of versioned files (ls -v format, filename ... filename (n) ), how keep largest or smallest sized (in bytes, not in version number) version of each file? bonus if possible keep both smallest , largest, should needed. keep in delete others.
any usual unix shell tools, preferaby avoiding xargs (host system doesnt have xargs installed).
it can trusted filename ending in (number).ext versioned file.
it took little while started, because ls -s did not work (this low end soho file server box 2.6 kernel , ancient low mem versions of anything), core of idea sort each file's versions sort, if chmod, owner , group same, sort according size.
ls -1p | grep -v / | sed 's/ *([0-9]*)//' | sed 's/\.ext//' | uniq | \ awk '{system("ls -l \""$0"\"\* | sort -r | head -n 1")}' | \ sed 's/.*[0-9][0-9]:[0-9][0-9] //' > largest.txt
a fancy solution remove both smallest , largest inside awk system call awk , run rm remaining entries, awk rusty.
Comments
Post a Comment