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 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