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


Comments

This is does work well. It

This is does work well. It does makes things faster and easier to use the command line.
fort lauderdale motorcycle accident attorney

href="http://www.replicahandb

href="http://www.replicahandbagsone.com/gucci-handbags-c-13.html">replica GUCCI Handbags lower price for sale, the more fake GUCCI Handbags you choose, the cheaper price you get.
As everyone knows that the Chanel Handbags are popular in the whole world, sell replica Chanel Handbags can get more profit, many people like to sell chanel bags because fashion women love them.
Taking the Prada Handbags to join the big party can show women's fashion, so even the poor people like to buy replica Prada Handbags, so when you walk on the road, you never surprised by so much fake Prada Handbags, do you feel have a Prada replica Handbags is a cool action?
Fashion women love Miu Miu Handbgs, but the price is to high, so many ladies choose Miu Miu replica Handbgs, in the replica Miu Miu Handbgs shops, you will get the popular fake Miu Miu Handbgs that you love.

href="http://www.replicahandb

href="http://www.replicahandbagsone.com/louis-vuitton-handbags-c-17.html">Louis Vuitton Replica Handbags are lower than the real one.
Tom open a GUCCI Handbags store, but he usually sale the GUCCI Replica Handbags, all the replica GUCCI Handbags are 80% off on sale, so many fashion women like to buying fake GUCCI Handbags in there.
Chanel Handbags are the famous world brand, so the replica Chanel Handbags shops are open all over the world, you never tell out the different from the chanel bags.
Macdonad love the Prada Handbags, she offen join the big exhibitions through the cities, but one day she buy one replica Prada Handbags, and go to a big party, all the women in there can not recognise this fake Prada Handbags, so in the end, when she tell them about the Prada replica Handbags, the whole party shocked.
Fashion Miu Miu Handbgs are very popular among the young women, because the high price of Miu Miu replica Handbgs, so many of them go to the replica Miu Miu Handbgs stores and buy the fake Miu Miu Handbgs as a lowest price.

Louis Vuitton Replica Epi

Louis Vuitton Replica Epi Leather Pochette Accessoires Cassis M5298K
Louis Vuitton Replica Epi Leather Pochette Accessoires Rubis M5298M
Louis Vuitton Replica Epi Leather Pochette Demi-Lune Orange Sunset M52622
Louis Vuitton Replica Epi Leather Pont Neuf Bag Black M52052
Louis Vuitton Replica Epi Leather Pont Neuf Bag Blue 52047
Louis Vuitton Replica Epi Leather Pont Neuf PM Black M59072
Louis Vuitton Replica Epi Leather Segur Handbag Purple M58872
Louis Vuitton Replica Epi Leather Shoulder Bag Ivory M4023M
Louis Vuitton Replica Epi Leather Soufflot Handbag Cream 59032
Louis Vuitton Replica Epi Leather Soufflot Hnadbag Orange M52225
Louis Vuitton Replica Epi Leather Speedy 25 Ivory M5923J
Louis Vuitton Replica Epi Leather Speedy 30 Black M59222
Louis Vuitton Replica Epi Leather Speedy 30 Ivory M5922J
Louis Vuitton Replica Epi Leather Speedy 30 Red M59022
Louis Vuitton Replica Epi Leather Speedy 30 Red M59022
Louis Vuitton Replica Epi Leather Top Handle Bag Blue 52046
Louis Vuitton Replica Epi Leather Wallet Organizer M6359J
Louis Vuitton Replica Epi Pont-Neuf Hnadbag Orange M52042
Louis Vuitton Replica Epi Seller Dragonne Clutch Bag Black M52762
Louis Vuttion Replica Epi Leather Bagatelle PM Purple M40232
Louis Vuttion Replica Epi Leather Bowling Montaigne PM Purple M59322
Louis Vuttion Replica Epi Leather Cannes Cosmetic Bag Orange M48032
Louis Vuttion Replica Epi Leather Croisette MM Black M5250
Louis Vuttion Replica Epi Leather Dhanura PM Blue M5981H
Louis Vuttion Replica Epi Leather Ellipse Bag Orange M51127
Louis Vuttion Replica Epi Leather Eugenie Wallet Black M63882
Louis Vuttion Replica Epi Leather French Purse Purple M6364K
Louis Vuttion Replica Epi Leather Honfleur Clutch Yellow M52732
Louis Vuttion Replica Epi Leather Monceau Black M52122

tiffany jewelry Choose, buy

tiffany jewelry
Choose, buy and shop for on sale tiffany jewelry including Tiffany & Co Silver Necklace, Pendants, Bangles, Bracelets, Earrings, Rings and Accessories.
tiffany co
Tiffany Jewellery offering bangle Jewellery, bracelet jewelry, eardrop jewelry, necklace jewelry, ring jewelry, finger ring jewelry and earring jewelry
tiffany
tiffany and co
links of london
links london
Tiffany Style Silver Jewelry: Rings, Earrings, Necklaces, Bracelets and more Tiffany Jewellery at low prices.

always provide GUCCI Replica

always provide GUCCI Replica Handbags, and all kinds of replica GUCCI Handbags lower price for sale, the more fake GUCCI Handbags you choose, the cheaper price you get.
As everyone knows that the Chanel Handbags are popular in the whole world, sell replica Chanel Handbags can get more profit, many people like to sell chanel bags because fashion women love them.
Taking the Prada Handbags to join the big party can show women's fashion, so even the poor people like to buy replica Prada Handbags, so when you walk on the road, you never surprised by so much fake Prada Handbags, do you feel have a Prada replica Handbags is a cool action?
Fashion women love Miu Miu Handbgs, but the price is to high, so many ladies choose Miu Miu replica Handbgs, in the replica Miu Miu Handbgs shops, you will get the popular fake Miu Miu Handbgs that you love.

Research Paper

Hello,I love your blog! I

Hello,I love your blog! I think you've got a nice mix going actually.

great post! double bashers

great post! double bashers when added to power threads will make a pretty decent and wicked sick and exponential increase in efficiency. Thanks for the advice on how to utilize my resources well. I'll look into this more and research on how I can improve.

I have a method for

I have a method for automatically archiving the commands and automatically taring and moving it using log rotate. Its useful for finding an old command. Check it out.
http://blog.sriunplugged.com/shell-scripting/automatic-command-archive/

OMG! That's exactly what i

OMG! That's exactly what i was looking for...a detailed description od the process. I have already tried a few times but have always got confused. Hopefully it will work out now!!

And don't forget about magic

And don't forget about magic space!

More fun with bash

More fun with bash history

To execute the most recent command that contains the string "jpg":

!?jpg?

The second "?" means the string is not at the end of the line. If you omit both question marks, it means the string is at the beginning of the line.

In order to create an alias:

alias dothis='`history -p "!?jpg?"`'

(those are backquotes just inside the single quotes).

If instead of the first example, you type !?jpg and press M-^ it allows you to edit the command on the command line. M (meta) is usually the escape key pressed first and separately. You may also be able to press alt-shift-^ as a combined keystroke.

Control-s goes back forward through the entries found by control-R, but you may have to do stty -ixon beforehand to turn off the usual function of C-s.

Run the last thing in bash

Run the last thing in bash history that ended in "jpg", but substitute png for jpg.
!?jpg:s/jpg/png
or run the last command substituting png for jpg
^jpg^png^
Run the first line in bash_history in your default text editor
fc 1
or specify kwrite as the text editor
fc -e kwrite 1
(the editted command will run as soon as you save and close the text editor.
To make fc use kwrite as it's default editor, add this to your .bashrc
export EDITOR=kwrite fc

For my backup function, I

For my backup function, I use pass the %F-%R to my date command. This would allow me to make multiple backup copies of a file in one day and have them ordered by date/time.

Keith

Thankyou for ctrl R I have

Thankyou for ctrl R
I have been using command line for two years and one of
my biggest grips was this issue. I and now flying around the command line
thanks

Nice set of tricks. I knew

Nice set of tricks. I knew most of them already but it refreshed my memory. Thanks.

I find even more handy to have this in ~/.inputrc :
# -------- Bind page up/down wih history search ---------
"\e[5~": history-search-backward
"\e[6~": history-search-forward

I'll take the same example : on the bash prompt, type "gre" and Page up, this will give you "grep root /etc/passwd", the last command that started with "gre". Enter Page up again and it'll show you the previous one. Page down is obvioulsy used to show the next one.

I just noticed that the "set -o vi" trick is messing with this one ^_^ Can't tell you why.

Nice stuff... There are some

Nice stuff...

There are some GNU/Linux distributions that already use aliases "built-in" .
like rm which is "rm -i" in rhel5 .
So if you want to ignore the alias for known commands like rm for example, just type :

command rm

it will ignore the alias for the command.