- history command example
Linux bash history keeps every command a user typed in the command line terminal into a file named .bash_history.
Here is an example of Linux history command with no option running in Ubuntu server system command line terminal:
linuxcare@example.com:~$ history |
In the example above, number 1 is the oldest command and number 10 is the latest command that a current user has typed.
If you never clear the history file, you might see hundreds of commands that you typed including the latest 'history'. The bash history command would print only the latest page of the command history. However you can view bash commands history from the first page with pipe, like an example below:
linuxcare@example.com:~$ history | less |
Let's see another example of bash history options. You can also use event designators to run command in the history using the ! key. See an example below:
linuxcare@example.com:~$ !8 |
The example above run number 8 command in the history file, which is the man route command. Here is a complete options available with event designators:
Event Designators |
You can clear Linux bash history if it is too large or you don't want other people see what you were doing in the command line terminal. If you want to clear the bash history, use history command with -c option. See example below:
linuxcare@example.com:~$ history -c |
Linux history command can be really helpful when we are working in command line terminal. We can save a lot of times not typing regularly used command such as ls, cd and many more. However, keeping all bash history commands also might cause security threat to the system. For example, consider the root bash history file was read by unauthorised person. What you can do is always clear history after root session.
You can also delete line in history file. Let's look at the example Linux history file below:
1 updatedb |
If I want to delete the history line number 4, what I have to do is to use history command with '-d 4' option. The -d option deletes history entry while the 4 argument is history entry line 4. See the example below:
linuxcare@example.com:~$ history -d 4 |
Now let's look at the Linux history file again:
1 updatedb |
The history entry number 4 has been deleted and the previous number 5 entry is now at number 4.
Here are other history command options that you can try:
- -a — Appends to history file.
- -n — Loads from history file.
- -p — Performs history lookup/substitution.
- -r — Reads history from a file.
- -s — Adds new history entries.
That is all the Linux history command options regularly used in Linux administration.
0 comments:
Post a Comment