Mastering the Command Line History in Linux

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.

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

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

LinuxHistory1

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:

LinuxHistory2

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

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

LinuxHistory3

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

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

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.

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.

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:

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

5 comments

  1. I tried to use !! and the corresponding commands in my workflow a while ago but it’s not as useful as PgUp or PgDown – with these I only need do write the command and can switch through every unique calling of that command.
    Also I can easily apply changes to the line, which !! doesn’t provide.

    But carrying over an argument or preventing certain commands to be shown is awesome – thanks!

    I hope you make a series out of this, since there are much more things, that aren’t commonly known (e.g. “cd -” which changes your current directory with your last, thus enabling you to switch between two folders easily, no matter where they are on your system).

    • Hey Nelo,
      Glad you found it useful. Sure I can do a series. What’s your main problem area that you’d like to get command line shortcuts to? Let us know what’s annoying you, and we’ll help figure out the shortcuts/apps/tricks to make it better.

      • Well, you could make a further introduction to bash syntax/scripting (e.g. what is the difference between (), {} and [] in bash; in which different ways can pipeline, time and tee be used; how to make use of the mail command).

        Maybe a bit about I/O-Redirection – I’m using find 2>/dev/null to redirect the STDERR to nirvana when searching on a non-administrative user, or when starting a job in background w/ & the STDOUT and STDERR still get’s displayed on the terminal – cut that off w/ &> /dev/null (or to a log-file).

        Also neat things would be ways to make use of the .bash_logout (e.g. mine clears my ~/temp/ folder) and to add a piece of script to the .bashrc that automatically sources itself, as soon as changes were made (this can be achieved using stat -c and test).

        These are things I had to work out in months of work, while reading many books and posts and wished there were somebody that told me earlier.

  2. I regularly use !$ as the last argument from the previous command.

    Loyal.W@PHX2LT ~
    $ ls a b c
    a b c

    Loyal.W@PHXQLT ~
    $ ls -l !$
    ls -l c
    -rw-r–r– 1 Loyal.Wiens Domain Users 0 Apr 9 07:48 c

    Loyal.Wiens@PHXQLT ~

  3. Struggling a bit with this one:

    ls | more
    then:
    ls -al !$
    result:
    ls -al more
    ls: cannot access more: No such file or directory

    How can I take over ALL arguments of the last command?

Comments are closed.

Sponsored Stories