The history command on Linux can be used to display and rerun commands that you've used, and it can also help you hide commands that you don't want remembered. Credit: STILLFX / Getty Images Linux shells like bash have a convenient way of remembering commands that you type, making it easy to run them again without having to retype them. Just use the history command (which is a bash built-in) and then use an exclamation point followed by the number shown in front of the command in the history command output that you want to rerun. Alternatively, you can back up to that command by pressing the up arrow key as many times as needed to reach that command and then press return. Don’t forget, though, that you can also set up commands you are likely to use often as aliases by adding a line like this to your ~/.bashrc file so that you don’t need to search for them in your command history. Here’s an example: $ alias users="who | awk '{print $1}'" $ users myacct shs shs $ alias uusers="who | awk '{print $1}' | uniq" The aliases above will display logged in users. The second removes duplicates. Add the commands to your ~/.bashrc file to have them set up each time you log in. Making use of the history command can make it easier to reuse commands and avoid typos. However, there are ways to get Linux to ignore some of the commands that you type so that your history buffer doesn’t fill up with commands you don’t want it to remember – especially if those commands would take up a lot of room in your history buffer. Command history can also help with troubleshooting a problem as you can get a listing of the commands that were run before the problem appeared. How much to remember The $HISTSIZE setting controls how many commands will be remembered. If you only want the most recent 100 lines to be remembered in your history buffer, you could run a command like this: $ export HISTSIZE=100 In general, $HISTSIZE defaults to 1,000 commands. Check it by running a command like this one: $ echo $HISTSIZE 1000 Change the setting in your ~/.bashrc file if you want history to remember more or fewer commands. Ignoring specific commands You can keep certain commands from being saved in your history buffer by adding them to the $HISTIGNORE variable. In the setting shown below, the pwd, date, history and clear commands will be ignored since these commands are unlikely to be rerun using command history. $ export HISTIGNORE='pwd:date:history:clear' $ pwd /home/myacct $ history $ history | tail -1 86 pwd 87 history | tail -1 Note, however, that the history | tail -1 command shown above is captured, but not the history command when entered by itself. Ignoring duplicate commands If you type a command some number of times in a row and your $HISTCONTROL setting contains the string “ignoredups” (i.e., ignore duplicates), you’ll only see the command once for each repetitive sequence. $ echo hello hello $ pwd /home/myacct $ pwd /home/myacct $ who | wc -l 4 $ pwd $ history | tail -5 79 echo hello 80 pwd Ignoring commands that are entered with a preceding space You can also elect to have your history buffer fail to remember any command that you enter when you run the command after pressing the space key – for example, if you type “ date” instead of “date”. This allows you to have your command history ignore any command without having to set up each command separately in your $HISTIGNORE setting. In fact, if you want your command history to omit both commands that start with a space along with duplicate commands entered sequentially, either of these settings will do the same thing. Add them to your ~/.bashrc file to make them permanent. $ export HISTCONTROL="ignoredups:ignorespace" $ export HISTCONTROL="ignoreboth" To be clear, the “ignoreboth” setting means to ignore both duplicate commands when entered sequentially and commands that are entered with a preceding space. Your history file The command history file is identified by the $HISTFILE setting, but is rarely anything but .bash_history. Note that commands you just entered will not yet have been added to this file. $ echo $HISTFILE /home/shs/.bash_history The command below would be added to my history buffer, but note that it doesn’t show up when I ask to see the bottom of my .bash_history file. $ echo byebye for now byebye for now $ tail -1 ~/.bash_history tail -2 ~/.bash_history But on next login, I will see this: $ tail -1 ~/.bash_history echo byebye for now Wrap-up Deciding what commands you want remembered in your command history file and which you want ignored can improve the usefulness of your command history – especially if you don’t want to scan through hundreds of remembered commands to find the handful that you want to return. I hope that some part of this post has left you with some good ideas about making your command history more valuable. Related content how-to How to examine files on Linux Linux provides very useful options for viewing file attributes, such as owners and permissions, as well as file content. By Sandra Henry Stocker Oct 24, 2024 6 mins Linux how-to 8 easy ways to reuse commands on Linux Typing the same command again and again can become tiresome. Here are a number of ways you can make repeating commands – or repeating commands but with some changes – a lot easier than you might expect. By Sandra Henry-Stocker Oct 15, 2024 5 mins Linux news SUSE Edge upgrade targets Kubernetes and Linux at the edge SUSE Edge 3.1 includes a new stack validation framework and an image builder tool that are aimed at improving the scalability and manageability of complex Kubernetes and Linux edge-computing deployments. By Sean Michael Kerner Oct 15, 2024 6 mins Edge Computing Linux Network Management Software how-to Lesser-known xargs command is a versatile time saver Boost your Linux command line options and simplify your work with xargs, a handy tool for a number of data manipulation tasks. By Sandra Henry Stocker Oct 11, 2024 6 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe