How to find history of shell commands since machine was created?
Solution 1
It may or may not be possible to get all commands. It depends on how many commands you executed and how the history limit was set.
Nevertheless, you can see the history list which is stored here:
/home/$USER/.bash_history
where $USER
is your username.
Here is a related question (for handling multiple shells history): Is it possible to make writing to .bash_history immediate?
Solution 2
By default, there is no place where all commands are recorded and kept indefinitely, although ~/.bash_history
contains the past few commands (if you use bash
, which is the default shell in Ubuntu).
If you want every command typed in bash
recorded forever, you have to set it up yourself. For example, you can put the following in your ~/.bashrc
file to log all the commands typed in a bash
shell to the file ~/.command_log
:
# log every command typed and when
if [ -n "${BASH_VERSION}" ]; then
trap "caller >/dev/null || \
printf '%s\\n' \"\$(date '+%Y-%m-%dT%H:%M:%S%z')\
\$(tty) \${BASH_COMMAND}\" 2>/dev/null >>~/.command_log" DEBUG
fi
The above sets a trap on DEBUG
, which is executed just before an ordinary command is executed. The caller
built-in is used to test whether the command is being typed at an interactive shell or run via something like ~/.bashrc
. The value ${BASH_COMMAND}
contains the command currently being executed.
Solution 3
Something that might also be of interest to you is how to search through your previous command history. You can reverse search your history on the command line by pressing Ctrl+r
and then typing letters you wish to match. If you have more than one matching command, press Ctrl+r
again. To quit the reverse search, press Ctrl+g
.
http://www.ice2o.com/bash_quick_ref.html
Solution 4
You can only go back so far as your history limit is set; once it has reached that point the history will start to be overwritten. However, it is possible to have a larger history size for the future. Put this in your .bashrc and specify a value (mine is set at 1000):
export HISTSIZE=1000
Related videos on Youtube
Edward Tanguay
Updated on September 18, 2022Comments
-
Edward Tanguay over 1 year
I created an Ubuntu virtualbox machine a couple weeks ago and have been working on projects off and on in it since then.
Now I would like to find the syntax of some commands I typed in the terminal a week ago, but I have opened and closed the terminal window and restarted the machine numerous times.
How can I get the
history
command to go back to the first command I typed after I created the machine?
Or is there another place that all the commands are stored in Ubuntu?-
RobotHumans almost 12 yearsAs far as I know you can't. You can only go back as far as the history buffer was set. If you're asking about what package you could install to indefinitely save command history in the future, perhaps you should revise your question.
-
-
Eliah Kagan almost 12 yearsIt's not true that history is only saved from one terminal/shell. When you run
history
, it shows the current shell's history as most recent, and history from all other shells (including shells in previous sessions, including across power cycles) before that. All shell instances save their history to.bash_history
when the exit. No instances save it there automatically, before that point. -
Ashish Ahuja over 5 yearsI'd like to note that on macOS, this technique also logs
update_terminal_cwd
in the history file, which is a command the user does not run, but is automatically run.