Leaderboard ad

3.21.2015

Basics of Using Bash History

Question sent in by Stacy from California.

Q: Is there a way to repeat long bash commands without hitting the up arrow a hundred times trying to find the command?

A: Bash history is a wonderfully efficient tool if you know how to use it.  You can use the history command to do everything from clearing your bash history, reissuing a long command, or searching history for a previously typed command.

To display a list of previously entered commands you will use the history command. Simply type history at the prompt like so:

[root@Centos7 ]# history
    1  cd /var/log
    2  tail -10 /var/log/messages
    3  history
 ... output truncated ...

You can show the last x number of commands typed by simply following the command with a number. For example if you wanted to see the last 20 commands:

history 20

Example output:

    7  vi names
    8  for i in `cat names`; do dig +short $i; done
    9  vi names
   10  for i in `cat names`; do dig +short $i; done
   11  for i in `cat names`; do dig +short $i >> ptr; done
   12  cat ptr
   13  for i in `cat names`; do echo -n "FORWARD: $i - REVERSE: "; dig +short $i; done
   14  cd ~
   15  cd scripts/
   16  ls -lrt
   17  vi update-clam.sh
   18  freshclam
   19  history -d 19
   20  cd /var/tmp
   21  ls -lrt
   22  unzip android-studio-ide-135.1740770-linux.zip
   23  cd android-studio/
   24  cd bin/
   25  ./studio.sh
   26  history 20

If you wanted to repeat the very last command, you can simply type !! at the prompt and bash will run the very last command you typed.  I find this command not as useful as the others because you can just hit the up arrow for the same effect.  But understanding the fact that !! is available, will lead to greater things.  For example, let's say you wanted to run a command you ran six commands ago.  You can use the just run the following command:

!-6

In my case this ran ls -lrt again, as you can see from my history output, that was six commands up from my last command.

You can also use the ! as a sort of search.  Using my example bash history, let's say I wanted to run the last for loop I issued.  You could use !-14, but  it is hard to remember how many commands it was ago, at least for me.  Instead we can tell bash to just run the last command starting with "for" like so:

!for

Example:
[root@Centos7 ]# !for
for i in `cat names`; do echo -n "FORWARD: $i - REVERSE: "; dig +short $i; done
FORWARD: putorius.net - REVERSE: 50.87.151.78
... output truncated ...

You can also specify by number, which command you want to run.  For example let's say I wanted to run command 18 on my example output above.  Using our friend the exclamation point, I can simply issue:

!18

Example output:
[root@Centos7 ]# !18
freshclam
ClamAV update process started at Sat Mar 21 08:23:52 2015
main.cvd is up to date (version: 55, sigs: 2424225, f-level: 60, builder: neo)
daily.cvd is up to date (version: 20218, sigs: 1354532, f-level: 63, builder: neo)
bytecode.cld is up to date (version: 247, sigs: 41, f-level: 63, builder: dgoddard)


You can also search bash history. At the bash prompt hold down Ctrl and hit r and start typing the first few letters of the command your searching for.  For example, let's say I was searching for a while loop I typed a little while back, I would hit [Ctrl+r] and type wh, like so:

[root@Centos7 ]#
(reverse-i-search)`wh': while read line; do echo $line | cut -f"\s*" -d 2; done < forward

This search will find the last command starting with "wh", to search higher or deeper into the history simply hit [Ctrl+r] again.

You can also search for a string within a command by issuing exclamation point and wrapping the string in question marks like so:

!?FORWARD?

Example:
[root@Centos7 ]# !?FORWARD?
for i in `cat names`; do echo -n "FORWARD: $i - REVERSE: "; dig +short $i; done

You can also use external tools like more, less or grep.
history | more
history | less
history | grep ssh

Using those tools are outside the scope of this tutorial, but feel free to drop my a line if you have specific questions.

Now that we covered viewing, using and searching bash history, let's talk about clearing or deleting specific items from bash history.

Your bash history is just a text file, usually saved at ~/.bash_history.  You can simply delete that file to clear your bash history, or you can more gracefully issue the history command with the clear option like so:

history -c

You can also delete a specific line from your bash history using the delete option like so:

history -d 12

There is a lot more you can do with history, but we just covered a lot of the basics.  For more information consult the history man pages.

No comments:

Post a Comment