Ready to make the Linux history command work harder to serve your needs? Let's look at some new tricks you might want to shove up your sleeve. Credit: Bill Stilwell The Linux history command allows users to repeat commands without retyping them and to look over a list of commands they’ve recently used, but that’s just the obvious stuff. It is also highly configurable, allows you to pick and choose what you reuse (e.g., complete commands or portions of commands), and controls what commands are recorded. In today’s post, we’re going to run through the basics and then explore some of the more interesting behaviors of the history command. The basics of the Linux history command Typing “history” and getting a list of previously entered commands is the command’s most obvious use. Pressing the up arrow until you reach a command that you want to repeat and hitting enter to rerun it is next. And, as you probably know, you can also use the down arrow. In fact, you can scroll up and down your list of previously entered commands to review them or rerun them. Since commands in your history display are numbered, you can also enter a command such as !11 to repeat a command—in the case, the one in the 11th line of the history display. That’s the easy stuff. Configuring history What you might not know is how many aspects of the history command’s behavior you can control. You can determine how many commands are recorded (HISTSIZE), what file your commands are stored in (HISTFILE), whether sequentially repeated commands will show up in your command history once or as many times as they’re entered (HISTCONTROL), and whether some commands are not recorded at all (HISTIGNORE). You can also enter a command (history -c) to empty your history buffer and select the time format that you’d like to see displayed. The commands shown below do all of these things. These commands: Set the size of the history buffer to 100 (many admins choose to save 500 or 1,000 commands) Select the file to be used to store the commands you use (going with the default is generally best) Prevent the recording of sequentially repeated commands Remove previously repeated commands Prevent the recording of commands that are preceded by blanks Prevent the recording of specified commands Set the time format to be used when history is viewed Here are examples of the settings you might usee in your ~/.bashrc file: HISTSIZE=100 HISTFILE=~/.history HISTCONTROL=ignoredups HISTCONTROL=erasedups HISTCONTROL=ignorespace HISTIGNORE="history:pwd:date:ls:ls *:man *" HISTTIMEFORMAT="%h %d %H:%M:%S> " The HISTIGNORE setting can be very helpful in restricting the commands that are remembered to just those that are important to tracking your activities and those of other users on your system. There’s probably little value in recording that someone looked at a man page, listed files, displayed the date/time, or asked the system what directory they’re sitting in. Knowing that they edited some particular file or issued a command that shut some particular service down is another matter. While we can choose how time is displayed when we use the history command, that is not how it is stored. Instead, you’ll see time stored as “epoch time” inside the file. The only issue for HISTTIMEFORMAT is how that time value is displayed when you use the history command. #1497576143 vi .config #1497576172 rm .config- Picking and choosing what you repeat Linux history does not restrict you to repeating entire commands. You can reuse portions of commands by using some special arguments to represent the portions of commands that you want to reuse. For example, to reuse the last argument in the previously entered command, use ! (previous command) followed by :$ as shown in the example below. $ echo one two three one two three $ echo !:$ echo three three To reuse the first argument, try !^. $ echo !^ echo one one To reuse a particular argument, provide its position in the list of arguments. $ vi !echo:2 vi two And please note that in the last example above, we’re plucking an argument not from the last command entered, but the last echo command entered. This works for any of the argument positions just discussed. Wrap-up I should probably end this post with some comment about being doomed to repeat history, but the history command provides too many useful options for anything so gloomy. I hope you’ve picked up a trick or two that makes your use of this command more rewarding. Related content how-to How to find files on Linux There are many options you can use to find files on Linux, including searching by file name (or partial name), age, owner, group, size, type and inode number. By Sandra Henry Stocker Jun 24, 2024 8 mins Linux opinion Linux in your car: Red Hat’s milestone collaboration with exida With contributions from Red Hat and critical collaborators, the safety and security of automotive vehicles has reached a new level of reliability. By Sandra Henry Stocker Jun 17, 2024 5 mins Linux how-to How to print from the Linux command line: double-sided, landscape and more There's a lot more to printing from the Linux command line than the lp command. Check out some of the many available options. By Sandra Henry Stocker Jun 11, 2024 6 mins Linux how-to Converting between uppercase and lowercase on the Linux command line Converting text between uppercase and lowercase can be very tedious, especially when you want to avoid inadvertent misspellings. Fortunately, Linux provides a handful of commands that can make the job very easy. By Sandra Henry Stocker Jun 07, 2024 5 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe