When you are using Linux/Unix command line frequently, using the history effectively can be a major productivity boost. You can accomplish things with less typing. For example, you will be typing lots of commands like
|
1 2 3 4 5 |
ls -ltr cd /tmp/user pwd cd /tmp/usr |
and so on. There are 6 ways to repeat your previous commands quickly without having to type a lot.
- Use the up arrow to view the previous command and press enter to execute it.
- Type !! and press enter from the command line to execute the last command again
- Type !-1 and press enter from the command line to execute the last command !-2 for command before the last and so on.
- Type history followed by enter, which prints a list of last few commands, and then !25 to execute a particular command
- Press Control+P will display the previous command, press enter to execute it.
- Press Control + R, and then search for the command or arguments typed. This the most useful one.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
$history | more 71 ls -ltr 72 ls -ltr 73 ls -ltr 74 ls -ltr 75 pwd 76 pwd 77 I-2 78 pwd 79 ls -ltr 80 pwd 81 pwd 82 pwd 83 ls -ltr 84 hsitory | more 85 history | more |
To execute the command pwd again, you can do
|
1 2 |
!71 |
Now, what if you want to execute a previous command that starts with certain letters
|
1 2 |
!p |
to execute the pwd command again. The most useful command of all is
Control+P type “<search text>”
|
1 2 3 4 5 6 7 8 9 |
$ cd test15/myAppProperties/ $ ls -ltr $ cd /opt/tmp $ CONTROL+Rjb and type <test15> $(reverse-i-search)`jbTest15': cd test15/myAppProperties/ </test15> |
This a contextual search of your command history. As you type the search text, the display changes. The most powerful, and useful.
By default, history is stored in ~/.bash_history and ~/.sh_history files. You can list the hidden files with
|
1 2 3 4 |
$ cd ~ $ ls -a |
~ is the home directory.
You can clear all the previous history with
|
1 2 3 |
$ history -c |
You can do a lot more, but for Java developers, this is a good start.