Monday, December 27, 2010

Remove all but the newest packages on hosts

Remove all but the newest packages on hosts

for h in $(ls XXX* | sed 's/\(\w*\)\..*/\1/' | sort -u ); do
ls -t $h*  | tail -n +4 
done | xargs sudo rm
 
list just packages. 
sed 's/\(\w*\)\..*/\1/'  

this turns "XXXyyy-1.23.rpm" in to XXXyyy-1
sort -u 

then only give me unique package names.
for h

indexs through each type of package.

Then we pass each package type $h* in to a ls -t that sorts by time,
tail -n +4  

this removes the first 3 lines from what it list in the ls, just the 3 newest versions
xargs 

then takes this list of files from everything before and passes it as an argument to rm

No comments: