Command-line history in Linux

In Linux shell, you can use the up and down keys on your keyboard to scroll through previously used commands. Once you find the command you want, you can edit it – move the cursor with the left and right keys and make the changes you want to it. When done press the Enter key to execute the command.

To see a list of the last commands, use the history command.
– use the history without any options to see ALL the previous commands (this can be a very long list)
– use the history -n  to see the last n commands (the last command in the list will be the history command itself)

root@web [~]# history 10
  944  2017-05-03 14:57:22 mysqlcheck llp
  945  2017-05-03 14:57:31 mysqlcheck roundcube
  946  2017-05-03 14:57:41 mysqlcheck -o roundcube
  947  2017-05-03 16:20:11 exit
  948  2017-05-12 11:12:13 history 3
  949  2017-05-12 11:12:31 history 5
  950  2017-05-12 11:12:45 w
  951  2017-05-12 11:12:49 free -m
  952  2017-05-12 11:12:51 uptime
  953  2017-05-12 11:12:56 history 10
root@web [~]#

– notice that you can also see how many commands are in history. Look at the number in front of the commands. In the above example, we have 953 commands. – to see and execute the last executed command use !!

root@web [~]# date
Fri May 12 11:23:59 EDT 2017
root@web [~]# !!
date
Fri May 12 11:24:02 EDT 2017
root@web [~]#

Another tip is to use the CTRL+R to search a previously executed command. Just press CTRL+R, and you will see

(reverse-i-search)`d': date

Here type part of the command you are looking for. When you find it, press Enter to execute it.

CentOS is the most used Linux distribution in the web hosting world. It uses Red Hat Enterprise Linux (RHEL) as its upstream source. More info at CentOS page on Wikipedia

To connect to the Linux shell you can use a software like PuTTY.

Leave a Reply