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.\nUsing history to record Linux commands\nThe 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.\n$ echo $HISTSIZE\n1000\n\nThe 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.\n$ history | tail -2\n 1012 05\/04\/23 14:26:11 vi myfile\n 1013 05\/04\/23 14:28:30 history | tail -2\n\nWhen 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.\n$ history | head -2\n 1 04\/03\/23 11:35:11 vi getdocs\n 2 04\/03\/23 11:35:30 getdocs\n\nPassing the output of the history command to more will scroll through your recorded commands a screenful at a time.\n$ history | more\n\nIgnoring commands\nTo 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.\n$ grep HISTIGNORE ~\/.bashrc\nHISTIGNORE="pwd:clear:cd:ls:man:history"\n\nReusing commands from your history buffer\nAny 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.\nYou 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.\nUsing script to record Linux commands\nThe 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:\n$ script script11\n\nIf 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:\n$ script -a script11\n\nAdditional details about the script command\nThe 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.