Find out what command I last ran that starts a certain way?
Solution 1
If you search history via Ctrl+r and typing some letters of the command and not pressing Enter but pressing →, the command will show up and will not run. Another alternative is:
history | grep 'mycommand'
This nice history cheatsheet might also help.
Solution 2
You can show last command run, by append :p
to !!
:
$ echo 123
123
$ !!:p
echo 123
If you want show last command run with certain name, use one exclamation:
$ echo 123
123
$ pwd
/home/cuonglm
$ !echo:p
echo 123
Solution 3
Every answer here, as far as I can tell, is non-portable. For a portable option, consider the POSIX-guaranteed fc
:
$ man fc
−e editor Use the editor named by editor to edit the commands. The editor string is a utility name, subject to search via the PATH variable ... The value in the FCEDIT variable shall be used as a default when −e is not specified. If FCEDIT is null or unset, ed shall be used as the editor.
−l (The letter ell.) List the commands rather than invoking an editor on them. The commands shall be written in the sequence indicated by the first and last operands, as affected by −r, with each command preceded by the command number.
−n Suppress command numbers when listing with −l.
−r Reverse the order of the commands listed (with −l) or edited (with neither −l nor −s).
−s Re-execute the command without invoking an editor.
OPERANDS
first, last Select the commands to list or edit. The number of previous commands that can be accessed shall be determined by the value of the HISTSIZE variable. The value of first or last or both shall be one of the following:
[+or-]number A positive (or negative) number representing a command number; command numbers can be displayed with the −l option... For example, −1 is the immediately previous command...
When the −l option is used to list commands, the format of each command in the list shall be as follows:
"%d\t%s\n", <line number>, <command>
If both the −l and −n options are specified, the format of each command shall be:
"\t%s\n", <command>
If the consists of more than one line, the lines after the first shall be displayed as:
"\t%s\n", <continued-command>
But be sure to use -l or -e if you ONLY want to see/edit your commands. By default fc
will open the command-list requested in FCEDIT (note that is distinct from your EDITOR environment variable) and, when FCEDIT quits, fc
will run the edited commands.
Anyway, specifically the answer to this question could be:
% fc -l -1
Or with no line numbers:
% fc -ln -1
Or the last five commands in reverse order:
% fc -lrn -1 -5
In your pager:
% fc -lrn -1 -5 |$PAGER
For your last call to fc
:
% fc -l fc
Solution 4
hit Ctrlr then start typing the command you want to see. bash will incrementally complete. When you are satisfied, hit Enter to re-run it or Ctrlg to abort.
Solution 5
If you just want the last command use:
history | cut -c 8- | tail -n 2 | head -n 1
If you want to be able to do that repeatedly, put a space before history
or
use:
history | cut -c 8- | grep -Ev '^history' | tail -n 1
If you just want to see the last command starting with pattern xyz
:
history | cut -c 8- | grep -E '^xyz' | tail -n 1
But this doesn't work if the pattern would be the initial characters of 'history' and then you would need to something like:
history | cut -c 8- | grep -Ev 'history \|' | grep -E '^hist' | tail -n 1
Related videos on Youtube
bernie2436
Updated on September 18, 2022Comments
-
bernie2436 over 1 year
On bash commandline environment, you can do
!!
to rerun your last command. And you can press up arrow to see the the last command you ran. But what if you want to only see but not run the last command you ran that began in a certain way?-
Admin about 10 yearsCould you please modify your question. I is still not clear what you are asking.
-
-
orion about 10 yearsAnd if you are not looking for the first match and want to keep searching backwards, press ctrl-r again and again. And this is only one of many powerful commands provided by bash history & readline library.
-
jon about 10 yearsThere's a typo in your first use of
cut
. -
Admin about 10 yearsConsidering you have the verified answer, I'll pull mine and if you want to use the
"^.\{7\}pattern"
expression in your answer, you're welcome to do so! -
grebneke about 10 years+1 for effort. However, the question is tagged bash
-
mikeserv about 10 yearsIt works in
bash
, and everything else. The use ofbash
doesn't preclude portability, but certainly developing bad habits will. -
wildeyes over 8 yearsBest answer here and should be the accepted one, on account of the portability.