---
title: More command line handiness
date: 2013-03-14 12:00
slug: more-command-line-handiness
categories:
- Code
tags:
- scripting
- terminal
---

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](https://github.com/revans/bash-it/blob/master/plugins/available/extract.plugin.bash) distribution.



```shell
# 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
}
```

[View the code on GitHub](https://gist.github.com/5156628)



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.



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

[View the code on GitHub](https://gist.github.com/5157745)



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](http://brettterpstra.com/2010/03/06/fk-redux/).

### 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.



```ruby
# 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
}
```

[View the code on GitHub](https://gist.github.com/5157849)



### pbgist

If you use [Gist](https://gist.github.com/ "Gists") 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.



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

[View the code on GitHub](https://gist.github.com/5157834)



### 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.

---

Brett is a writer and developer living in Minnesota, USA. You can follow him as ttscoff. This content is supported by readers like you. [More about Brett](https://brettterpstra.com/contact.md), including social links.

Copyright © 2026 Brett Terpstra. All rights reserved.
