The historical past Linux command is a handy gizmo that presentations a listing of all of the instructions {that a} person has in the past done within the terminal. This checklist is saved in a historical past document, and through the use of the historical past command, you’ll view this document, making it more uncomplicated to recall and reuse previous instructions.

This command is frequently used to trace movements carried out within the terminal, particularly when troubleshooting or repeating complicated command sequences. It may save effort and time through permitting customers to temporarily recall and rerun earlier instructions with no need to kind them out once more. Some customers additionally make the most of the historical past to audit or assessment movements taken within the gadget, helping in gadget management and safety.

Basic syntax for historical past command:

historical past [OPTIONS...]
1. Display used instructions
historical past [number]

This command presentations a restricted collection of in the past done instructions.

Instance:

The historical past 5 command in Linux will show the final 5 instructions that have been done within the terminal. Right here’s a pattern output:

  501  git standing
  502  cd /var/www/html
  503  ls -la
  504  nano myfile.txt
  505  historical past 5

Every line presentations a command from the historical past, preceded through a singular quantity that represents the command’s place within the historical past document. On this instance, the final 5 instructions that have been run are proven, together with the historical past 5 command itself.

2. Delete instructions from historical past checklist
historical past -d [number]

Use the -d solution to delete instructions from the historical past checklist.

Instance:

Sooner than deleting:

1  ls
2  cd Paperwork/
3  contact document.txt
4  nano document.txt
5  rm document.txt
6  historical past

After working historical past -d 5:

1  ls
2  cd Paperwork/
3  contact document.txt
4  nano document.txt
5  historical past

As you’ll see, the fifth command (rm document.txt) has been got rid of from the historical past.

3. Seek used command inside of historical past
historical past | grep [text]

The historical past | grep command is helping you to find instructions that fit a textual content development.

Instance:

The historical past | grep cat command in Linux will seek via your command historical past and show all of the strains that comprise the phrase “cat.” The grep command filters the output of the historical past command to simply display strains with the desired development, on this case, “cat.” Right here’s a pattern output:

  42  cat document.txt
  73  cat /and so forth/passwd
 100  cat paperwork/be aware.txt | extra
 123  concatenate file1.txt file2.txt > blended.txt
 150  cat logs/error.log | grep "ERROR"

On this instance, every line starts with a host representing the command’s place within the historical past, adopted through the command itself. It comprises each example the place “cat” seems, whether or not it’s the cat command itself or simply a part of some other phrase or command.

4. Clearing the historical past checklist
historical past -c

The -c (transparent) choice is used to transparent all instructions from the historical past checklist.

5. Execute the command with match quantity
!quantity

This command lets you execute instructions with an match quantity from the historical past checklist. There is not any want to write a whole command.

Instance:

Think the tenth command to your historical past was once ls -l, which lists information within the present listing in lengthy structure. Should you run !10, the output may glance one thing like this:

general 24
drwxr-xr-x 5 person person 4096 Aug  6 10:00 Paperwork
drwxr-xr-x 2 person person 4096 Aug  6 09:45 Downloads
drwxr-xr-x 2 person person 4096 Aug  6 09:45 Track
drwxr-xr-x 2 person person 4096 Aug  6 09:45 Footage

This output presentations the contents of the present listing, simply as in the event you had run the ls -l command at once.

6. Write adjustments to bash_history document
historical past -w [filename]

Use this solution to write all adjustments made within the present consultation to the bash_history document.

Instance:

The historical past -w document.txt command in Linux doesn’t produce output within the terminal itself. As a substitute, it writes the present historical past of instructions to a document named document.txt.

So, in the event you have been to open document.txt, it’s possible you’ll see one thing like this:

1 cd /house/person/paperwork
2 ls -la
3 sudo apt-get replace
4 nano document.txt
5 historical past -w document.txt

This document now comprises the checklist of instructions that have been in the past done within the terminal, and you’ll view or edit it the use of any textual content editor.

Extra Linux instructions:
Listing Operations rmdir · cd · pwd
Document Operations cat · cp · dd · much less · ls · mkdir · mv · tail · tar · zip
Document Machine Operations chown · mkfs
Networking ping · curl · wget · iptables
Seek and Textual content Processing to find · grep · sed · whatis
Machine Data and Control env · historical past · best · who
Person and Consultation Control display · su · sudo

The publish How you can Use Historical past Command in Linux seemed first on Hongkiat.

WordPress Website Development Source: https://www.hongkiat.com/blog/linux-command-history/

[ continue ]