How to find history of shell commands since machine was created?

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?


It may or may not be possible to get all commands. It depends how many commands you executed and how the history limit was set.

However you can see the history list are stored here

/home/<YOUR_USERNAME>/.bash_history

Related question (for handling multiple shell history):

Is it possible to make writing to .bash_history immediate?


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.


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


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