Linux Print History Command Without Line Numbers – POFTUT

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.

Print History File with Numbers

By default history command is used to print Linux bash history where the commands with the index numbers are printed to the screen. By deafult, only the last 1000 commands are printed.

$ history
Print History File with Numbers
Print History File with Numbers

We can see that before the command the number of the file is printed to the screen.

Printing .bash_history File Without Line Numbers

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.

$ cat ~/.bash_history
Printing .bash_history File
Printing .bash_history File

From the output, we can see that only commands are printed from the .bash_history.

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.

$ history | awk '{$1="";print substr($0,2)}'
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.
$ history | sed 's/^[ ]*[0-9]\+[ ]*//'
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
$ history | cut -d ' ' -f 4-
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

 

LEARN MORE  Python Glob() Function To Match Path, Directory, File Names with Examples

Leave a Comment