what command allows you to see commands that you previously typed in?

A terminal prompt on a Linux laptop computer.
Fatmawati Achmad Zaenuri/Shutterstock

Linux's shell saves a history of the commands yous run, and y'all can search information technology to repeat commands you've run in the past. Once you understand the Linux history command and how to utilise it, it can significantly heave your productivity.

Manipulating History

As George Santayana famously said, "Those who cannot remember the past are condemned to echo information technology." Unfortunately, on Linux, if you tin't remember the past, you can't repeat it, fifty-fifty if you lot desire to.

That's when the Linux history command comes in handy. Information technology allows you to review and repeat your previous commands. This isn't intended to just encourage laziness or save time—there's besides an efficiency (and accuracy) cistron at play. The lengthier and more complicated a command is, the harder information technology is to remember and type without making an error. There are two types of errors: one that prevents the command from working, and one that allows the control to work, simply makes it do something unexpected.

Thehistory command eliminates those issues. Like nearly Linux commands, at that place'due south more to it than yous might retrieve. However, if yous acquire how to use the history command, it can improve your employ of the Linux command line, every unmarried mean solar day. It's a good investment of your time. In that location are far meliorate ways to use the history command than just hitting the Up pointer repeatedly.

The history Command

In its simplest form, you can employ the history command by just typing its name:

history

The list of previously used commands is then written to the final window.

The commands are numbered, with the most recently used (those with the highest numbers) at the end of the listing.

To run into a certain number of commands, you can pass a number to history on the control line. For instance, to come across the last ten commands you've used, type the post-obit:

history x

Y'all can achieve the same result if you pipe history through the tail command. To do so, blazon the post-obit:

history | tail -n x

RELATED: How to Use Pipes on Linux

Repeating Commands

If you want to reuse a command from the history list, blazon an exclamation betoken (!), and the number of the command with no spaces in-between.

For example, to repeat command number 37, you would type this control:

!37

To repeat the final command, type two assertion points, again, without spaces:

!!

This tin be useful when yous issue a command and forget to utilize sudo . Type sudo, i infinite, the double exclamation points, and then hitting Enter.

For the following example, nosotros typed a command that requires sudo. Instead of retyping the whole line, we tin can salvage a agglomeration of keystrokes and just typesudo !!, as shown beneath:

mv ./my_script.sh /usr/local/bin/
sudo !!

So, you tin can blazon the respective number from the listing to echo a command or use the double exclamation points to repeat the last command you used. However, what if you desire to repeat the fifth or eighth command?

You can apply i assertion indicate, a hyphen (-), and the number of any previous control (again, without spaces) to repeat information technology.

To repeat the 13th previous command, you would type the following:

!-13

Searching for Commands by Cord

To repeat the concluding command that starts with a particular cord, you can type an exclamation point, so the string with no spaces, and and then hit Enter.

For example, to echo the last command that started with sudo, you would blazon this command:

!sudo

In that location's an chemical element of danger in this, though. If the concluding control that started with sudo isn't the ane you retrieve it is, you'll launch the wrong command.

To provide a prophylactic net, though, you can use the :p (print) modifier, as shown below:

!sudo:p

This instructs history to print the command to the terminal window, rather than executing it. This allows you to encounter the control earlier you lot apply it. If it is the command yous want, printing the Up arrow, and then hit Enter to utilise information technology.

If you desire to find a command that contains a particular string, yous can use an assertion point and question mark.

For example, to find and execute the first matching command that contains the give-and-take "aliases," you would type this command:

!?aliases

This will find whatsoever command that contains the string "aliases," regardless of where it appears in the string.

Interactive Search

An interactive search allows you to hop through a list of matching commands and repeat the one y'all want.

Just press Ctrl+r to start the search.

A terminal window after "Ctrl+r" is pressed.

As you blazon the search clue, the first matching command will appear. The letters yous blazon announced betwixt the backtick (`) and the apostrophe ('). The matching commands update as you type each alphabetic character.

A "Ctrl+r" search with "sudo" typed as the search clue.

Each fourth dimension you press Ctrl+r, you're searching backward for the adjacent matching command, which appears in the terminal window.

When you printing Enter, the displayed command will execute.

To edit a command earlier you execute it, press either the Left or Correct arrow fundamental.

The command appears on the command line, and y'all can edit it.

A command on the command line with editing enabled.

Yous tin use other Linux tools to search the history list. For instance, to pipage the output from history into grep and search for commands that comprise the string "aliases" you could apply this command:

history | grep aliases

Modifying the Last Command

If you need to fix a typo, then repeat the command, you tin use the caret (^) to change it. This a nifty trick to take up your sleeve for whenever you misspell a command or want to rerun one with a different command-line option or parameter.

To use it, blazon (without spaces) a caret, the text you desire to replace, another caret, the text you want to supplant information technology with, another caret, and then press Enter.

For case, suppose y'all type the following command, accidentally typing "shhd" instead of "sshd":

sudo systemctl start shhd

You could correct this easily by typing the following:

^shhd^sshd^

The command is executed with "shhd" corrected to "sshd."

Deleting Commands from the History Listing

Yous can besides delete commands from the history list with the -d (delete) option. There's no reason to go on your misspelled command in the history list.

You tin can use grep to find it, pass its number to history with the -d selection to delete it, so search once more to make sure information technology's gone:

history | grep shhd
history -d 83
history | grep shhd

You tin also laissez passer a range of commands to the -d option. To delete all list entries from 22 to 32 (inclusive), type this command:

history -d 22 32

To delete only the last five commands, yous can type a negative number, similar and then:

history -d -5

Manually Updating the History File

When you log in or open a concluding session, the history list is read in from the history file. In Bash, the default history file is .bash_history.

Any changes you make in your current concluding window session are simply written to the history file when you lot close the final window or log out.

Suppose you want to open up some other concluding window to admission the total history list, including commands you typed in the first last window. The -a (all) selection allows you to do this in the outset terminal window before y'all open the second.

To use information technology, blazon the post-obit:

history -a

The commands are written silently to the history file.

If you lot want to write all changes to the history list to the history file (if y'all deleted some old commands, for example), you can employ the -w (write) option, like so:

history -w

Clearing the History List

To clear all commands from the history list, yous can use the -c (clear) option, as follows:

history -c

If you additionally desire to strength these changes to the history file, use the -w pick, like so:

history -w

Security and the History File

If you utilize any applications that crave you to type sensitive information (like passwords) on the command line, remember this will also exist saved in the history file. If yous don't desire certain information saved, you tin use the post-obit control structure to delete information technology from the history list immediately:

special-app my-surreptitious-countersign;history -d $(history 1)
history 5

This construction includes ii commands separated with a semicolon (;). Allow'due south interruption this down:

  • special-app: The name of the program nosotros're using.
  • my-secret-password: The hugger-mugger countersign we need to provide for the application on the command line. This is the end of command one.
  • history -d: In command two, we invoke the -d (delete) option of history. What we're going to delete comes is in the side by side portion of the command.
  • $(history ane): This uses a command substitution.  The portion of the control contained in the$() is executed in a subshell. The effect of that execution posts as text in the original command. The history one command returns the previous command. So, you can think of the 2nd command as history -d "concluding command here."

You tin use the history 5 command to make sure the command containing the password was removed from the history list.

There's an even simpler way to do this, though. Because Bash ignores lines that begin with a infinite by default, simply include a space at the start of the line, as follows:

            special-app some other-password
history 5

The command with the password isn't added to the history list. The reason this trick works is contained within the .bashrc file.

The .bashrc File

The.bashrc file executes each fourth dimension you log in or open up a terminal window. It also contains some values that control the behavior of the history control. Let'southward edit this file withgedit.

Type the following:

gedit .bashrc

Most the top of the file, you see two entries:

  • HISTSIZE : The maximum number of entries the history listing tin contain.
  • HISTFILESIZE : The limit for the number of lines a history file can contain.

".bashrc" in the gedit editor.

These 2 values interact in the following ways:

  • When you log in or start a terminal window session, the history listing is populated from the .bash_history file.
  • When you close a terminal window, the maximum number of commands set in HISTSIZE are saved to the .bash_history file.
  • If the histappend trounce option is enabled, the commands are appended to .bash_history. If histappend isn't set, .bash_history is overwritten.
  • After saving the commands from the history list to .bash_history , the history file is truncated to comprise no more than HISTFILESIZE lines.

Also virtually the top of the file, you run into an entry for the HISTCONTROL value.

The "HISTCONTROL" entry in the ".bashrc" file in gedit.

Yous tin set this value to do any of the post-obit:

  • ignorespaces:Lines that brainstorm with a space aren't added to the history list.
  • ignoredups:Duplicate commands aren't added to the history file.
  • ignoreboth:Enables both of the to a higher place.

You tin can too listing specific commands y'all don't desire added to your history list. Separated these with a colon (:) and put them in quotation marks ("…").

You would follow this structure to add together a line to your.bashrc file, and substitute the commands yous want to be ignored:

consign HISTIGNORE="ls:history"

An "export HISTIGNORE="ls:history" command in gedit.

Using Timestamps

If you lot desire to add timestamps to the history list, yous can use the HISTIMEFORMAT setting. To practice so, you just add a line like the post-obit to your .bashrc file:

export HISTTIMEFORMAT="%c "

Notation that there's a infinite before the closing quotation marks. This prevents the timestamp from butting up to the commands in the command listing.

An "export HISTTIMEFORMAT="%c " command in gedit.

At present, when you run the history command, y'all see date- and timestamps. Annotation that any commands that were in the history list before y'all added the timestamps volition be timestamped with the date and fourth dimension of the first command that receives a timestamp. In this example shown below, this was command 118.

That's a very long-winded timestamp. Nevertheless, you can employ tokens other than%c to refine information technology. The other tokens you lot can use are:

  • %d: Day
  • %m: Month
  • %y: Year
  • %H: Hour
  • %1000: Minutes
  • %Due south: Seconds
  • %F: Total engagement (year-calendar month-date format)
  • %T: Time (hour:minutes:seconds format)
  • %c : Complete engagement and time stamp (mean solar day-date-calendar month-twelvemonth, and hour:minutes:seconds formats)

Let's experiment and use a few unlike tokens:

export HISTTIMEFORMAT="%d n%m %T "

The export HISTTIMEFORMAT="%d n%m %T " command in gedit.

The output uses the day, calendar month, and fourth dimension.

If we remove the twenty-four hour period and month, though, it volition simply show the time.

Any changes yous make to HISTIMEFORMAT use themselves to the entire history listing. This is possible because the fourth dimension for each command is stored as the number of seconds from the Unix epoch. The HISTTIMEFORMATdirective just specifies the format used to render that number of seconds into a human-readable style, such as:

export HISTTIMEFORMAT="%T "

The "export HISTTIMEFORMAT="%T" command in gedit.

Our output is now more than manageable.

You can also use the history control to audit. Sometimes, reviewing commands y'all've used in the past tin help you identify what might have caused an outcome.

Just as y'all can in life, on Linux, yous can use thehistory control to relive the practiced times and learn from the bad.

RELATED: 37 Important Linux Commands You Should Know

davisalose1999.blogspot.com

Source: https://www.howtogeek.com/465243/how-to-use-the-history-command-on-linux/

0 Response to "what command allows you to see commands that you previously typed in?"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel