Linux search commands in history

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.

Читайте также:  Can restart network service linux

Источник

Searching command history

I press Ctrl r and then I type «this is an» and the old command commes up but not the older. How can I search all the «this is an» commands? Is it possible to pipe all similar commands to grep or something? If I set -o vi , how do I undo it?

to undo vi-style line editing just set -o emacs since there only exists emacs and vi line editing mode.

In addition to other answers you can also search for a condition and view only 10 results history | grep | head head command will give you last 10 results.

2 Answers 2

To search for a command in the history press ctrl+r multiple times 😉

You can also grep through the history using:

 history | grep YOUR_STRING 

If I understand correctly and you want to search for older entries, just press ctrl+r again.

You must log in to answer this question.

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.17.43537

Linux is a registered trademark of Linus Torvalds. UNIX is a registered trademark of The Open Group.
This site is not affiliated with Linus Torvalds or The Open Group in any way.

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

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.

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

Читайте также:  Linux fatal unable to access

Источник

The Power of Linux “History Command” in Bash Shell

We use history command frequently in our daily routine jobs to check history of command or to get info about command executed by user. In this post, we will see how we can use history command effectively to extract the command which was executed by users in Bash shell. This may be useful for audit purpose or to find out what command is executed at what date and time.

By default date and timestamp won’t be seen while executing history command. However, bash shell provides CLI tools for editing user’s command history. Let’s see some handy tips and tricks and power of history command.

linux history command

1. List Last/All Executed Commands in Linux

Executing simple history command from terminal will show you a complete list of last executed commands with line numbers.

[[email protected] ~]$ history 1 PS1='\e[1;35m[\[email protected]\h \w]$ \e[m ' 2 PS1 /cdn-cgi/l/email-protection" data-cfemail="423702">[email protected]\h \W]$ \e[m " 3 PS1 /cdn-cgi/l/email-protection" data-cfemail="e396a3">[email protected]\h:\w [\j]$ " 4 ping google.com 5 echo $PS1 6 tail -f /var/log/messages 7 tail -f /var/log/messages 8 exit 9 clear 10 history 11 clear 12 history

2. List All Commands with Date and Timestamp

How to find date and timestamp against command? With ‘export’ command with variable will display history command with corresponding timestamp when the command was executed.

[[email protected] ~]$ export HISTTIMEFORMAT='%F %T ' 1 2013-06-09 10:40:12 cat /etc/issue 2 2013-06-09 10:40:12 clear 3 2013-06-09 10:40:12 find /etc -name *.conf 4 2013-06-09 10:40:12 clear 5 2013-06-09 10:40:12 history 6 2013-06-09 10:40:12 PS1='\e[1;35m[\[email protected]\h \w]$ \e[m ' 7 2013-06-09 10:40:12 PS1 /cdn-cgi/l/email-protection" data-cfemail="90e5d0">[email protected]\h \W]$ \e[m " 8 2013-06-09 10:40:12 PS1 /cdn-cgi/l/email-protection" data-cfemail="cdb88d">[email protected]\h:\w [\j]$ " 9 2013-06-09 10:40:12 ping google.com 10 2013-06-09 10:40:12 echo $PS1
Meaning of HISTTIMEFORMAT variables
%F Equivalent to %Y - %m - %d %T Replaced by the time ( %H : %M : %S )

3. Filter Commands in History

As we can see same command is being repeated number of times in above output. How to filter simple or non destructive commands in history?. Use the following ‘export‘ command by specifying command in HISTIGNORE=’ls -l:pwd:date:’ will not saved by system and not be shown in history command.

[[email protected] ~]$ export HISTIGNORE='ls -l:pwd:date:'

4. Ignore Duplicate Commands in History

With the below command will help us to ignore duplicate commands entry made by user. Only single entry will be shown in history, if a user execute a same command multiple times in a Bash Prompt.

[[email protected] ~]$ export HISTCONTROL=ignoredups

5. Unset export Command

Unset export command on the fly. Execute unset export command with variable one by one whatever commands have been exported by export command.

6. Save export Command Permanently

Make an entry as follows in .bash_profile to save export command permanently.

[[email protected] ~]$ vi .bash_profile # .bash_profile # Get the aliases and functions if [ -f ~/.bashrc ]; then . ~/.bashrc fi # User specific environment and startup programs export HISTCONTROL=ignoredups PATH=$PATH:$HOME/bin export PATH

7. List Specific User’s Executed Commands

How to see command history executed by a specific user. Bash keeps records of history in a ‘~/.bash_history’ file. We can view or open file to see the command history.

[[email protected] ~]$ vi .bash_history cd /tmp/ cd logstalgia-1.0.3/ ./configure sudo passwd root apt-get install libsdl1.2-dev libsdl-image1.2-dev libpcre3-dev libftgl-dev libpng12-dev libjpeg62-dev make gcc ./configure make apt-get install libsdl1.2-dev libsdl-image1.2-dev libpcre3-dev libftgl-dev libpng12-dev libjpeg62-dev make gcc++ apt-get install libsdl1.2-dev libsdl-image1.2-dev libpcre3-dev libftgl-dev libpng12-dev libjpeg62-dev make gcc apt-get install make mysql -u root -p apt-get install grsync apt-get install unison unison

8. Disable Storing History of Commands

Some organization do not keep history of commands because of security policy of the organization. In this case, we can edit .bash_profile file (It’s hidden file) of user’s and make an entry as below.

[[email protected] ~]$ vi .bash_profile # .bash_profile # Get the aliases and functions if [ -f ~/.bashrc ]; then . ~/.bashrc fi # User specific environment and startup programs PATH=$PATH:$HOME/bin HISTSIZE=0 export PATH .bash_profile (END)

Save file and load changes with below command.

Читайте также:  Авто монтирование диск linux

Note: If you don’t want system to remember the commands that you have typed, simply execute below command which will disable or stop recording history on the fly.

Tips: Search ‘HISTSIZE‘ and edit in ‘/etc/profile’ file with superuser. The change in file will effect globally.

9. Delete or Clear History of Commands

With up and down arrow, we can see previously used command which may be helpful or may irate you. Deleting or clearing all the entries from bash history list with ‘-c‘ options.

10. Search Commands in History Using Grep Command

Search command through ‘.bash_history‘ by piping your history file into ‘grep‘ as below. For example, the below command will search and find ‘pwd‘ command from the history list.

[[email protected] ~]$ history | grep pwd 113 2013-06-09 10:40:12 pwd 141 2013-06-09 10:40:12 pwd 198 2013-06-09 15:46:23 history | grep pwd 202 2013-06-09 15:47:39 history | grep pwd

11. Search Lastly Executed Command

Search previously executed command with ‘Ctrl+r’ command. Once you’ve found the command you’re looking for, press ‘Enter‘ to execute the same else press ‘esc‘ to cancel it.

(reverse-i-search)`source ': source .bash_profile

12. Recall Last Executed Command

Recall a previously used specific command. Combination of Bang and 8 (!8) command will recall number 8 command which you have executed.

13. Recall Lastly Executed Specific Command

Recall previously used command (netstat -np | grep 22) with ‘!‘ and followed by some letters of that particular command.

[[email protected] ~]$ !net netstat -np | grep 22 (No info could be read for "-p": geteuid()=501 but you should be root.) tcp 0 68 192.168.50.2:22 192.168.50.1:1857 ESTABLISHED - tcp 0 0 192.168.50.2:22 192.168.50.1:2516 ESTABLISHED - unix 2 [ ] DGRAM 12284 - @/org/freedesktop/hal/udev_event unix 3 [ ] STREAM CONNECTED 14522 - unix 2 [ ] DGRAM 13622 - unix 3 [ ] STREAM CONNECTED 12250 - @/var/run/hald/dbus-ujAjOMNa0g unix 3 [ ] STREAM CONNECTED 12249 - unix 3 [ ] STREAM CONNECTED 12228 - /var/run/dbus/system_bus_socket unix 3 [ ] STREAM CONNECTED 12227 -

We have tried to highlight power of history command. However, this is not end of it. Please share your experience of history command with us through our comment box below.

Источник

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