Monday, April 3, 2023
HomeNetworkingRecording your instructions on the Linux command line

Recording your instructions on the Linux command line


Recording the instructions that you simply run on the Linux command line will be helpful for 2 necessary causes. For one, the recorded instructions present a method to assessment your command line exercise, which is extraordinarily useful if one thing did not work as anticipated and you’ll want to take a more in-depth look. As well as, capturing instructions could make it straightforward to repeat the instructions or to show them into scripts or aliases for long-term reuse. This submit examines two methods which you can simply report and reuse instructions.

Command historical past

The historical past command makes it extraordinarily straightforward to report instructions that you simply enter on the command line as a result of it occurs robotically. The one factor you would possibly wish to test is the setting that determines what number of instructions are retained and, subsequently, how lengthy they are going to keep round for viewing and reusing. The command beneath will show your command historical past buffer measurement. If it is 1,000 like that proven, it should retain the final 1,000 instructions that you simply entered.

$ echo $HISTSIZE
1000

The historical past command beneath reveals the newest two instructions that have been run. Because the listing ends with command quantity 1013, it should have began with 14.

$ historical past | tail -2
 1012  05/04/23 14:26:11 vi myfile
 1013  05/04/23 14:28:30 historical past | tail -2

If you first open a session on the command line, the oldest instructions in your historical past buffer will probably be numbered 1 and a couple of. Oldest instructions are displayed first.

$ historical past | head -2
    1  04/03/23 11:35:11 vi getdocs
    2  04/03/23 11:35:30 getdocs

Passing the output of the historical past command to extra will scroll by your recorded instructions a screenful at a time.

$ historical past | extra

Ignoring instructions

To keep away from saving your entire instructions in your command historical past buffer, you need to use the HISTSIGNORE variable. I take advantage of one like this to keep away from storing instructions that I do not wish to protect, leaving extra space for these I do.

$ grep HISTIGNORE ~/.bashrc
HISTIGNORE="pwd:clear:cd:ls:man:historical past"

Reusing instructions out of your historical past buffer

Any command within the historical past buffer will be rerun by coming into its command quantity following an exclamation level (e.g., !927). Instructions will retain their historical past command numbers throughout a single login session.

You can too use your up arrow key to again up over any variety of latest instructions. If you attain the one that you simply wish to rerun, press the enter key and it is going to be run once more.

Utilizing the script command

The script command gives a method to report as many instructions as you need “on the fly”. In different phrases, kind “script” and every command that you simply enter will robotically be saved in a file. The file will probably be referred to as “typescript” until you give it a distinct title by including it to the command like this:

$ script script11

If you wish to add the instructions you’re making ready to enter and their output to an current output file, you need to use the -a (append) choice to your script command. This is an instance:

$ script -a script11

Further particulars in regards to the script command

The script command will run your .bashrc start-up file if you begin your command recording. It should save the instructions you enter together with the output generated by them. The file used to avoid wasting entered instructions won’t be given execute permission. If you wish to flip the instructions right into a script, you’ll be able to set that up after eradicating the command output and retaining solely these instructions you wish to protect.

Copyright © 2023 IDG Communications, Inc.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments