Mastering the Command Line History in Linux

Spread the love

If you spend a lot of time working on Linux, it makes sense to learn how to optimize your use of the command line options – especially the time you spend typing in commands. Most Linux users are familiar with the basic Linux command line history. The simplest way to avoid typing out a command is to hit the up arrow key until you find your command, or to use the simple !! for the last executed command. Today we’ll show you a few relatively unknown, and yet effective ways you can use Linux history to speed up the way you work.

Extend the humble !! to work for you

Say you ran a command that required root privilege, but you forgot to do a sudo. Rather than type it out again, or navigate up and then left, here’s what you can do

$ touch build_04052014.tgz
touch: cannot touch 'build_04052014.tgz': Permission denied
 
$ sudo !!
sudo touch new_binary
[sudo] password for richa:

If you want to save the previous command into a new script, here’s how you can do that, without having to type it out

$ touch build_04052014.tgz
$ echo !! > script1.sh
$ cat script1.sh
touch build_04052014.tgz

Search & run command from history

Having access to the last run command has limited usage. Sometimes you would want to search through the history to find an obscure or really large command you’d typed out. A simple ![keyword] search lets you access it. For example, you had a large command that you’d run in the morning, and you don’t quite remember the options you gave, but you still want to run it again. If this is what a screenshot of your history from that time was:

So how do you run that command again? Just do:

$ !make
make brutus.mk && tar brutus/* brutus.tgz && cp brutus.tgz ../output
Building....

How to carry over an argument from the previous command

Sometimes you may not want to execute an earlier command, but just carry over the argument. You can use the “!$” operator like shown below.

If there were multiple arguments to your earlier command, and you want to access only the first, you can use “!^

$ ls *.png *.txt *.tgz
build_04052014.tgz OOO2.png out2.png unix.txt
OOO1.png out1.png out3.png windows.txt
 
$ ls -a !^
ls -a *.png
OOO1.png OOO2.png out1.png out2.png out3.png

To access argument number x, just use “![prev_command]:[argument_number]“. The example below shows it more clearly.

$ ls *.png *.txt *.tgz
build_04052014.tgz OOO2.png out2.png unix.txt
OOO1.png out1.png out3.png windows.txt
 
$ ls -a !ls:3
ls -a *.tgz
build_04052014.tgz

What if you have some command you don’t want to show up in the history at all – if you just want to remove duplicates and have a cleaner history. You can set the HISTCONTROL environment variable to handle this. Here’s how to do it.

$ export HISTCONTROL = erasedups

Prevent a Command From Appearing in the History

At times, you may want to prevent simple commands form appearing in the history, like “ls” or “pwd“. In such cases, you can prevent them from appearing in the history by using the HISTCONTROL command again, like we did in the above example. However, it’s usually not a good idea to hide a command from appearing in the history.

To hide a command, you need to first set the HISTCONTROL to ignore spaces, i.e. ignore all commands that start with space. Then when you type the command, make sure to add an extra space before you type. This will prevent history from remembering the command.

$ export HISTCONTROL = ignorespace

Just in case for some strange reason, you may want to disable your Linux history entirely. To do that, you can use the following command:

$ export HISTSIZE = 0

If you search for your history now, it will be blank. Note that the HISTSIZE command can also be used to change the size of your history. If you wanted to store more commands in history, open up the .bash_profile file in your Home directory and change the HISTSIZE (make it bigger or smaller).

Image credit: bash command-line linux shell terminal , Introduction to the Command Line cover

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Sign up for all newsletters.
By signing up, you agree to our Privacy Policy and European users agree to the data transfer policy. We will not share your data and you can unsubscribe at any time. Subscribe


Richa

I’m a techie with over a decade of programming experience, spread across a wide range of interesting, path breaking technologies. Now I’m sharing my passion for technology, and making tech easier, with everyone! Hope you enjoy reading about, and playing with technology, as much as I do!

Comments (5)