Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

History Command in Linux (Bash History) - DesignLinux

Sep 25 2020
designlinux 0 Comments
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.

Related

Tags: history, terminal

10 Linux Distributions and Their Targeted Users

Prev Post

7 Amazing Linux Distributions For Kids

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 0
  • 65
  • 605,815

DesignLinux.com © All rights reserved

Go to mobile version