15 Examples To Master Linux Command Line History

When you are using Linux command line frequently, using the history effectively can be a major productivity boost. In fact, once you have mastered the 15 examples that I’ve provided here, you’ll find using command line more enjoyable and fun.

1. Display timestamp using HISTTIMEFORMAT

Typically when you type history from command line, it displays the command# and the command. For auditing purpose, it may be beneficial to display the timepstamp along with the command as shown below.

# export HISTTIMEFORMAT='%F %T '
# history | more
1  2008-08-05 19:02:39 service network restart
2  2008-08-05 19:02:39 exit
3  2008-08-05 19:02:39 id
4  2008-08-05 19:02:39 cat /etc/redhat-release

2. Search the history using Control+R

I strongly believe, this may be your most frequently used feature of history. When you’ve already executed a very long command, you can simply search history using a keyword and re-execute the same command without having to type it fully. Press Control+R and type the keyword. In the following example, I searched for red, which displayed the previous command “cat /etc/redhat-release” in the history that contained the word red.

# [Press Ctrl+R from the command prompt,
which will display the reverse-i-search prompt]
(reverse-i-search)`red': cat /etc/redhat-release
[Note: Press enter when you see your command,
which will execute the command from the history]
# cat /etc/redhat-release
Fedora release 9 (Sulphur)

Sometimes you want to edit a command from history before executing it. For e.g. you can search for httpd, which will display service httpd stop from the command history, select this command and change the stop to startand re-execute it again as shown below.

# [Press Ctrl+R from the command prompt,
which will display the reverse-i-search prompt]
(reverse-i-search)`httpd': service httpd stop
[Note: Press either left arrow or right arrow key when you see your
command, which will display the command for you to edit, before executing it]
# service httpd start

3. Repeat previous command quickly using 4 different methods

Sometime you may end up repeating the previous commands for various reasons. Following are the 4 different ways to repeat the last executed command.

  1. Use the up arrow to view the previous command and press enter to execute it.
  2. Type !! and press enter from the command line
  3. Type !-1 and press enter from the command line.
  4. Press Control+P will display the previous command, press enter to execute it

4. Execute a specific command from history

In the following example, If you want to repeat the command #4, you can do !4 as shown below.

# history | more
1  service network restart
2  exit
3  id
4  cat /etc/redhat-release

# !4
cat /etc/redhat-release
Fedora release 9 (Sulphur)

5. Execute previous command that starts with a specific word

Type ! followed by the starting few letters of the command that you would like to re-execute. In the following example, typing !ps and enter, executed the previous command starting with ps, which is ‘ps aux | grep yp’.

# !ps
ps aux | grep yp
root     16947  0.0  0.1  36516  1264 ?        Sl   13:10   0:00 ypbind
root     17503  0.0  0.0   4124   740 pts/0    S+   19:19   0:00 grep yp

6. Control the total number of lines in the history using HISTSIZE

Append the following two lines to the .bash_profile and relogin to the bash shell again to see the change. In this example, only 450 command will be stored in the bash history.

# vi ~/.bash_profile
HISTSIZE=450
HISTFILESIZE=450

7. Change the history file name using HISTFILE

By default, history is stored in ~/.bash_history file. Add the following line to the .bash_profile and relogin to the bash shell, to store the history command in .commandline_warrior file instead of .bash_history file. I’m yet to figure out a practical use for this. I can see this getting used when you want to track commands executed from different terminals using different history file name.

# vi ~/.bash_profile
HISTFILE=/root/.commandline_warrior

If you have a good reason to change the name of the history file, please share it with me, as I’m interested in finding out how you are using this feature.

8. Eliminate the continuous repeated entry from history using HISTCONTROL

In the following example pwd was typed three times, when you do history, you can see all the 3 continuous occurrences of it. To eliminate duplicates, set HISTCONTROL to ignoredups as shown below.

# pwd
# pwd
# pwd
# history | tail -4
44  pwd
45  pwd
46  pwd [Note that there are three pwd commands in history, after
executing pwd 3 times as shown above]
47  history | tail -4

# export HISTCONTROL=ignoredups
# pwd
# pwd
# pwd
# history | tail -3
56  export HISTCONTROL=ignoredups
57  pwd [Note that there is only one pwd command in the history, even after
executing pwd 3 times as shown above]
58  history | tail -4

9. Erase duplicates across the whole history using HISTCONTROL

The ignoredups shown above removes duplicates only if they are consecutive commands. To eliminate duplicates across the whole history, set the HISTCONTROL to erasedups as shown below.

# export HISTCONTROL=erasedups
# pwd
# service httpd stop
# history | tail -3
38  pwd
39  service httpd stop
40  history | tail -3

# ls -ltr
# service httpd stop
# history | tail -6
35  export HISTCONTROL=erasedups
36  pwd
37  history | tail -3
38  ls -ltr
39  service httpd stop
[Note that the previous service httpd stop after pwd got erased]
40  history | tail -6

10. Force history not to remember a particular command using HISTCONTROL

When you execute a command, you can instruct history to ignore the command by setting HISTCONTROL to ignorespace AND typing a space in front of the command as shown below. I can see lot of junior sysadmins getting excited about this, as they can hide a command from the history. It is good to understand how ignorespace works. But, as a best practice, don’t hide purposefully anything from history.

# export HISTCONTROL=ignorespace
# ls -ltr
# pwd
#  service httpd stop [Note that there is a space at the beginning of service,
to ignore this command from history]
# history | tail -3
67  ls -ltr
68  pwd
69  history | tail -3

11. Clear all the previous history using option -c

Sometime you may want to clear all the previous history, but want to keep the history moving forward.

# history -c

12. Subtitute words from history commands

When you are searching through history, you may want to execute a different command but use the same parameter from the command that you’ve just searched.

In the example below, the !!:$ next to the vi command gets the argument from the previous command to the current command.

# ls anaconda-ks.cfg
anaconda-ks.cfg
# vi !!:$
vi anaconda-ks.cfg

In the example below, the !^ next to the vi command gets the first argument from the previous command (i.e cp command) to the current command (i.e vi command).

# cp anaconda-ks.cfg anaconda-ks.cfg.bak
anaconda-ks.cfg
# vi  !^
vi anaconda-ks.cfg

13. Substitute a specific argument for a specific command.

In the example below, !cp:2 searches for the previous command in history that starts with cp and takes the second argument of cp and substitutes it for the ls -l command as shown below.

# cp ~/longname.txt /really/a/very/long/path/long-filename.txt
# ls -l !cp:2
ls -l /really/a/very/long/path/long-filename.txt

In the example below, !cp:$ searches for the previous command in history that starts with cp and takes the last argument (in this case, which is also the second argument as shown above) of cp and substitutes it for the ls -l command as shown below.

# ls -l !cp:$
ls -l /really/a/very/long/path/long-filename.txt

14. Disable the usage of history using HISTSIZE

If you want to disable history all together and don’t want bash shell to remember the commands you’ve typed, set the HISTSIZE to 0 as shown below.

# export HISTSIZE=0
# history
# [Note that history did not display anything]

15. Ignore specific commands from the history using HISTIGNORE

Sometimes you may not want to clutter your history with basic commands such as pwd and ls. Use HISTIGNORE to specify all the commands that you want to ignore from the history. Please note that adding ls to the HISTIGNORE ignores only ls and not ls -l. So, you have to provide the exact command that you would like to ignore from the history.

# export HISTIGNORE="pwd:ls:ls -ltr:"
# pwd
# ls
# ls -ltr
# service httpd stop

# history | tail -3
79  export HISTIGNORE="pwd:ls:ls -ltr:"
80  service httpd stop
81  history
[Note that history did not record pwd, ls and ls -ltr]

Recommended Reading

Bash 101 Hacks, by Ramesh Natarajan. I spend most of my time on Linux environment. So, naturally I’m a huge fan of Bash command line and shell scripting. 15 years back, when I was working on different flavors of *nix, I used to write lot of code on C shell and Korn shell. Later years, when I started working on Linux as system administrator, I pretty much automated every possible task using Bash shell scripting. Based on my Bash experience, I’ve written Bash 101 Hacks eBook that contains 101 practical examples on both Bash command line and shell scripting. If you’ve been thinking about mastering Bash, do yourself a favor and read this book, which will help you take control of your Bash command line and shell scripting.

Awesome Linux Articles

Following are few awesome 15 examples articles that you might find helpful.

时间: 2024-09-23 07:31:44

15 Examples To Master Linux Command Line History的相关文章

manage RAID on Linux by hpacucli HP Command Line Array Configuration Utility

通过hp提供的工具hpacucli可以在CentOS 6.x x64中管理HP 服务器的RAID卡. # which hpacucli /usr/sbin/hpacucli # rpm -qf /usr/sbin/hpacucli hpacucli-8.75-12.0.i386 例如我们在服务器上新插入了两块硬盘, 还没有配置RAID. 首先要列出RAID卡控制器的信息 : # hpacucli ctrl all show detail config Smart Array P410i in S

Linux常用命令大全 Linux Commands Line - v1.0

The most complete and updated list of commands on linux by LinuxGuide.it - over 350 commands!          COMMAND DESCRIPTION    System information arch show architecture of machine uname -r show used kernel version dmidecode -q show hardware system com

如何判断应用程序运行于GUI模式还是Command Line

越来越多的应用程序提供以命令行的方式来运行,通常的做法有两种:单独给应用程序写一个基于命令行运行的控制台程序,用户运行这个程序时它一定是以命令行的方式来运行:以GUI和Command Line共享一个应用或exe文件,但通过不同的arguments来判断,最终分别做不同的处理. 对于单独给应用程序写基于命令行运行的控制台程序,无非是通过判断传递的args数组来辨别并设置程序运行所需要的参数,最终设定各项参数而完成所需要的工作.在这里建议提供对于/?的帮助菜单,方便用户查询. if (args.L

Win7安装软件提示“Command Line Option Syntax Error”怎么办?

  不管是使用过很久的电脑还是新买的电脑,都是会出现一些问题的,最常见的要数安装软件问题了,有用户反映说每次安装软件都会提示"Command Line Option Syntax Error"错误,那么当Win7系统安装软件提示"Command Line Option Syntax Error"该怎么办呢?不懂的朋友看看以下文章吧! 问题一:怎么解决在安装软件时出现的Microsoft Visual C++ 2005 Redistributable Command

如何解决MindManager15安装错误Command line option syntax error

  MindManager15是Mindjet公司研发出来的最高版本,MindManager思维导图在中国拥有众多的用户群.新电脑或者系统重装之后会提示各种各样的安装错误,其实这和MindManager15没有直接关系,是电脑系统的问题,现在教您解决Microsoft Visual C++ 2005 Redistributable Command line option syntax error这个安装错误. 新电脑或者重装系统后很多插件都没有,目前众多软件的运行环境又离不开插件,当在新环境下安

Warning: Using a password on the command line interface can be insecure.

做全备份不要使用名文,在命令行 [root@t2 ~]# mysqldump -uroot -pjsb --lock-all-tables --master-data=2 --events --routines --all-databases > /tmp/database_`date +%F`.sql Warning: Using a password on the command line interface can be insecure. [root@t2 ~]# mysqldump -

Can't use Subversion command line client:svn 报错处理_java

需要主意的有两点: 1.安装svn的时候,第二项,command line client tools也要安装 2.点击报错的fix it 打开了settings,把红色框中的地址改为安装svn地址路径下的svn.exe Android Studio面板中的以下两个按钮分别是 svn的跟新 和 上传 通过此文,希望能帮助大家解决此问题!谢谢大家对本站的支持!

MySQL5.6出现”Using a password on the command line interface…”解决办法

今天老左有在帮一个网友搬家网站过程中,习惯导出MySQL数据库的时候采用mysqldump命令,但是意外发生了出现"Warning: Using a password on the command line interface can be insecure."的错误提示,当然数据库肯定也没有能备份下来.这个问题应该是在MySQL5.6+版本的时候就有出现,可能是为了确保数据库的安全性采用的保护机制.   遇到问题那就去解决问题,大概搜索到国内的一些网站,大部分都是复制的,也没有讲的明

[iOS越狱开发]安装command line tools for Xcodew

网上搜了篇文章,介绍iOS的越狱开发,其中提到了要给Xcode安装command line tools,以前从没听过这个工具,然后就google了下. 关于Xcode Command Line Tools的介绍: https://developer.apple.com/library/ios/technotes/tn2339/_index.html What is the Command Line Tools Package? The Command Line Tools Package is