How to Instantly Insert Your Most Used Bash Commands

Toby Osbourn Blog developer, tech tools, web developer

There are commands we don’t run often enough to commit to muscle memory. Here are the most common ones:

Cleaning up merged branches

When using version control for a long-lived project, it’s easy to accumulate branches. This can get messy, so it pays to tidy up your merged in branches.

git branch --merged | egrep -v "(^\*|master|dev)" | xargs git branch -d

Breaking this down:

  • git branch --merged returns all branches that have been merged.
  • egrep -v "(^\*|master|dev)" filters out all those branches called ‘master’ or ‘dev’, we won’t want to delete those.
  • xargs git branch -d will take each branch left and pass it into git branch -d which will delete the branch.

Finding out what is running on a port

With local services and servers running all at once, often something will hog a port and not shut down. We can stop the process, but first we need to find the port

lsof -i tcp:5432

lsof stands for “list open files” and we’re passing in tcp connections on port 5432.

Decompressing

A common task is taking a .tar file, which is a compressed folder containing other files, and expanding it out to take the contents.

tar -xvzf my_compressed_file.tar.gz

This is what each flag is doing:

  • x – This stands for extract, in other words get the files from the compressed file.
  • v – verbose, this returns the files we’ve extracted
  • z – filter the output through gzip
  • f – tell is which file to tar, in this case my_compressed_file.tar.gz

Aliasing

A common way to store commands in your terminal is to create an alias. To do this, we append to the .bash_profile file in your home directory the alias you want to make.

If you always call ls with the flags -lah you could write an alias like this:

alias ls='ls -lah'

Or perhaps you always mistype git as gut:

alias gut=git

The problem with aliasing is once the aliases are made they aren’t particularly easy to look up.

TextExpander for bash commands

Because TextExpander is in your menu bar, you can easily access the snippets without having to remember what to type. Normally we’d suggest making a memorable term, with things you don’t type that often being able to quickly browse great for your productivity.

What are your favourite commands you have to look up?

We had a lot of fun asking our friends and co-workers about this, please let us know @TextExpander and on Facebook.

Share this Post