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.

  1. # export HISTTIMEFORMAT='%F %T '
  2. # history | more
  3. 1 2008-08-05 19:02:39 service network restart
  4. 2 2008-08-05 19:02:39 exit
  5. 3 2008-08-05 19:02:39 id
  6. 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.

  1. # [Press Ctrl+R from the command prompt,
  2. which will display the reverse-i-search prompt]
  3. (reverse-i-search)`red': cat /etc/redhat-release
  4. [Note: Press enter when you see your command,
  5. which will execute the command from the history]
  6. # cat /etc/redhat-release
  7. 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.

  1. # [Press Ctrl+R from the command prompt,
  2. which will display the reverse-i-search prompt]
  3. (reverse-i-search)`httpd': service httpd stop
  4. [Note: Press either left arrow or right arrow key when you see your
  5. command, which will display the command for you to edit, before executing it]
  6. # 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.

  1. # history | more
  2. 1 service network restart
  3. 2 exit
  4. 3 id
  5. 4 cat /etc/redhat-release
  6.  
  7. # !4
  8. cat /etc/redhat-release
  9. 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’.

  1. # !ps
  2. ps aux | grep yp
  3. root 16947 0.0 0.1 36516 1264 ? Sl 13:10 0:00 ypbind
  4. 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.

  1. # vi ~/.bash_profile
  2. HISTSIZE=450
  3. 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.

  1. # vi ~/.bash_profile
  2. 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.

  1. # pwd
  2. # pwd
  3. # pwd
  4. # history | tail -4
  5. 44 pwd
  6. 45 pwd
  7. 46 pwd [Note that there are three pwd commands in history, after
  8. executing pwd 3 times as shown above]
  9. 47 history | tail -4
  10.  
  11. # export HISTCONTROL=ignoredups
  12. # pwd
  13. # pwd
  14. # pwd
  15. # history | tail -3
  16. 56 export HISTCONTROL=ignoredups
  17. 57 pwd [Note that there is only one pwd command in the history, even after
  18. executing pwd 3 times as shown above]
  19. 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.

  1. # export HISTCONTROL=erasedups
  2. # pwd
  3. # service httpd stop
  4. # history | tail -3
  5. 38 pwd
  6. 39 service httpd stop
  7. 40 history | tail -3
  8.  
  9. # ls -ltr
  10. # service httpd stop
  11. # history | tail -6
  12. 35 export HISTCONTROL=erasedups
  13. 36 pwd
  14. 37 history | tail -3
  15. 38 ls -ltr
  16. 39 service httpd stop
  17. [Note that the previous service httpd stop after pwd got erased]
  18. 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.

  1. # export HISTCONTROL=ignorespace
  2. # ls -ltr
  3. # pwd
  4. # service httpd stop [Note that there is a space at the beginning of service,
  5. to ignore this command from history]
  6. # history | tail -3
  7. 67 ls -ltr
  8. 68 pwd
  9. 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.

  1. # 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.

  1. # ls anaconda-ks.cfg
  2. anaconda-ks.cfg
  3. # vi !!:$
  4. 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).

  1. # cp anaconda-ks.cfg anaconda-ks.cfg.bak
  2. anaconda-ks.cfg
  3. # vi !^
  4. 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.

  1. # cp ~/longname.txt /really/a/very/long/path/long-filename.txt
  2. # ls -l !cp:2
  3. 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.

  1. # ls -l !cp:$
  2. 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.

  1. # export HISTSIZE=0
  2. # history
  3. # [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.

  1. # export HISTIGNORE="pwd:ls:ls -ltr:"
  2. # pwd
  3. # ls
  4. # ls -ltr
  5. # service httpd stop
  6.  
  7. # history | tail -3
  8. 79 export HISTIGNORE="pwd:ls:ls -ltr:"
  9. 80 service httpd stop
  10. 81 history
  11. [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.

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

  1. [笔记]The Linux command line

    Notes on The Linux Command Line (by W. E. Shotts Jr.) edited by Gopher 感觉博客园是不是搞了什么CSS在里头--在博客园显示效果挺 ...

  2. Linux Command Line Basics

    Most of this note comes from the Beginning the Linux Command Line, Second Edition by Sander van Vugt ...

  3. 5 Ways to Send Email From Linux Command Line

    https://tecadmin.net/ways-to-send-email-from-linux-command-line/ We all know the importance of email ...

  4. 《The Linux Command Line》 读书笔记04 Linux用户以及权限相关命令

    Linux用户以及权限相关命令 查看身份 id:Display user identity. 这个命令的输出会显示uid,gid和用户所属的组. uid即user ID,这是账户创建时被赋予的. gi ...

  5. 《The Linux Command Line》 读书笔记02 关于命令的命令

    <The Linux Command Line> 读书笔记02 关于命令的命令 命令的四种类型 type type—Indicate how a command name is inter ...

  6. 《The Linux Command Line》 读书笔记01 基本命令介绍

    <The Linux Command Line> 读书笔记01 基本命令介绍 1. What is the Shell? The Shell is a program that takes ...

  7. Linux Command Line 解析

    Linux Command Line 解析 0 处理模型 Linux kernel的启动包括很多组件的初始化和相关配置,这些配置参数一般是通过command line进行配置的.在进行后续分析之前,先 ...

  8. 10 Interesting Linux Command Line Tricks and Tips Worth Knowing

    I passionately enjoy working with commands as they offer more control over a Linux system than GUIs( ...

  9. Reso | The Linux Command Line 的中文版

    http://book.haoduoshipin.com/tlcl/book/zh/ 本书是 The Linux Command Line 的中文版, 为大家提供了多种不同的阅读方式. 中英文双语版- ...

随机推荐

  1. Eclipse c++环境搭建 并加载OpenCV库 2015最新

    C++: 搜索 Eclipse c++ 即可 注意新版的mingw安装器,要安装: 1.mingw-developer-toolkit 2.mingw32-base 3.mingw32-gcc-g++ ...

  2. aspose.word 在书签处插入符号

    doc.Range.Bookmarks["CBJYQQDFS110"].Text = ""; Aspose.Words.DocumentBuilder buil ...

  3. linux清空日志文件内容 (转)

    随着系统运行时间越来越长,日志文件的大小也会随之变得越来越大.如果长期让这些历史日志保存在系统中,将会占用大量的磁盘空间.用户可以直接把这些日志文件删除,但删除日志文件可能会造成一些意想不到的后果.为 ...

  4. LruCache--远程图片获取与本地缓存

    Class Overview A cache that holds strong references to a limited number of values. Each time a value ...

  5. BF-KMP 算法

    #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<string. ...

  6. word添加页码

    问:在Word里面,分两栏插入页码,怎么使两栏都有页码? 可以通过插入域来实现分两栏后两栏都有页码.左边一栏页码为 当前页页码×2-1 :右边一栏页码为 当前页页码×2.在页眉页脚视图中,分别在左右两 ...

  7. “内部类” 大总结(Java)

    (本文整理自很久以前收集的资料(我只是做了排版修改),作者小明,链接地址没有找到,总之感谢,小明) (后面也对"静态内部类"专门做了补充) 内部类的位置: 内部类可以作用在方法里以 ...

  8. [转] js prototype详解

    JavaScript能够实现的面向对象的特征有:·公有属性(public field)·公有方法(public Method)·私有属性(private field)·私有方法(private fie ...

  9. Spring Batch的事务– Part 3: 略过和重试

    原文:https://blog.codecentric.de/en/2012/03/transactions-in-spring-batch-part-3-skip-and-retry/ This i ...

  10. Bluebird-Core API (三)

    Promise.join Promise.join( Promise<any>|any values..., function handler ) – -> Promise For ...