Linux offers a couple of easy ways to record commands you type so that you can review or rerun them. Recording the commands that you run on the Linux command line can be useful for two important reasons. For one, the recorded commands provide a way to review your command line activity, which is extremely helpful if something didn’t work as expected and you need to take a closer look. In addition, capturing commands can make it easy to repeat the commands or to turn them into scripts or aliases for long-term reuse. This post examines two ways that you can easily record and reuse commands. Using history to record Linux commands The history command makes it extremely easy to record commands that you enter on the command line because it happens automatically. The only thing you might want to check is the setting that determines how many commands are retained and, therefore, how long they’re going to stay around for viewing and reusing. The command below will display your command history buffer size. If it’s 1,000 like that shown, it will retain the last 1,000 commands that you entered. $ echo $HISTSIZE 1000 The history command below shows the most recent two commands that were run. Since the list ends with command number 1013, it will have started with 14. $ history | tail -2 1012 05/04/23 14:26:11 vi myfile 1013 05/04/23 14:28:30 history | tail -2 When you first open a session on the command line, the oldest commands in your history buffer will be numbered 1 and 2. Oldest commands are displayed first. $ history | head -2 1 04/03/23 11:35:11 vi getdocs 2 04/03/23 11:35:30 getdocs Passing the output of the history command to more will scroll through your recorded commands a screenful at a time. $ history | more Ignoring commands To avoid saving all of your commands in your command history buffer, you can use the HISTSIGNORE variable. I use one like this to avoid storing commands that I don’t want to preserve, leaving more space for those I do. $ grep HISTIGNORE ~/.bashrc HISTIGNORE="pwd:clear:cd:ls:man:history" Reusing commands from your history buffer Any command in the history buffer can be rerun by entering its command number following an exclamation point (e.g., !927). Commands will retain their history command numbers during a single login session. You can also use your up arrow key to back up over any number of recent commands. When you reach the one that you want to rerun, press the enter key and it will be run again. Using script to record Linux commands The script command provides a way to record as many commands as you want “on the fly”. In other words, type “script” and each command that you enter will automatically be saved in a file. The file will be called “typescript” unless you give it a different name by adding it to the command like this: $ script script11 If you want to add the commands you are preparing to enter and their output to an existing output file, you can use the -a (append) option to your script command. Here’s an example: $ script -a script11 Additional details about the script command The script command will run your .bashrc start-up file when you start your command recording. It will save the commands you enter along with the output generated by them. The file used to save entered commands will not be given execute permission. If you want to turn the commands into a script, you can set that up after removing the command output and retaining only those commands you want to preserve. 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