These are just a few tricks that have come in handy lately that I thought I’d pull out of my .bash_profile and stick on a web page.

lsz

This first one is a simple, universal shortcut for getting lists of files inside of various archive formats. Not that it’s terribly hard to do (tar tvf file.tgz), but why not just create one command to rule them all (and make it really short)? This is inspired by the extract function, which does similar for extracting files. I don’t know offhand who to credit with that, but you can find it in the bash-it distribution.

lsz.shraw
"
# ls archives (inspired by `extract`)
lsz() {
	if [ $# -ne 1 ]
	then
		echo "lsz filename.[tar,tgz,gz,zip,etc]"
		return 1
	fi
	if [ -f $1 ] ; then
		case $1 in
			*.tar.bz2|*.tar.gz|*.tar|*.tbz2|*.tgz) tar tvf $1;;
			*.zip)  unzip -l $1;;
			*)      echo "'$1' unrecognized." ;;
		esac
	else
		echo "'$1' is not a valid file"
	fi
}

Put that in your .bash_profile and next time you want to know what’s in an archive, just type lsz filename.tgz (think “list zip”). It currently works with zip and tar (including gzip, and bzip) files. Plenty of room to add more filetypes if you have other compression/archiving formats.

psgrep

This one is mostly in Ruby because I go down rabbit holes any time I start playing with sed and awk. It’s an alias that lets you scan running processes and quickly find the pid and name.

a.rbraw
"
# updated per Lri's comment
alias psgrep="ps -Aco pid,comm | sed 's/^ *//'| sed 's/:/ /'|grep -iE"

It accepts a partial, case-insensitive search string, or you can use regex (grep extended), like this: psgrep 'growl$' to match “growl” but not “growlhelper.” I use a similar trick for quickly finding and killing processes with an interactive menu called fk.

lb

I love this one. It opens the current directory or a specified file in LaunchBar, ready for action. Hit tab and you can quickly copy, move, run a Service on it, open it in an app… whatever’s handy.

a.rbraw
"
# Select the current directory in launchbar, optionally a file
lb () {
	if [[ $# = 1 ]]; then
		[[ -e "$(pwd)/$1" ]] && open "x-launchbar:select?file=$(pwd)/$1" || open "x-launchbar:select?file=$1"
	else
		open "x-launchbar:select?file=$(pwd)"
	fi
}

pbgist

If you use Gist and the command line, you probably have “jist” installed. If not, run gem install jist and make your life easier. This extremely short alias is great for popping a clipboard straight to a public gist and opening the link in your browser.

alias pbgist="jist -Ppo"

chgext

chgext batch changes the extension of all of the specified files in a directory. It takes two arguments, first the original extension, then the extension to replace it with. For example chgext html php will turn a directory of HTML files into PHP files. Magic.

I had to give this one a longer name because I don’t use it so often that I would remember a clever short one. chg+ TAB is easy enough for me to find this when I need it.

a.rbraw
"
# batch change extension (fix from Lri, again)
chgext() {
	for file in *.$1 ; do mv "$file" "${file%.$1}.$2" ; done
}

ql

This last one isn’t anything new, but it’s extremely useful (at least for web developers), so I figured I’d mention it. It calls qlmanage, the command line interface for Quick Look, and opens a Quick Look preview of whatever file you run it on.

alias ql="qlmanage -p &>/dev/null"

Someday I’ll actually get my entire dotfile setup onto GitHub, but don’t hold your breath.

Ryan Irelan has produced a series of shell trick videos based on BrettTerpstra.com posts. Readers can get 10% off using the coupon code TERPSTRA.