Search code examples
bashcommand-linecommandback

Shell command for getting history of a specific command


I am looking for a command that shows what type of parameters are used earlier for the command I am using currently.

For example, if I want to use the command say tail, can I see the list of commands I have used recently which are of type tail. I know that command history gives recently used commands but I am looking for history of specific command. I am using bash shell.

Also if that is possible then can I restrict the result to see only 2 outputs that means recently used 2 tail commands?


Solution

  • I would still use history. Just grep the command you like from the output of history:

    history | grep tail
    

    and only the two last ones:

    history | grep tail | tail -3 | head -2
    

    The tail -3 gives the last 3 in the list, but this includes the command you just typed in. So we then get the first two of the three to exclude the history line.