All pastes #979652 Raw Edit

Miscellany

public text v1 · immutable
·published 2008-04-10 14:33 UTC
rendered paste body
#!/bin/bash

# Author           : Martti Kühne 
# Created On       : Thu Apr 10 03:06:54 CEST 2008
# Created On Node  : base (my laptop)
# Last Modified By : Martti Kühne
# Last Modified On : Thu Apr 10 03:06:54 CEST 2008
# Last Machine Used: base (my laptop)
# Update Count     : 1
# Status           : Tested. A Backup of the target directory is recommended though.
# HISTORY          : 
# Description      : $($progname -h)

# make sure we exit on error
set -e

# we do use globbing in this script and don't want to be bothered with error messages
shopt -s nullglob

progname="`basename \"$0\"`"
pversion='$Revision: 1.0 $'

# echoes the ascii char of $1
chr() {
#	printf \\$(printf '%03o' $1) # doesn't work
#	eval "echo \$'\\x$(printf %x $1)'"
	printf '%b\n' "$(printf '\\0%o' "$1")"
}

# echoes 0 if the first character of $1 is a digit
isnum() {
	echo ${1:0:1} | tr '[:digit:]' '0'
}

upper() {
	echo $@ | tr "[:lower:]" "[:upper:]"
}

# echoes the full path or returns 1
fullp() {
	[ -d $@ ] || return 1
	[ ${@:0:1} = "/" ] || echo -n "$(pwd)/"
	echo $@
}

usageversion() {
		cat >&2 <<EOF
GNU/Linux $progname $version
           Copyright (C) 2008 Martti Kühne.
This is free software; see the GNU General Public Licence for copying
conditions.  There is NO warranty.

Usage: $progname [options]
Options:
     -h,     --help          print this message
     -d=foo                  set the directory with the files
     -unaz                   reverse the process, removes the directories

If no directory or a relative path (not starting with '/') is given,
$progname assumes that $(pwd) is the path to start from.
The directory structure is created lower case, and the comparison assumed
to be case-insensitive.
EOF
}

# command line options
unaz=0
unpack=0
targetdir="$(pwd)"

for arg in $@; do
	case $arg in
	-h|--help)
		usageversion
		exit 0
	;;
	-d=*)
		targetdir=$(fullp "${arg#*=}")
		[ $? = "1" ] && {
			echo >&2 "$progname: error: the directory specified does not exist.";
			exit 1
		}
	;;
	-unaz)
		unaz=1
	;;
	*)
		cat >&2 <<EOF
$progname: invalid command line option.
Usage:
  $progname [ --help ] [ -d=path ] [ -unaz ]
EOF
	;;
	esac
done

# now we get all chars 0-9 and a-z
for i in `seq 48 57` `seq 97 122`; do
	char=$(chr $i)
	# put files starting with digits into 0/
	compchar=$(isnum $char)
	fullpath="$targetdir/$compchar"
	if [ $unaz = "1" ]; then
		[ -d "$fullpath" ] && {
			# oh man this was giving me the hoodoos for months...
			# basically a silly tweak, doing the whole thing backwards
			# "$fullpath/*" != "$fullpath"/*
			set +e
			mv "$fullpath"/* "$targetdir" 2>/dev/null
			set -e
			rm -r "$fullpath"
		}
	else
		# only create directories that don't exist
		if [ $char = $compchar ]; then
			files=("$targetdir"/$char* "$targetdir"/$(upper $char)*)
		else
			files=("$targetdir"/$char*)
		fi
		if [ ${#files[@]} != "0" ]; then
			[ ! -d "$fullpath" ] && mkdir "$fullpath"
			# here we go. 
			for line in "${files[@]}"; do
				[ -f "$line" ] && mv "$line" "$fullpath"
			done
		fi
	fi
done

exit 0