LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to recall an old command at the command prompt? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-recall-an-old-command-at-the-command-prompt-47178/)

J_Szucs 02-25-2003 11:30 PM

How to recall an old command at the command prompt?
 
I know how to step through the command history at the command prompt, but is it also possible to search for and recall a command that was issued a long time before (supposed that it is still in the history)?
From time to time I wish there was one, but I could not find the clue in the bash manual.
Thanks in advance.

Crashed_Again 02-25-2003 11:38 PM

Yes there is:

history | more

This will spit back the last 1000 commands you have issued. Well for bash it is 1000 by default but you can change it. Each command will have a number before it. Heres an example of one of the things you will get back from the history command:

1057 mount -t iso9660 /dev/scd0 /mnt/cdrom

Now if you want to run that command again just do:

!1057

A very cool feature I think.

rickenbacherus 03-06-2003 09:43 AM

Right on Crashed_Again - I gotta write that one down somewhere.

wapcaplet 03-06-2003 10:32 AM

Definitely, I'm a big fan of the shebang-history command. Also, if you have a terminal in which the up-arrow doesn't recall your last command, hitting !! repeats the last command.

Good for compile-edit cycles, too:

$ vi prog.c
$ gcc prog.c
(errors)
$ !v
(edit in vi, then exit)
$ !g
(more errors)

:D

boreo 03-06-2003 03:29 PM

Or if your looking for commands that contains exact words your would do this

history |grep mount

And all the commands with the word "mount" will show up instead of the whole history.
You will get something like this.

370 mount /mnt/cdrom1
371 umount /mnt/cdrom1
372 umount /mnt/cdrom
385 umount /mnt/cdrom1
386 umount /mnt/cdrom
425 umount /mnt/cdrom
516 history |grep mount

MasterC 03-06-2003 03:32 PM

I learn something new everyday... literally. Thanks everyone. :)

Cool

cuckoopint 03-06-2003 10:24 PM

you could also use the bang command as a search/recall automatically....instead of grepping history, if you know you want the last mount command (which may have been a long time ago)

!mount

will do.

Texicle 03-06-2003 10:39 PM

That is too cool. Thanks everyone! :D With stuff like that, how can you not love Linux?

Crashed_Again 03-06-2003 10:40 PM

How can you not love the BASH shell.

orange400 03-07-2003 12:29 AM

I use it half the time, I gotta say it's the most useful Linuc prog I use besides Mozilla.


All times are GMT -5. The time now is 05:42 AM.