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
cat ~/.bash_history
less ~/.bash_history
...any other pager or output command...
Or you can use bash's builtin command:
history
To clear the history, delete the file and clear the temp history:
rm ~/.bash_history && history -c
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:
HISTSIZE=<number of entries, -1 for unlimited>
This will not take effect immediately, but only to newly started sessions. To apply this, re-source the .bashrc file:
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):
history 5
If do you want to view only commands containing a string (i.e. mv), you can do this:
history | grep mv
You can recall a command by typing ! followed by the entry number.
Let's say that I have a history like this:
1 ls -la
2 mkdir foo
3 mv bar.txt foo
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^.
I often just want those recent commands, too. To post to my development notes or, well, stackexchange sites like these... This has proven very useful, removing irrelevant line numbers:
history | cut -c 8- | tail
or, if you like it as an alias, line numbers removed, and indented right away (just as needed for code quote markdown)
alias lastones="history | tail | sed -e 's/^ [0-9]\{1,5\} / /gi'"
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 ofOr 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 theHISTSIZE
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:
mkdir foo
, you can type!2
.!-1
or!!
!-2
If you run a command that fails because it needs root privileges (i.e.
touch /etc/foo
), you can usesudo !!
to run the last command as root.!man
you will execute the last command that begins withman
!?man?
it will execute the last command that containsman
(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^
.Source: https://www.digitalocean.com/community/tutorials/how-to-use-bash-history-commands-and-expansions-on-a-linux-vps
Just type :
A new file called print.txt will be created in your currently working directory.
I often just want those recent commands, too. To post to my development notes or, well, stackexchange sites like these... This has proven very useful, removing irrelevant line numbers:
or, if you like it as an alias, line numbers removed, and indented right away (just as needed for code quote markdown)
You may want to try https://github.com/dvorka/hstr which allows simple browsing, navigation and "suggest box style" filtering of your Bash history:
It can be easily bound to Ctrl-r and/or Ctrl-s