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 news Supermicro unveils AI-optimized storage powered by Nvidia New storage system features multiple Nvidia GPUs for high-speed throughput. By Andy Patrizio Oct 24, 2024 3 mins Enterprise Storage Data Center news Nvidia to power India’s AI factories with tens of thousands of AI chips India’s cloud providers and server manufacturers plan to boost Nvidia GPU deployment nearly tenfold by the year’s end compared to 18 months ago. By Prasanth Aby Thomas Oct 24, 2024 5 mins GPUs Artificial Intelligence Data Center news Gartner: 13 AI insights for enterprise IT Costs, security, management and employee impact are among the core AI challenges that enterprises face. By Michael Cooney Oct 23, 2024 6 mins Generative AI Careers Data Center news Network jobs watch: Hiring, skills and certification trends What IT leaders need to know about expanding responsibilities, new titles and hot skills for network professionals and I&O teams. By Denise Dubie Oct 23, 2024 33 mins Careers Data Center Networking PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe