Previous command in linux

Using grep and history command to find previous commands in Linux

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

Exit fullscreen mode

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:

Exit fullscreen mode

after that you will get the following output

Exit fullscreen mode

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

Exit fullscreen mode

which will then execute the needed command.

I am sorry for the bad grammar. Feedbacks and suggestions are kindly welcome. Thank you.

Источник

Different Ways To Repeat Your Last Command In Linux

Today, I will be teaching you how to repeat your last command in Linux. You don’t have to re-type the previously executed commands in your Shell. Of course, we use the UP arrow key to select the last executed commands and hit ENTER to execute them. However, there are also other a few ways to do this. There could be many ways to repeat the last commands in Unix-like systems, but these are the only ways I am aware of now. All these commands were tested in my Arch Linux and Ubuntu desktop with BASH shell.

A word of caution: When you try the following methods, the previously executed commands will run immediately. Just make sure your last command wasn’t any harmful like formatting a partition or deleting files, folders or any other important data.

Repeat last command in Linux

Let us run a few commands.

As I mentioned earlier, we can execute the last command by simply pressing the UP arrow and hit ENTER key. This is the most commonly used way by many users to execute the previous command. This method will work on all SHELL, regardless of the Linux distribution you use.

However, like I said already, there are a few other methods to do this.

Now, let me show you how to execute the last command with some practical examples.

Method 1 — Using exclamation marks

To execute any last executed command, just type double exclamation marks, and hit ENTER:

This will execute the last command. Sample output would be:

Add sudo in-front of !! to execute the last command as root user like below.

You can also use the following to execute the previous command as sudo user:

Читайте также:  Восстановление ntuser dat windows 10

Sample output:

Cool, isn’t it? You don’t need to type the last command completely. It could be useful when you’ve already executed a very long command, and don’t want to re-type the same command again.

Method 2 — Repeat N-th command

You might have run so many commands, and want to repeat a specific command. What will you do? Simple! You can do this by typing a specific word of the previous command.

To repeat previous command that starts with a specific word/letter, for example un , just type:

The above command will execute last command that starts with the letters «un».

Sample output:

As you see in the above example, you don’t need to type the whole command (i.e uname -r ). Instead, just type a few letters of the command, and any previous command that contains words will run.

Also, If you know the full command name, just type it like below:

It will execute the last command.

What if you don’t want to repeat the last command, but just display it. Sometimes, you don’t want to run a command, but just retrieve it from the history. If so, find the prefix number of the command you want to run:

Sample output:

Let us say you want to retrieve the 1685th command, but don’t want to run it immediately, add :p next to the command like below.

This will display 1685th command from the history, but won’t execute it.

Another way to do this is by searching your command line history using CTRL+R . Press CTRL+R key to search through the command line history. I personally prefer this method. It searches history interactively which I think feels safer than executing commands blindly from the BASH history.

Have a look at the following example. In the following example, I searched for «ost» , which displayed the last command “sudo netctl restart ostechnixjio” in the history that contained the word «ost» . Then, I hit ENTER to execute the command immediately or right arrow key complete the command and hit ENTER key to execute it.

Just in case, you want to edit/modify the last command before executing it, just press the left arrow key, then edit the last command and hit ENTER to execute it. Some commands may start with same letters. In such cases, keep hitting the CTRL+R key to bring back the command you wanted to run.

Method 4 — Use hyphen symbol with command prefix numbers

Here is yet another way to run the previous command.

Sample output:

Similarly, !-2 will run the second last command, !-3 will run the third last command, and so on.

Method 5 — Using CTRL+p and CTRL+o

I don’t want to type any command manually. Is there any way to repeat a last command by simply pressing a particular key(s) in my Keyboard? Yes, of course!

Press CTRL+P to switch to the last command, and then press CTRL+O to execute it. This will do the wonder. No configuration needed! You can use CTRL+O as many times as you want to keep re-executing the last commands.

Method 6 — Using fc command

This is another way to repeat the last executed command. The fc command is used to list, edit and re-run the previously entered command.

To repeat the last command using fc, simply run:

Sample output:

Suggested read:

Method 7 — Using ALT+period key

As one of our reader said in the comment section, we can also use ALT+. keys (press ALT+period) to retrieve the last command without running it. However, there is one limitation. Hitting ALT+. will only retrieve the last argument in the command. For example, if your previous command is «uname -r» , it will only bring back the -r argument, but not the entire command.

And, that’s all. You know now how to repeat your last command without typing it in the Terminal. If you want to view the output of your last commands without actually having to retype them, these methods will help.

If you know any other methods, please let me know in the comment section below. I will check and update the guide accordingly.

Читайте также:  Удалить windows с навигатора

You knew now how to repeat a previously executed command. How do you repeat a Linux command or program until it succeeds? Well, that’s easy! Refer the following guide for more details.

Источник

View history of commands run in terminal

Is there a way to save all my typed terminal commands and view it like history in a log book?

5 Answers 5

This is automatically done. Bash stores your commands in

/.bash_history . If you want to have a look at the history, either print the output of this file using one of

Or you can use bash’s builtin command:

To clear the history, delete the file and clear the temp history:

The history size defaults to 500 commands. You can, however, increase this by adding a line to your

/.bashrc file to set the HISTSIZE variable:

This will not take effect immediately, but only to newly started sessions. To apply this, re-source the .bashrc file:

or run HISTSIZE=. in your current session.

You can type history on a terminal to view all the previous executed commands.

You can truncate the output to some lines (where 5 is the number of lines):

If do you want to view only commands containing a string (i.e. mv ), you can do this:

You can recall a command by typing ! followed by the entry number.

Let’s say that I have a history like this:

  • To run mkdir foo , you can type !2 .
  • To run the last command, you can use !-1 or !!
  • To run the penultimate, you can use !-2

If you run a command that fails because it needs root privileges (i.e. touch /etc/foo ), you can use sudo !! to run the last command as root.

  • If you type !man you will execute the last command that begins with man
  • If do you type !?man? it will execute the last command that contains man (not neccessarily at the line begin)

If do you have a typo in a command, you can fix it this way. Let’s say that I type cat .bash_hi , to replace .bash_hi by .bash_history I only need to type ^hi^history^ .

Источник

Using bash history to get a previous command, copy it and then ‘run’ it but with the command commented

Just a question to improve my bash skills. I always do this:

I can then run the command the command I found by doing:

However, I often want to do this:

I.e. change the command before I run it. Can you use bash to run this command instead:

so it gets commented.

Then I don’t have to use my mouse to highlight the command, edit it and then run it (I can just use the keyboard — faster).

I suppose I could write a script to do it but there might already be functionality built in somewhere.

10 Answers 10

I’d suggest instead of using the history command, you use ctrl+r and start typing that command. When you press an arrow key as if to go to modify it, it will drop out of autocomplete recognition, and will let you edit before running.

UPDATE: also, if you want to cycle through the different commands that contain the string you just typed, keep on pressing ctrl+r

Actually, you can just append :p to the command to print it without actually running it. For example:

Will print out ls -la as the previous command without running it, and you can just press ↑ (up) to find it and edit it.

You can also do

to print out the 123rd command as your previous command.

You can also try fc command to edit the command in the history.

​fc​ is a standard program on Unix that lists or edits and reexecutes, commands previously entered to an interactive shell. fc is a built-in command in the bash shell; help fc will show usage information.

Apart from reverse-incremental search( Ctrl + R ), we have some more bash shortcuts:

Will run command 123 replacing the string ‘old’ with the string ‘new’.

You can get to edit mode by hitting M-^ (option-shift-6 on a mac).

And you’ll be editing command #123. It’s sort of like using ctrl-r, but starting with exclamation-point syntax.

Читайте также:  Удалить второй windows входе

Instead of using the history command, bind history-search-backward / history-search-forward to key shortcuts which can be remembered easily (I prefer PgUp/PgDown). To do that, put this into your .inputrc file:

To get , type Ctrl-V in the shell, and replace the starting ^[ with \e in whatever was output.

After this is set up, you can just type some and press PgUp to get some_long_command . If you need some_long_command with_some_arg but there is a similar command some_long_command with_some_other_arg later in the history, you can cycle through until you reach it by typing some and then hitting PgUp repeatedly, or you can type some , hit PgUp, move the cursor to where the two commands start to differ, type a few characters and hit PgUp once more. This ability to quickly page through / differentiate between similar commands makes it in my opinion a much more comfortable tool than Ctrl-R .

Источник

How do I search my command-line history for commands I used before?

Other than viewing the history, is there a way to filter my history?

Say I want to search for a command that started with «ssh»?

11 Answers 11

Press Ctrl + R and type ssh . Ctrl + R will start search from most recent command to old one (reverse-search). If you have more than one command which starts with ssh , Press Ctrl + R again and again until you find the match.

Once you’ve found the match you can press Enter to execute the command or left / right cursor to just select the text of the command.

There is no default reverse option for Ctrl + R to invert the direction of the search but here you will find some suggestions about it.

If you just want to search your history, you can just use history | grep ssh , substituting ssh for whatever you want to search.

I do a slight variation of the above, works well for me (if you’re referring to your bash history

In my home folder I create a file named

Inside goes this

Note: the above doesn’t seem to work anymore in 14.04 so this does the same thing —

Then typing however much of a previous command I wish & using the page up/page dn buttons searchs the history, always starting with page up

Pressing Ctrl + R will start «reverse-i-search» mode, typing «ssh» would search your history for commands which contain «ssh».

Here’s another method using classic commands (more likely to work across distros). Command history is stored in the file .bash_history in your home directory, so you can do this:

Don’t forget the -i flag if you need case insensitive search.

I found the following function somewhere on the Internet and have used it to great effect. Put this in your

Now re-load your shell: exec bash . You now have a new command, which you can use like so:

It will show you a list of matching commands from your history. To run a command, type ! followed by the command number. Here’s an example:

I like this approach better than Ctrl + R because it allows much more flexible searches, and I can see multiple results at once.

Put this in your

History is good but limited — I prefer to set up my bash environment so that I log all of the commands I have ever run, in addition to the directory in which they were run in. Then I run a command to list all of the commands I have run in the current directory, which I can pipe to grep etc — it’s called ‘dish’ see:

If you want to cut to the chase, just source this in your .bashrc (https://github.com/wolfwoolford/dish/blob/master/dishrc)

There’s also a really useful command you get for free called ‘dishg’ — or dish global — which prints out every command ever run, regardless of directory. Obviously this one is only useful when used with grep and tail etc.

I’ve been using it for years and it’s literally the first thing I install whenever I setup a new box. It logs the commands you run into text files in a hidden directory (

/.dish) .. I’ve never had an issue with disk space.

Источник

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