Recording your commands on the Linux command line

linux ventura 100937735 large

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.

Command history

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

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

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 the script command

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.

Copyright © 2023 IDG Communications, Inc.

gsmarena 000

Realme Narzo N55 teased ahead of imminent launch

gsmarena 000

Sony WF-C700 TWS earphones debut, WH-1000XM5 get Midnight Blue color