Ubuntu – How to recall history in teminal

bashcommand lineenvironment-variables

I installed Ubuntu 10.10 yesterday. Then while using the terminal I noticed that once I quit a terminal session its history is gone, although history exists for applications in terminal like-ROOT(CERN) history.

In giving the command

echo $HISTFILESIZE 

it shows 2000. I changed that to 10000 when I quit the session and opened a new terminal; it again shows history as 2000 and no command can be accessed through up/down arrow key.

Please help. I am getting frustrated with soooo…. lot of typing in the terrminal.

Best Answer

If no commands can be run using or that indicates that your history file is empty or you have no read-permissions. Check that.

Maybe you run the commands as another user. If so do:

su otheruser
history 10

and look at the output.

If you change $HISTFILESIZE the changes will be overwritten, whenever you invoke an other shell. To prevent that you should change that variable in your .bashrc. Also you should set the variable $HISTSIZE to a larger value.

Generally usefull tips using histories:

Ctrl+R does a reverse search of your history for you. Alt+. pastes the last argument of the last command into your prompt at cursor position.

Also the bang (!) operator will repeat commands for you in terminal (if it's just about saving you some typing. Example:

confus@confusion:~$ echo "bang + letters will repeat the last command starting with these letters."
bang + letters will repeat the last command starting with these letters.

confus@confusion:~$ clear

confus@confusion:~$ !ech
bang + letters will repeat the last command starting with these letters.

All those histroy related things are stored in the home directory of any user in a file called .bash_history. You can look them up by using the history-command. Her is a small tutorial on howto use history.

confus@confus:~$ history 4 #will print last 4 commands
 1848  ls
 1849  clear
 1850  vi /home/confus/.local/share/applications/nautilus-home.desktop
 1851  history 4

Another very usefull thing is to create a .inputrc file in your home directory with the following content:

"\e\e[C": forward-word
"\e\e[D": backward-word
"\e[A": history-search-backward
"\e[B": history-search-forward

This way you can use the or to complete commands you started to type from history. E.g. when you type a rather lenghty command such as rsync -a -v --human-readable --prune-empty-dirs -e 'ssh -i .ssh/id_rsa' --include="*/" --exclude="snapshot_*" --exclude="restart.*" /scratch/ x@cluster1:/home/x/runs/ and want to run it a second time you can just type rsy and then an it will complete to the last command that started with rsy. Another press of the uparrow will complete to the second-last and so on. I don't know why this isn't the default.