Nov 182010
 

history

The history command is very useful when you frequently work at the terminal and you want to invoke a command given in the past.But there are several options that are not so common or poorly understood, in this article we’ll see some of them in bash and zsh.

Basic usage

Show last commands in history.

Apply to bash and zsh

history



Output:

  312  sudo -i
  315  mkdir joom
  316  cd joom
  317  unzip ~/Downloads/Joomla_1.5.21_to_1.5.22-Stable-Patch_Package_ita.zip
  318  cd /tmp
  319  ls
  320  ls -alrt
  321  mkdir image
  322  cd image
  323  unzip ../ed ambientale.zip.html
  324  cd ed ambientale

Task 1 Display Date And Time in history commands

An useful and easy thing is that to add to every single command you give the date and time they were given, this can be useful to understand when something is happened in our system.

Bash Add to your ~/.bash_profile

export HISTTIMEFORMAT='%Y-%m-%d %H:%M:%S '

Zsh Add to your ~/.zshrc

HISTFILE=~/.zsh_history

HISTSIZE=9999

SAVEHIST=9999

setopt extendedhistory

Task 2 Search for a string in your history

You know a part of the command and you want to search in your history.

Apply to bash and zsh

In the terminal, hold down Ctrl and press R to invoke “reverse-i-search. (bash)” Type a letter – like s – and you’ll get a match for the most recent command in your history that starts with s. Keep typing to narrow your match. When you reach the desired command, press Enter to execute the suggested command.

Task 3 execute the last command given as root

You gave a long command, and after that you have realized that it was necessary to be root

Apply to bash and zsh

sudo !!

You will be asked for your password, then the shell will run the last command, this is because !! is interpreted as the last command given.

Task 4 Check command history for “string” but avoid running it

Print the first occurrence of string found in the history, but don’t run it.

!string:p

!string will search your command history and execute the first command that matches ‘whatever’. If you don’t feel safe doing this put :p on the end to print without executing. Recommended when running as superuser.

Task 5 delete a line of the history

Useful when you do an error and type a password as normal command.

Apply to bash

history -d < history number >

Delete the entry with the < history number > from the command line history

Task 6 List of commands you use most often

useful in deciding what new alias to do or for the statistics.

Apply to bash and zsh

history | awk '{print $2}' | sort | uniq -c | sort -rn | head

Output:

    53 aptitude
     47 ip
     40 ls
     33 apt-get
     26 cd
     25 xrandr
     24 man
     17 history
     15 rmmod
     15 apt-cache

Check Also the Bash history cheatsheet

Popular Posts:

Flattr this!

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

*