The history
Linux command is a great tool that shows a list of the entire directions {{that a}} individual has up to now accomplished inside the terminal. This tick list is stored in a history file, and thru using the history command, you’ll view this file, making it easier to recall and reuse earlier directions.
This command is continuously used to track actions performed inside the terminal, in particular when troubleshooting or repeating sophisticated command sequences. It will save time and effort thru allowing consumers to quickly recall and rerun previous directions with out a want to type them out yet again. Some consumers moreover benefit from the history to audit or evaluate actions taken inside the system, serving to in system control and protection.
Fundamental syntax for history
command:
history [OPTIONS...]
1. Show used directions
history [number]
This command shows a limited number of up to now accomplished directions.
Example:
The history 5
command in Linux will display the overall 5 directions which were accomplished inside the terminal. Proper right here’s a trend output:
501 git status 502 cd /var/www/html 503 ls -la 504 nano myfile.txt 505 history 5
Each line shows a command from the history, preceded thru a unique amount that represents the command’s position inside the history file. In this example, the overall 5 directions which were run are confirmed, along side the history 5
command itself.
2. Delete directions from history tick list
history -d [number]
Use the -d
option to delete directions from the history tick list.
Example:
Faster than deleting:
1 ls 2 cd Bureaucracy/ 3 touch file.txt 4 nano file.txt 5 rm file.txt 6 history
After running history -d 5
:
1 ls 2 cd Bureaucracy/ 3 touch file.txt 4 nano file.txt 5 history
As you’ll see, the 5th command (rm file.txt
) has been removed from the history.
3. Search used command inside history
history | grep [text]
The history | grep
command helps you in finding directions that have compatibility a text construction.
Example:
The history | grep cat
command in Linux will search by way of your command history and display the entire lines that include the word “cat.” The grep command filters the output of the history command to easily show lines with the specified construction, in this case, “cat.” Proper right here’s a trend output:
42 cat file.txt 73 cat /and so on/passwd 100 cat bureaucracy/bear in mind.txt | further 123 concatenate file1.txt file2.txt > combined.txt 150 cat logs/error.log | grep "ERROR"
In this example, each line begins with a number representing the command’s position inside the history, followed in the course of the command itself. It contains every instance where “cat” turns out, whether or not or no longer it’s the cat
command itself or just part of another word or command.
4. Clearing the history tick list
history -c
The -c (clear)
selection is used to clear all directions from the history tick list.
5. Execute the command with fit amount
!amount
This command means that you can execute directions with an fit amount from the history tick list. There is no need to write an entire command.
Example:
Suppose the 10th command on your history was once as soon as ls -l
, which lists data inside the provide list in long construction. Will have to you run !10
, the output would possibly look something like this:
basic 24 drwxr-xr-x 5 individual individual 4096 Aug 6 10:00 Bureaucracy drwxr-xr-x 2 individual individual 4096 Aug 6 09:45 Downloads drwxr-xr-x 2 individual individual 4096 Aug 6 09:45 Observe drwxr-xr-x 2 individual individual 4096 Aug 6 09:45 Pictures
This output shows the contents of the current list, merely as for those who had run the ls -l
command without delay.
6. Write changes to bash_history file
history -w [filename]
Use this option to write all changes made inside the provide session to the bash_history
file.
Example:
The history -w file.txt
command in Linux doesn’t produce output inside the terminal itself. Instead, it writes the existing history of directions to a file named file.txt
.
So, for those who were to open file.txt
, it’s imaginable you’ll see something like this:
1 cd /space/individual/bureaucracy 2 ls -la 3 sudo apt-get exchange 4 nano file.txt 5 history -w file.txt
This file now contains the tick list of directions which were up to now accomplished inside the terminal, and also you’ll view or edit it using any text editor.
Additional Linux directions:
Record Operations | rmdir · cd · pwd |
Record Operations | cat · cp · dd · much less · ls · mkdir · mv · tail · tar · zip |
Record Gadget Operations | chown · mkfs |
Networking | ping · curl · wget · iptables |
Search and Text Processing | to find · grep · sed · whatis |
Gadget Information and Keep watch over | env · historical past · best · who |
Individual and Session Keep watch over | display · su · sudo |
The put up How you can Use Historical past Command in Linux appeared first on Hongkiat.
Supply: https://www.hongkiat.com/blog/linux-command-history/
0 Comments