Linux search in terminal history

Is there any way to execute commands from history?

I’m just wondering if there any way to execute the 636 command without typing it again, just type something plus the number, like history 636 or something.

5 Answers 5

In bash, just !636 will be ok.

In Linux — This has more to do with the shell (bash, zsh, etc) a person is running then the OS you are running it on. There are shells on linux without support for history.

If you’ve got a command that’s very similar to what you want to run, you can append :p to print the command without running it, !636:p for example. Which means that you can easily select it again from your history by pressing the up arrow and make the minor changes you want

Yes, it’s called «history expansion.» See

LESS='+/^HISTORY EXPANSION' man bash 

Using an exclamation point followed by a number is arguably the simplest use:

However, you can also run the last executed command directly without knowing its history number:

Or you can run two commands back:

The form I most often use, however, is to repeat the last argument of the last command:

echo this text goes into a file > /tmp/afile.txt cat !$ 

Or, let’s say I’m inspecting a file. Using tab completion, I might do the following:

ls -l /really/long/path/to/some/file less !$ 

Again, read the man page for more details:

LESS='+/^HISTORY EXPANSION' man bash 

Interactively, Alt-. can be more convenient than !$ . It inserts the last argument of the last command verbatim into the line.

@ulidtko Where are those special escapes documented (I knew about !$ , but never heard about Alt-. )? Will you post a link to it, please?

LESS=»+/DEFAULT KEY BINDINGS» man 3 readline , /etc/inputrc , ~/.inputrc . Many programs besides bash use libreadline; for those who do, you get the same features like Alt-. , Ctrl-X-U (undo), Ctrl-/ (describe shortcut), tab-completion, etc — for free. For example, with just three lines of PYTHONSTARTUP one can enable tab-completion and history in the stock python REPL. Which will work (mostly) the same as in bash, since it’s the GNU Readline library that does the work. The readline’s manpage describes the features in detail.

Also, Ctl-UpArrow (or AltUpArrow on bash) will scroll backwards through the history list. Typing a few characters then the UpArrow gets only the commands starting with that letter, e.g. «fr(Ctl-Up)» scrolls through all the «free. » commands in the OP’s list. This also works in things like gnuplot & gdb that use readline.

@Ajedi32, see the final bolded line in my answer. 🙂 Truthfully this answer is barely a taste of what history expansion can do. Keep in mind that this is at the level of the shell, not the command you are calling; hence echo followed by touch !$ will touch five files, not just one. So strictly speaking it’s not the last argument but the last shell word on that line. (Also try echo hello >temp followed by echo goodbye !$ .) If you can think of a way to put it more clearly feel free to suggest an edit.

Читайте также:  Linux rename files by pattern

A nice one, if you don’t want to first history, note the number, etc:

( ctrl-r = «reverse search interactively») ( something = a part of a previous command)

This will show you the latest history line containing something . To get the one before, do ctrl-r (alone) again, each time it gets a previous line.

to show the latest history line containing ’10’ (for exemple the line with $10 , in your exemple), and ctrl-r again until you retrieve the history line you were looking for

When the line you wanted appear, just Enter to run it (or you can also edit it, using the arrow keys , backspace , and ctrl-a to go to the beginning of the line, ctrl-e to get to the End, ctrl-k : to «kill» from cursor to end of line (=deletes+saves it in a buffer), ctrl-y : to restore, from the buffer, a previously killed bit, etc.)

If you want to search forward (ctrl-s), you first need to disable XON : see https://stackoverflow.com/a/791800/1841533 :

» just to disable XON/XOFF by running

» (and then you will be able to use ctrl-s, without having it freeze the terminal)

Источник

Using grep and history command to find previous commands in Linux

Use of history command

You input command in your linux terminal which is very long and hard to remember. It is needed to input the command again to the terminal. You find it hard to search for the command that is needed. You keep pressing up arrow again and again until the required command is found. Time is wasted during this process. This is where history command comes in. Go to your linux terminal and type the command history. You will find the list of commands you previously used like this.
In the above image you can see all the previous commands you have used along with their respective numbers. Now let’s assume you have to use the command git clone https://github.com/asimdahall/wethanks/ which is number 56 . Now you can execute the command by typing

which will execute the command on number 56.
Now let’say we have to search the previous command that we have used. For that, we can combine grep with the history command. Let’s take a case here. I have to search for the command
xcrun simctl shutdown all

which I have used before but I don’t remember the full command and only remember xcrun . So for searching the command, we can combine the history and grep command like this:

asims-MacBook-Pro:~ asim$ history|grep xcrun 
 422 xcrun simctl shutdown all 452 xcrun simctl shutdown all 454 xcrun simctl shutdown all 502 history|grep xcrun 

you get all the commands where xcrun command is used with their respective numbers. Now you can execute the command like this:

which will then execute the needed command. I am sorry for the bad grammar. Feedbacks and suggestions are kindly welcome. Thank you. You can follow me on Twitter

Источник

search through the command history in the terminal [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

  • This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
  • This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Читайте также:  Linux authentication token lock busy

Have you ever used a command in the Terminal, but then later forgot what you typed , and wished there was some way that you could view the last used commands?

2 Answers 2

There are many ways to search through your command history. Here are 3:

  1. use the up and down arrows on your keyboard when at the command prompt.
  2. use the history command.
  3. Simply press : Control + R when at the command prompt.

E.g : Control + R , then write php

enter image description here

If you need to see all of the commands history just type : history , but if you forgot a command that you typed before use Control + R

Sure, but omitting history would make this answer incomplete. Generally speaking, this question is also off-topic for StackOverflow since it does not directly relate to programming.

History command is best option for review all previous commands, using of this command all previous commands are showing in one page, so that you can easy check logs of command and you can also use up and down arrow for review the last command studentthadi.com/news/post/history-command-in-linux/11

Источник

Finding an old command in the shell history [duplicate]

How can I find an old command I ran in my terminal? I used an appengine command and wish to just find it in my local command history without researching it online again.

Run history in terminal or if u remember some parts of the command history | grep , but note if you have made other commands and ure history settings in .bashrc is not large then you might never see it.

Not sure it’s worth a full answer. but you can also hit the ~/.bash_history file in your editor of choice ie: vim ~/.bash_history and search inside the file/editor.

3 Answers 3

History is what you are looking for.

Run history to get a list of the last 1000 commands, or run:

history | grep command_to_search 

to search some pattern within the history generated list, for example:

To search any apt related command, note that grep does not anchor your pattern to either end of the string, so no wildcards needed in most cases.

The history list is generated from the last 1000 commands (by default) stored in ~/.bash_history (which stores the last 2000 by default), and such file gets only updated whenever you exit your Bash session cleanly. That means, all commands from your current session will not be in that file until you close your terminal. They will also not be written if you kill the shell process.

Therefore, the command history (2000 last commands) can also be found at ~/.bash_history , by either:

  • less ~/.bash_history or, e.g. gedit ~/.bash_history
  • Opening Nautilus, Ctrl + h , and searching for the .bash_history file.
Читайте также:  Linux disk space utility

Both history and ~/.bash_history behavior can be changed by adding or modifying the options in ~/.bashrc , these are a few examples:

  • Append export HISTCONTROL=ignoreboth:erasedups or modify the existing HISTCONTROL line, to remove duplicate commands, i.e. if one runs echo Hello immediately after echo Hello , only one will appear in bash_history .
  • Modify HISTSIZE=1000 to extend or reduce the amount of commands shown with history
  • Modify HISTFILESIZE=2000 to extend or reduce the amount of commands stored in bash_history . Set HISTFILESIZE=-1 for unlimited.
  • Append HISTFILE=/path/to/file to save the history somewhere else.

Sources and further reading:

Источник

How can I search the bash history and rerun a command?

Note that while Ctrl R is the default, if you wanted the command ( reverse-search-history ) to be bound to Ctrl T you could configure that with the following:

bind '"\C-t": reverse-search-history'

There are a whole host of other readline bindable commands that are available to you as well. Take a look at the bash man page.

Bash has many facilities to search and access interactive command history. The most basic of which is the history builtin. Typing just:

Will print a list of commands along with a numeric index, like:

$ history 1 clear 2 ls -al 3 vim ~/somefile.txt 4 history $

You can then execute any of these commands using their numeric index by prefacing the index with a single ! , as Mitch pointed out:

Will execute the clear command. The history builtin has many features itself, you can see more in the bash and history man pages.

You can also specify relative negative offsets when using the ! designator, so using our history list above, if we wanted to execute vim again, we could do:

Which is basically telling bash to execute the command you ran «two commands ago.» To run the previous command in the history list, we can just use !! (which is just shorthand for !-1 ).

The ! designator doesn’t limit you to numerically specifying which command to run. hayalci showed that you can instruct bash to execute a command based on either the text it begins with (using ! ) or text within the command itself (using !? ). Again, using our example history list above, if we wanted to execute clear again, all we need to do is type:

and press Enter . And what about vim ? That is as simple as:

The most important point from hayalci’s response is the call to the shopt builtin:

This will enable history verification so that commands that are matched by the ! , !! , and !? designators are not blindly executed, but instead filled in on the command line so you can ensure they will do no evil before executing them. This is even more important when you are executing commands as the root user. This option can be set in your .bashrc startup file so that it is set whenever you log in.

As has already been pointed out, all of this information can be gleaned from the bash man page. For the !, . and !? designators, take a look at Section 9.3 History Expansion.

Источник

Оцените статью
Adblock
detector