Linux Print History Command Without Line Numbers

Linux history command is used to get previously used commands by the current user. The default size for the history command is 1000 which means that last 1000 command will be stored in history.

While listing history of bash line numbers are provided too. This is not expected in some situations. To get history without line numbers following commands can be used.

Printing .bash_history File

Executed bash commands are stored in a file named .bash_history.  This file is located in every user’s home directory. For example for user ismail is will be located at /home/ismail/.bash_history. The simplest and easiest way is printing .bash_history file. History file stored commands in a plain format.

Printing .bash_history File

Printing .bash_history File

Using Awk

awk is a very useful tool used to read strings and filter them according to the given expression. With awk following command can be used to filter with substring. We will remove the command history line numbers with an expression which will get only commands from history command output.

Using Awk

Using Awk

Using Sed

sed is an alternative to the awk.  We will use a similar tactic where we will remove line numbers. We can use sed streamline editor to filter line numbers we will remove all numbers from the start of each line up to space in this example.

  • /s used to search
  • ^ used to start of the line
  • [ ]* used to spaces from start
  • [0-9]\+ used for numbers
  • // is used to remove previously given regex which is numbers and spaces from the start of the line.

Using Sed

Using Sed

Using Cut

The cut is the simplest example of removing history line numbers. We will delimit output according to space and print columns start with 2 and others.

  • -d is used to specify the delimiter which is space.
  • -f is used to column number which is from 4 to the end

Using Cut

Using Cut

Linux Print History Command Without Line Numbers Infographic

Linux Print History Command Without Line Numbers Infographic

Linux Print History Command Without Line Numbers Infographic

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Enjoy this blog? Please spread the word :)