samba /.bash_aliases

A work in progress. Shortcuts to emulate 'vim' commands, and some handy shortcuts for manipulating/examining files/directories.
#!/bin/bash

# COLORIZING {{{
if [ "$TERM" != "dumb" ] && [ -x /usr/bin/dircolors ]; then
    eval "`dircolors -b`"
    alias ls='ls --color=auto'
    alias dir='ls --color=auto --format=vertical'
    alias vdir='ls --color=auto --format=long'

    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
fi
# }}}
# FILE LISTING {{{
alias ll='ls -l'
alias la='ls -A'
alias l='ls -CF'
# }}}
# VIM EMULATION - some friendly shortcuts {{{
alias :q='exit'		
alias :s='history -a'	# save the shell history
alias :m='pushd'	# store a path
alias :d='popd'		# pop a path
alias :marks='dirs -p'	# show all paths
# }}}
# GNU SCREEN {{{
# set the terminal type properly
alias screen="screen -T ${TERM}"
# wrap to simplify work from multiple locations
alias scr="screen -T ${TERM} -rx -S default"
# }}}
# Process Listing {{{
# set my preferred PS output format
PS_FORMAT="pid,%cpu,%mem,euser:15,command"
PS_SORT="euser,%cpu"
alias ps="ps -o ${PS_FORMAT}" # list running processes more elegantly
# }}}
# find duplicate files without 'fdupes' {{{
function s.dupes () {
	echo "Scanning for duplicates: $@">&2
	find $@ -type f -exec md5sum {} \; | sort -k 1 | uniq -w 32 -D 
}
# }}}

# for each stdin; execute this command... like xargs {{{
function each () {
	singular=0; items=`mktemp`
	while getopts ":s" Option; do
		case $Option in 
			s) singular=1;;
		esac
	done

	while read item; do
		[ $singular -eq 0 ] && $@ $item || echo $item > $items;
	done

	[ $singular -ne 0 ] && $@ `cat $items`;

	rm $items
}
# }}}

# friendly debian extractor... {{{
function deb.extract () {
	while getopts ":f:d:" Option; do
		case $Option in
			f) debfile=$OPTARG;;
			d) outputdir=$OPTARG;;
		esac
	done
	if [[ -z $debfile || ! -f $debfile ]]; then
		# usage
		echo "deb.extract -f  [ -d  ]";
	else
		[ -z $outputdir ] && outputdir=`basename $debfile .deb`
		mkdir -p $outputdir/DEBIAN
		dpkg-deb -e $debfile $outputdir/DEBIAN
		dpkg-deb -x $debfile $outputdir
	fi
}
# }}}
# open terminals over a tunnel {{{
function s.ssh () {
	# example: s.ssh -d user@dest -g user@gateway
	# grab/create a tunnel and connect	
	`s.ssh_tunnel $@ | awk '{ printf ( "ssh %s -p %d" , $1, $2 ); }'`

	# report still-running SSH processes
	`which ps | grep ps` -o pid,euser:15,command -C ssh 

}
# }}}
# push files over a tunnel to another machine {{{
function s.scp () {
	# example: s.scp -d user@dest:/path/to/target -g user@gateway /path/to/recv/local
	# example: s.scp [/path/to/send/local/*...]  user@dest:/path/to/target user@gateway
	
	declare -a files
	declare mode= scp_tunnel= scp_cmd=

	until [ $# -eq 0 ]; do
		OPTIND=1
		while getopts "g:d:rs" Option; do
			case $Option in
				g) gateway=${OPTARG};;
				d) destination=${OPTARG};;
				s) mode='send';;
				r) mode='recv';;
			esac
		done
		shift $(( OPTIND - 1 ))
		until [[ $# -eq 0 || $1 =~ ^- && ${#1} -eq 2 ]]; do
			files[${#files[@]}]=${1};
			shift
		done		
	done	

	# grab/create a tunnel
	tunnel=`s.ssh_tunnel -d ${destination%%:*} -g ${gateway%%:*}`
	
	[[ $mode == 'send' ]] && scp_cmd="scp ${files[@]} ${tunnel}";
	[[ $mode == 'recv' ]] && scp_cmd="scp ${tunnel} ${files[@]}";
	[[ ${#scp_cmd} -eq 0 ]] && echo "SCP Command has zero length! Something's up...">&2

	# tell me what you're going to do.
	echo "Would do: [[${scp_cmd}]]; but we're still debugging.">&2;

	# report still-running SSH processes
	`which ps | grep ps` -o pid,euser:15,command -C ssh 

}
# }}}
# opens/selects tunnels as specified {{{
function s.ssh_tunnel () {
	declare usedports=`mktemp` gwport=$(( RANDOM + 1024 )) Destination= GWUser=${SSHDefaultGateway%%@*} GWHost=${SSHDefaultGateway##*@};
	# parse the arguments
	OPTIND=1
	while getopts "g:d:" Option; do
		case $Option in
			g) [[ $OPTARG =~ "@" ]] && GWUser=${OPTARG%%@*} || GWUser=${USER}; GWHost=${OPTARG##*@};;
			d) [[ $OPTARG =~ "@" ]] && DestUser=${OPTARG%%@*} || DestUser=${USER}; DestHost=${OPTARG##*@};;
			*) echo "Unknown option: ${Option} = ${OPTARG}">&2;;
		esac
	done
	[ -z ${DestHost} ] && echo "No destination specified. Please specify.">&2 && return 13;
	[ -z ${GWHost} ] && echo "No gateway specified. Please specify, or set a default.">&2 && return 17;

	echo "Tunnel: ${GWUser}@${GWHost} >> ${DestUser}@${DestHost}">&2

	# make sure we're not trying to bind to an occupied port
	netstat -natl | sed '1,2d' | awk '{print $4}' | sed -r 's/(.*):([0-9]+)/\2/' | sort | uniq > ${usedports}
	while [ `grep ${gwport} ${usedports}` ]; do gwport=$(( RANDOM + 1024 )); done
	
	# search for existing tunnels to destination, using the same gateway
	`which ps | grep ps` -o pid,command -C ssh | grep -E "[0-9]+:${DestHost}:22 ${GWUser}@${GWHost}" > ${usedports}
	if [[ `wc -l ${usedports} | cut -d ' ' -f 1` -gt "0" ]]; then
		# use existing tunnel
		gwport=`sed -r "s/^(.*) ([0-9]+):${DestHost}:22 (.*)$/\2/" ${usedports}`
		echo "Found gateway port: ${gwport}">&2
	else
		# create a new tunnel
		echo "Creating new tunnel on port: ${gwport}">&2
		ssh -NfL ${gwport}:${DestHost}:22 ${GWUser}@${GWHost}
	fi	
	# clear our data from the disk
	rm ${usedports}

	# print the connection info	
	echo "${DestUser}@localhost ${gwport}";
}
# }}}
# quick and dirty archiver - lets me datestamp stuff easily {{{
function s.arc () {
	declare stamp= directory= extension='bz2' compress='--bzip2' verbose= delete= target=
	OPTIND=1
	while getopts "d:f:bzsvr" Option; do
#	echo "Reading option: ${Option} ${OPTIND} ${OPTARG}" >&2
	case $Option in
		s) stamp=".[`date +%F\ %H.%M.%S`]";;
		d) directory=${OPTARG};;
		b) compress='--bzip2'; extension='bz2';;
		z) compress='--gzip'; extension='gz';;
		v) verbose='v';;	
		r) delete='--remove-files';;
		f) target=${OPTARG};;
		*) echo "Unknown Option: ${Option} [$OPTARG]">&2;;
	esac
	done
#	echo "OPTIND: ${OPTIND}" >&2
	[ -z ${directory} ] && echo "Specify a directory with the -d option.">&2 && return 91;
	[ ! -d ${directory} ] && echo "I make archives out of *directories* [${directory}].">&2 && return 97;
	[ -z ${target} ] && target="`basename "${directory}"`${stamp}.tar.${extension}"
#	echo "Directory: ${directory}" >&2
	echo "Target: ${target}">&2
	tar -c${verbose}f "${target}" ${directory} ${delete} ${compress}
}
# }}}



# vim: foldenable foldmethod=marker