• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

History Command in Linux (Bash History)

Written by Admin, Updated On September 24, 2020
history, terminal
History Command in Linux (Bash History)

When you are working with terminal for long time, you need some of commands repeatedly. It would be easier if you can view the history of previously used command on your command line. In this article, we will discuss about history command, using that you can view the previously executed commands.

Use the history Command#

The history command is built-in command and it might work differently from shell to shell. In this article we will discuss about Bash built-in version of history command.

If you run the history command without any options, it will show the whole history list with line numbers.

history
...
788 uptime
789 lscpu
790 exit
791 history

To execute the command from the list, use !n command where n is the line number of the list. From above output, to execute command on line number 788, you would type:

!788

The output of history command does not fit to screen so you can make page wise output using less command:

history | less

Also, you can show last n number of line by passing number as an argument with history command. For instance, to view last 10 lines from history, type:

history -10

You should use the up and down arrow key to navigate to the lines. Press Enter key when you are on specific line to execute that command.

To execute the previously used command, type:

!!

It’s very useful when you need to run commands with sudo privileges. You can just use !! instead of typing whole command:

sudo !!

Command !-1 is equivalent to !! and if you want to run the second last you can run !-2, and so on.

To filter the output, use grep command. For example, you need to find commands which are run using sudo word you would type:

767  sudo apt update
768  sudo apt upgrade
769  sudo reboot
796  history | grep sudo

Now, again you can run the sudo apt update command, just type:

!767

Save History Command List#

Bash reads the history list from the .bash_history file by default. In the current session the list of executed commands are kept in memory and once the session closed it saved to the file.

You can save the current session history list to the .bash_history file using -a option with history command.

To write the complete history list to the history file, use the -w option.

history -w

Clearing History#

You also can clear the history command list or specific part using the history command. Use -c option to clear entire history list:

history -c

To delete a specific line or lines between a start and end positions from the history list, use the -d option.

For example, to remove the lines between 735 and 745, you would type:

history -d 735 745

It will includes the both line numbers 735 and 745. You can pass single line number to delete a specific line.

The above command will clear the list only from the memory not from the .bash_history file. To clear history from the file you should write the history list to the file:

history -c
history -w

Conclusion#

In this article explained how to use history command to get list of previously executed commands. To get more information about History Expansion, check the Bash manual.

If you have any questions or feedback, please leave a comment below.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article SSH Command
Next Article   Fsck Command in Linux (Repair File System)

Related Posts

  • How to Install SSH Keys on Ubuntu 22.04

    How to Set up SSH Keys on Ubuntu 22.04

    January 7, 2023
  • How to Install Fail2ban on Ubuntu 22.04

    How to Install and Configure Fail2ban on Ubuntu 22.04

    December 5, 2022
  • How to Enable SSH on Ubuntu 22.04

    How to Enable SSH on Ubuntu 22.04

    December 1, 2022

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install Microsoft Edge Browser on Ubuntu 22.04
    How to Install Microsoft Edge Browser on Ubuntu 22.04 March 14, 2023
  • How to Install Ruby on Ubuntu 22.04 LTS
    How to Install Ruby on Ubuntu 22.04 LTS February 27, 2023
  • How to Install LEMP Stack on Ubuntu 22.04
    How to Install LEMP Stack on Ubuntu 22.04 March 18, 2023
  • How to Install Set Up Apache Virtual Hosts on Ubuntu 22.04
    How to Set Up Apache Virtual Hosts on Ubuntu 22.04 March 2, 2023
  • How to Install MariaDB on Debian 11 Bullseye
    How to Install MariaDB on Debian 11 Bullseye March 8, 2023
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook