You are here

Type Less Using Bash Command History

Many of us prefer or have to use command line in Linux for web development or server setup/configuration/maintenance. By making use of the command history, we can be more efficient with various tasks we need to perform.

1) To see a list of commands in the command history

Simply type history or history 10 to see all or the last 10 commands in history. This will also show the command # for each command, which may be useful in repeating a particular command (see #3).

2) To repeat the last executed command

Just type !!

3) To repeat a particular command in history

Type !5 to execute command #5 in the history, or !-5 to repeat the 5th command from the end of command history.

4) To repeat a previous command that starts with a particular word

Type !ls to execute the most recent command that starts with ls.

5) To substitute characters in a previous command

Type !ls:s/dir1/dir2/ to repeat the most recent command starting with ls and replace dir1 with dir2 in that command; !!:gs/a/b/ to repeat the last executed command and replace all a's with b's; ^-l -h^-lh^ to repeat the last executed command and replace -l -h with -lh.

6) To reuse arguments from a previous command

Type vim !:1 to edit a file and the filename comes from the 1st argument of the last executed command (use !$ for the last argument of the last executed command), or vim !ls:2 to edit a file whose name comes from the 2nd argument of the most recent command that starts with ls (use !ls:$ for the last argument of the most recent command that starts with ls).

7) To search in command history

Press Ctrl+R, and type the keyword to look for a command. Or just use the arrow keys to navigate the command history.

A Bash reference manual can be found at http://www.gnu.org/software/bash/manual/bashref.html for more details about Bash.

SHARE SOCIAL
Appnovation Technologies