How to Be Faster at the Linux Command Line



Want to be faster at the Linux command line interface? Since most Linux distributions provide Bash as the default CLI, here are some Bash tricks that will help cut down the amount of typing needed to execute commands. Feel free to comment and share your own speed tricks.

Control-R Through Your History

This is my most used shortcut. Hit Control-R and begin to type a string. You immediately get the last command in your Bash history with that string. Hit Control-R again to cycle further backwards in your history.

For instance, type the following and hit Enter.

grep root /etc/passwd

Then hit Control-R and begin to type 'grep'.

Control-R
(reverse-i-search)`gre': grep root /etc/passwd

When you see the original command listed, hit Enter to execute it. Alternatively, you can also hit the Right-Arrow to edit the command before running it.

Use History Expansion

Bash's command history can be referenced using the exclamation mark. For instance, typing two exclamation marks (!!) will re-execute the last command. The next example executes date twice:

date
!!

If you are interested in more than just the last command executed, type history to see a numbered listing of your Bash's history.

history
  39 grep root /etc/passwd
  40 date
  41 date
  42 history

Since grep root /etc/passwd is command number 39, you can re-execute it like so:

!39

You can also reference Bash's history using a search string. For instance, the following will run the last command that started with 'grep'.

!grep

Note, you can set the number of commands stored in your history by setting HISTSIZE.

export HISTSIZE=1000

You can also wipe your history clear with the -c switch.

history -c

Use History Quick Substitution

Historical commands can be edited and reused with quick substitution. Let's say you grep for 'root' in /etc/passwd:

grep root /etc/passwd

Now, you need to grep for 'root' in /etc/group. Substitute 'passwd' for 'group' in the last command using the caret (^).

^passwd^group

The above command will run:

grep root /etc/group

Use Vi or Emacs Editing Mode

You can further enhance your abilities to edit previous commands using Vi or Emacs keystrokes. For example, the following sets Vi style command line editing:

set -o vi

After setting Vi mode, try it out by typing a command and hitting Enter.

grep root /etc/passwd

Then, Up-Arrow once to the same command:

Up-Arrow
grep root /etc/passwd

Now, move the cursor to the 'p' in 'passwd' and hit Esc.

grep root /etc/passwd
               ^

Now, use the Vi cw command to change the word 'passwd' to 'group'.

grep root /etc/group

For more Vi mode options, see this list of commands available in Vi mode. Alternatively, If you prefer Emacs, use Bash's Emacs mode:

set -o emacs

Emacs mode provides shortcuts that are available through the Control and Alt key. For example, Control-A takes you to the beginning of the line and Control-E takes you to the end of the line. Here is a list of commands available in Bash's Emacs mode.

Use Aliases and Functions

Bash allows for commands, or sets of commands, to be aliased into a single instruction. Your interactive Bash shell should already load some useful aliases from /etc/profile.d/. For one, you probably have ll aliased to ls -l.

If you want to see all aliases loaded, run the alias Bash builtin.

alias

To create an alias, use the alias command:

alias ll='ls -l'

Here are some other common aliases:

alias ls='ls --color=tty'
alias l.='ls -d .* --color=auto'
alias cp='cp -i'
alias mv='mv -i'

Note that you can also string together commands. The follow will alias gohome as cd , then run ls. Note that running cd without any arguments will change directory to your $HOME directory.

alias gohome='cd; ls'

Better yet, only run ls if the cd is successful:

alias gohome='cd && ls || echo "error($?) with cd to $HOME"'

More complex commands can be written into a Bash function. Functions will allow you to provide input parameters for a block of code. For instance, let's say you want to create a backup function that puts a user inputted file into ~/backups.

backup() {
  file=${1:?"error: I need a file to backup"}
  
  timestamp=$(date '+%m%d%y')
  backupdir=~/backups
  
  [ -d ${backupdir} ] || mkdir -p ${backupdir}
  cp -a ${file} ${backupdir}/$(basename ${file}).${timestamp}
  return $?
}

Like the example above, use functions to automate small, daily tasks. Here is one I use to set my xterm title.

xtitle() {
  unset PROMPT_COMMAND
  echo -ne "\033]0;${@}\007"
}

Of course, you can use functions together with aliases. Here is one I use to set my xterm title to 'MAIL' and then run Mutt.

alias mutt='xtitle "MAIL" && /usr/bin/mutt'

Finally, to ensure that your custom aliases and functions are available each login, add them to your .bashrc.

vim ~/.bashrc