This article is part of the article series "Bash One-Liners Explained."
<- previous article next article ->

This is the fourth part of the Bash One-Liners Explained article series. In this part I'll teach you how to work with bash history. I'll use only the best bash practices, various bash idioms and tricks. I want to illustrate how to get various tasks done with just bash built-in commands and bash programming language constructs.

I'll break this part into several sub-parts as it's very tiring to write long articles, and I'd rather publish many short articles and make quick progress.

See the first part of the series for introduction. After I'm done with the series I'll release an ebook (similar to my ebooks on awk, sed, and perl), and also bash1line.txt (similar to my perl1line.txt).

Also see my other articles about working fast in bash from 2007 and 2008:

Parts of this post are based on my earlier post Definitive Guide to Bash Command Line History. Check it out, too!

Let's start.

Part IV: Working with history

1. Erase all shell history

$ rm ~/.bash_history

Bash keeps the shell history in a hidden file called .bash_history. This file is located in your home directory. To get rid of the history, just delete it.

Note that if you logout after erasing the shell history, this last rm ~/.bash_history command will be logged. If you want to hide that you erased shell history, see the next one-liner.

2. Stop logging history for this session

$ unset HISTFILE

The HISTFILE special bash variable points to the file where the shell history should be saved. If you unset it, bash won't save the history.

Alternatively you can point it to /dev/null,

$ HISTFILE=/dev/null

3. Don't log the current command to history

Just start the command with an extra space:

$  command

If the command starts with an extra space, it's not logged to history.

Note that this only works if the HISTIGNORE variable is properly configured. This variable contains : separated values of command prefixes that shouldn't be logged.

For example to ignore spaces set it to this:

HISTIGNORE="[ ]*"

My HISTIGNORE looks like this:

HISTIGNORE="&:[ ]*"

The ampersand has a special meaning - don't log repeated commands.

4. Change the file where bash logs command history

$ HISTFILE=~/docs/shell_history.txt

Here we simply change the HISTFILE special bash variable and point it to ~/docs/shell_history.txt. From now on bash will save the command history in that file.

5. Add timestamps to history log

$ HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S"

If you set the HISTTIMEFORMAT special bash variable to a valid date format (see man 3 strftime) then bash will log the timestamps to the history log. It will also display them when you call the history command (see the next one-liner).

6. Show the history

$ history

The history command displays the history list with line numbers. If HISTTIMEFORMAT is set, it also displays the timestamps.

7. Show the last 50 commands from the history

$ history 50

If you specify a numeric argument, such as 50, to history, it prints the last 50 commands from the history.

7. Show the top 10 most used commands from the bash history

$ history |
    sed 's/^ \+//;s/  / /' |
    cut -d' ' -f2- |
    awk '{ count[$0]++ } END { for (i in count) print count[i], i }' |
    sort -rn |
    head -10

This one-liner combines bash with sed, cut, awk, sort and head. The perfect combination. Let's walk through this to understand what happens. Let's say the output of history is:

$ history
    1  rm .bash_history 
    2  dmesg
    3  su -
    4  man cryptsetup
    5  dmesg

First we use the sed command to remove the leading spaces and convert the double space after the history command number to a single space:

$ history | sed 's/^ \+//;s/  / /'
1 rm .bash_history 
2 dmesg
3 su -
4 man cryptsetup
5 dmesg

Next we use cut to remove the first column (the history numbers):

$ history |
    sed 's/^ \+//;s/  / /' |
    cut -d' ' -f2-

rm .bash_history 
dmesg
su -
man cryptsetup
dmesg

Next we use awk to record how many times each command has been seen:

$ history |
    sed 's/^ \+//;s/  / /' |
    cut -d' ' -f2- |
    awk '{ count[$0]++ } END { for (i in count) print count[i], i }'

1 rm .bash_history 
2 dmesg
1 su -
1 man cryptsetup

Then we sort the output numerically and reverse it:

$ history |
    sed 's/^ \+//;s/  / /' |
    cut -d' ' -f2- |
    awk '{ count[$0]++ } END { for (i in count) print count[i], i }' |
    sort -rn

2 dmesg
1 rm .bash_history 
1 su -
1 man cryptsetup

Finally we take the first 10 lines that correspond to 10 most frequently used commands:

$ history |
    sed 's/^ \+//;s/  / /' |
    cut -d' ' -f2- |
    awk '{ count[$0]++ } END { for (i in count) print count[i], i }' |
    sort -rn |
    head -10

Here is what my 10 most frequently used commands look like:

2172 ls
1610 gs
252 cd ..
215 gp
213 ls -las
197 cd projects
155 gpu
151 cd
119 gl
119 cd tests/

Here I've gs that's an alias for git status, gp is git push, gpu is git pull and gl is git log.

8. Execute the previous command quickly

$ !!

That's right. Type two bangs. The first bang starts history substitution, and the second one refers to the last command. Here is an example:

$ echo foo
foo
$ !!
foo

Here the echo foo command was repeated.

It's especially useful if you wanted to execute a command through sudo but forgot. Then all you've to do is run:

$ rm /var/log/something
rm: cannot remove `/var/log/something': Permission denied
$
$ sudo !!   # executes `sudo rm /var/log/something`

9. Execute the most recent command starting with the given string

$ !foo

The first bang starts history substitution, and the second one refers to the most recent command starting with foo.

For example,

$ echo foo
foo
$ ls /
/bin /boot /home /dev /proc /root /tmp
$ awk -F: '{print $2}' /etc/passwd
...
$ !ls
/bin /boot /home /dev /proc /root /tmp

Here we executed commands echo, ls, awk, and then used !ls to refer to the ls / command.

10. Open the previous command you executed in a text editor

$ fc

Fc opens the previous command in a text editor. It's useful if you've a longer, more complex command and want to edit it.

For example, let's say you've written a one-liner that has an error, such as:

$ for wav in wav/*; do mp3=$(sed 's/\.wav/\.mp3/' <<< "$wav"); ffmpeg -i "$wav" "$m3p"; done

And you can't see what's going on because you've to scroll around, then you can simply type fc to load it in your text editor, and then quickly find that you mistyped mp3 at the end.

Enjoy!

Enjoy the article and let me know in the comments what you think about it. In the next sub-part I'll continue discussing history keyboard shortcuts, history shell options and more.

This article is part of the article series "Bash One-Liners Explained."
<- previous article next article ->

Comments

Gaurav Permalink
October 13, 2012, 05:40

hi,
all the commands are nice and useful specially to find top 10 most used commands :)

but the third one is not working in my system even i am firing commands with space but still it is showing in history

October 13, 2012, 15:36

I forgot that what gets ignored is configured through the HISTIGNORE variable. It contains a : separated values of command prefixes that should be ignored.

It looks like your distribution doesn't have a default value for this variable.

It should contain this:

export HISTIGNORE="[ ]*"

Meaning - don't log anything to history that starts with a space.

I've this in my .bashrc:

export HISTIGNORE="&:[ ]*"

Here & has a special meaning - ignore repeated commands.

Gaurav Permalink
October 15, 2012, 11:06

hmm. it was hashed in my .bashrc now i have corrected it and it is working :)

October 13, 2012, 05:58

1. It looks weird, that you are taking the second column of history output with cut, instead of doing that inside of awk block. (in your "top 10" example)

2. I think that awk is an overkill for the common task of counting the number of entries. The same can be done a la "| sort | uniq -c | sort -nr". I think that this code snippet is much easier to remember than the line of awk code, even though it performs worse.

October 13, 2012, 15:38

1. I love when each command does just one thing, and I love to pipe things together.

I could have written it as a Perl one-liner:

history | perl -lne 's/\d+//;s/^\s+//;s/\s\s/ /; $count{$_}++; END { print "$count{$_} $_" for (sort { $count{$a} <= $count{$b} } keys %count )[0..10] }'

2. Good example of combining commands together.

lava Permalink
November 04, 2012, 20:14

Also, it would be an overkill to use
history | sort | uniq -c | sort -rn

because, as you stated in the article, history is printing .bash_history to the screen. For most users it would be better to run:
$ sort .bash_history | uniq -c | sort -rn

because this will exclude one pipe.

October 13, 2012, 11:11

3. does not work here either (Debian Squeeze).

10. can be done easier by using regular expressions after the bang, in case of your mistyped "for wav in..." you could type:

!f:s/m3p/mp3

(other example here http://linux-training.be/files/books/html/fun/ch13s09.html )

cheers,
paul

October 13, 2012, 15:48

3. I forgot that what gets ignored is configured through the HISTIGNORE variable. It contains a : separated values of command prefixes that should be ignored.

It looks like your distribution doesn't have a default value for this variable.

It should contain this:

export HISTIGNORE="[ ]*"

10. Good one. Alternatively you could've written ^m3p^mp3. I'll cover these in the next part.

chmurli Permalink
October 14, 2012, 10:02

By default commands started with space will be recorded to history.
In order to change it you have to configure it by:
export HISTCONTROL="ignorespace"

October 15, 2012, 18:38

Nice tip.

You can also do the same by doing:

export HISTIGNORE="[ ]*"
October 14, 2012, 13:03

I use CTRL+R a lot to quicksearch & reuse my history, might be a nice addition to your post.

October 15, 2012, 18:38

Definitely. I'll talk about it in the next post.

I actually wrote about these keyboard shortcuts in my Definitive Guide to Bash History a long time ago.

AltairIV Permalink
January 14, 2013, 12:54

I personally prefer using HISTCONTROL=erasedups. It's the inverse of ignoredups, in that it removes all previous instances of the command from your history, so that only the most recent one is kept.

djj Permalink
January 24, 2013, 21:42

Checkout Bash "inputrc", which controls the behavior of readline. You can do some really cool things (capture command in a visual editor, modify it, return it to the command line for execution). As I was reading the above, I kept echoing one variable at a time. I used parameter expansion:
for p in "${!HIST@}"; do echo $p=${!p}; done

djj Permalink
January 25, 2013, 01:01

I thought I'd replace sed/awk with bash using associative arrays and the redirection in your earlier one-liner. My history is prefixed with the date, time, as well as the number, thus the 3 _'s.

declare -A aA; while read -r _ _ _ cmd; do ((aA["${cmd}"]++)); done < <(history)

for cmd in "${!aA[@]}"; do if (( ${aA[$cmd]} > 1 )); then printf -- '%3d %-.50s\n' ${aA[$cmd]} "${cmd}"; fi; done | sort -rn

msccreater Permalink
October 24, 2013, 15:14

i still can't do the command succeed about export HISTIGNORE="[ ]*", here is my command history
616 export HISTIGNORE="[ ]*"
617 export HISTIGNORE="[ ]*"
618 export HISTIGNORE="[ ]*"
619 ls
620 ls
621 history

December 02, 2013, 05:45

nice development work.

April 16, 2014, 15:16

What about this?:

history | awk '{ count[$($1="")]++ } END { for (i in count) print count[i], i }' | sort -nr | head

There's a problem for instructions that take more than one line due to here documents, multi-linear strings and the like.

April 16, 2014, 17:07

Or
history | sed 's/ *[^ ]* *//' | awk '{count[$0]++} END { print count[i],i }' | sort -nr | head

Which is more or less the same as your one-liner but with one iteration less over the whole history. It also respects the spacings inside the commands, thing that my previous command does not.

June 23, 2014, 11:02

Ok maybe that is true

December 09, 2014, 09:58

nice
a href=" http://mynycstreets.com/2014/12/download-lingaa-movie-torrentlingaa-movie-free-download/">Lingaa Movie Torrent Downl

<oad>

lingaa movie in mp4lingaa movie in hdlingaa movie in free downloadLingaa Movie download in torrentfree download lingaa movie
diggnice article

Diana Permalink
December 17, 2014, 16:23

This article is quite helpful and informative too. I enjoyed a lot. Thanks for sharing such a great article.

Beautiful Christmas Quotes for your friends and family...
christmas quotes
Christmas Messages for Whatsapp

Best Christmas Greetings for your friends and family...
christmas greeting
christmas wishes

What to Write in Christmas Card....... Check out best Christmas Greeting Card Words
What to Write in Christmas Card

Get Beautiful and Unique Christmas Wallpapers for free
free christmas wallpaper
christmas tree decorating ideas

Thanks for sharing such a great article.

Leave a new comment

(why do I need your e-mail?)

(Your twitter name, if you have one. (I'm @pkrumins, btw.))

Type the word "security_334": (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.

Advertisements