Using the Linux “history” command

With a Linux dedicated server, you will execute tens of commands over the course of a month or even a year. Keeping track of all of them may prove to be too difficult for the average busy system administrator. Nevertheless, you owe it to yourself and your clients to make sure you can effectively execute commands quickly and with some coherence.

Fortunately, Linux keeps saves your commands for you. The history command is a convenient tool that you can use to review previous commands. To look at the most recent commands, simply press the up arrow key. Each time you press it, you will go deeper into the command history. To see all of the command history, type “history” from the command line. You can even export that list to a file to save for future use by entering:

history -w history-list.txt

If you need to append your current history to the end of a previously created history file, type:

history -a history-list.txt

You can also append whatever you have in your history file to the end of your current history list (useful if it has been erased). Type:

history -r history-list.txt

You will notice that each command in the history list is numbered. Each number represents a shortcut that you can use to call any previous command. For example, if “cat /proc/cpuinfo” is number 136 on the list, you can call it by placing a “!” in front of the number. For example:

!136

Press Enter, and it will automatically execute the associated command.

Finally, if you ever need to empty your history list, for either security or privacy reasons, type:

history -C

For more information about the history command, type “man history” for the command line.











Comments: