The “history” command is being used for displaying a list of commands that have been run.
The history command
Display a list of commands that have been run
[root@test-aws-test ~]# history
5 ls -l
6 cd dir1
7 ls -l
8 touch test{1..10}
・
・
・
995 netstat
996 netstat -l
997 netstat -t
998 netstat -p
999 netstat -nltp
1000 history
Display five recent commands that have been run
[root@test-aws-test ~]# history 5
999 netstat -nltp
1000 history
1001 history 5
1002 history
1003 history 5
Execute a command by number in the history
[root@test-aws-test kadai2]# !855
df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 475M 0 475M 0% /dev
tmpfs 492M 0 492M 0% /dev/shm
tmpfs 492M 400K 492M 1% /run
tmpfs 492M 0 492M 0% /sys/fs/cgroup
/dev/xvda1 8.0G 2.8G 5.3G 35% /
tmpfs 99M 0 99M 0% /run/user/1000
When you want to execute the command of number 855 in the list, the history command allows you to run that command by using “!” + “number(855)”.
Display command history that includes “mkdir”
[root@test-aws-test ~]# history | grep mkdir
194 mkdir test
197 mkdir dir3
291 mkdir test1
298 mkdir txt.1 txt.2
385 mkdir test
820 mkdir test
836 mkdir sntax
842 mkdir syntax
965 mkdir test
1003 history | grep mkdir
It’s possible to display a list of a specified command from the history using the “grep”.
After “history”, add “| (pipe)” + “grep” + “ the command you want to display”.
A way of searching history other than using “history” command
Pressing “Ctrl” + “r” in the terminal opens the searching mode for command history!
For example, search commands that contain “w” from command history, the output looks like this.
(reverse-i-search)`w': pwd
Once you see a command you are looking for, you can press enter to run the command.
It displays from the most recent command from history that you can go back one by one by pressing “Ctrl” + “r” each time.
Summary
With just the “history” command, it displays a list of all the commands that have been run, but if you use “grep” to narrow down, it shows a list of only commands that you are looking for. That may help to save your time.
This blog post is translated from a blog post written by Inoue Ayaka on our Japanese website Beyond Co..
Comments