Thoughts on Software, Technology, and Startups

Tag
Commands

These tricks have saved me a lot of time. Many of them I started using after reading this Definitive Guide to Bash Command Line History by fellow Hacker News reader, pkrumins. It includes a much deeper look at history than the quick examples I cover here.

Search Your History Quickly

No more history | grep ... or hitting the up button 20 times. Just open a command prompt, press Ctrl-R, and begin typing a word in your command. As you type, the most recent command matching what you type will appear. To continue searching backwards in the history, hit Ctrl-R again. Then hit the left or right key to edit the command or hit enter to run it.

Increase Your History Size

Once you know how to search your history, make sure your commands stick around for a while. By default, the history size is pretty low, usually only 500. To increase your history size, add the following to either ~/.bashrc, ~/.bash_profile, or /etc/profile:

export HISTFILESIZE=1000000000export HISTSIZE=1000000

Analyze Your History

Once you've built up a sizable history, analyze it to determine possible aliases that will reduce typing time. To see the top 30 most used commands, run:

cut -f1 -d" " .bash_history | sort | uniq -c | sort -nr | head -n 30

To see the top 30 most used commands including arguments, run:

sort .bash_history | uniq -c | sort -nr | head -n 30

Stop Getting Annoyed When You Forget sudo

I used to get so mad when I forgot to sudo a command, especially a long one. Not any more. To repeat a command using sudo, run:

sudo !!

Reuse Arguments

Say you want to backup a file then edit that file. Here's how you can reuse an argument from the most recent command:

cp a-very-long-file-name.txt a-very-long-file-name.txt.bak vi !^

Also, you can use !!:N for the Nth argument, !!:N-M for the Nth to Mth arguments, !!:$ for the last argument, or !!:* for all arguments.Self Promotion