#!/bin/sh #defrag v0.01 by Con Kolivas /dev/null abort() { echo Aborting rm -f tmpfile dirlist exit 1 } fail() { echo Failed abort } declare -i maxsize=`awk '/MemTotal/ {print $2}' /proc/meminfo` declare -i filesize=0 (( maxsize/= 3)) if [[ -a tmpfile || -a dirlist ]] ; then echo dirlist or tmpfile exists abort fi find -printf "%p %s\n" | sort -nr +1 | awk '{print $1}' > dirlist if (( $? )) ; then fail fi for i in `cat dirlist` do if [[ ! -f $i ]]; then continue; fi filesize=`find $i -printf "%s"` # read the file first to cache it in ram if possible if (( filesize < maxsize )) ; then cat $i > /dev/null fi cp -a -f $i tmpfile if (( $? )) ; then fail fi mv -f tmpfile $i if (( $? )) ; then fail fi done rm -f dirlist