archive_directory()
{
	local n=0
	local arch=5
	local arch_path=""
	
	if [ -d "$1" ]; then
		arch_path=$1
	else
		echo archive_directory: $1 not found
		return
	fi
	if [ -n "$2" ]; then
		arch=$2
	fi
	
	echo $arch_path $arch
	
	d=ARCH_`date +%Y%m%d`
	mkdir -p $d
	
	find $arch_path -type f -mindepth 1 -maxdepth 1 | while read i
	do
		mv "$i" "$arch_path/$d/"
	done
	
	# Remove ARCH_
	find $arch_path -type d -mindepth 1 -maxdepth 1 | sort -r | while read i
	do
		n=$(($n+1))
		if [ $n -le $arch ]; then
			echo "Keep $i"
			continue
		fi
		echo "Delete $i"
		rm -fr "$i"
	done
}
 
# Archiving the five last directories
archive_directory D:/temp/tmp

TAGS : Tutorial Shell Unix archive recent directory forlder Script DOS