Bash Tips
November 27, 2007 | Leave a Comment
1. Lost bash history
If you have a terminal open and are typing commands, then open another one and use that for a while, the new terminal won’t remember any of the commands typed in the first one. In addition, closing the first terminal, and then the second will overwrite any of the commands typed in the first terminal. Doubly annoying!
This happens because the bash history is only saved when you close the terminal, not after each command. To fix it:
Edit your .bashrc (for beginners, any file starting with a . is hidden - they contain user preferences.)nano ~/.bashrc
No need for a sudo here Ubuntuers, this is your own file, not a system setting. I like nano, but it’s up to you, choose gedit, kate, mousepad, vi or emacs as you wish.
add the linesshopt -s histappend
PROMPT_COMMAND=’history -a’
And save. (control - O to write out. ^ means control in nano and other software, so the bottom of the editor does actually make sense to beginners! ^X to exit.)
This makes bash append history instead of overwriting it, and makes it so that each time the prompt is shown it writes out all the history.
2. Stupid spelling mistakes
Add
shopt -s cdspell
to your .bashrc again. This will make sure that spelling mistakes such as ect instead of etc are ignored.
3. Duplicate entries in bash history
I often type cd .. multiple times in a row, when I then press UP to go back to earlier commands I don’t want to be reminded of my earlier inelegant excursions around the file system.
Add
export HISTCONTROL="ignoredups"
to .bashrc again.
Even better, add
export HISTIGNORE="&:ls:[bf]g:exit"
This will ignore duplicates, as well as ls, bg, fg and exit as well, making for a cleaner bash history.
4. Multiple line commands split up in history
Add
shopt -s cmdhist
to .bashrc, this will change multiple line commands into single lines for easy editing.
5. A couple of neat extras suggested by commenters
Press control R in bash, then start typing and you can search through your past commands much easier than just pressing UP 300 times…
Alternatively, use
history | grep "foo"
to search through your history - “foo” is the thing you are searching for. (Thanks to Ally)
cd -
goes to the last directory you were in - useful if you want to go somewhere to change something, then need to quickly flip back again.
Pressing Esc . brings up the last object you referred to. For instance, if you had just typed cat /etc/apt/sources.list , then typing rm then pressing esc . would auto complete to rm /etc/apt/sources.list


6.For getting the last object referred to, another way to do (that’s slightly quicker/easier to type than ESC + .) is:
Alt + . (I find it a lot easier to hit alt + . as to get escape requires moving hands quite a bit from the home row)
By far the coolest bash trick, (that I only learnt about last year) is repeating multiple parts of the commant with curly brackets {}. Ie, if you are in home and need to quickly make a backup copy of /etc/apache2/httpd.conf (to pick a file at random), instead of typing
cp /etc/apache2/httpd.conf /etc/apache2/httpd.conf.bakyou can type
cp /etc/apache2/httpd.conf{,.bak}I use that trick a *lot*.
7.when trying to track down a file(s) that are hogging up disk space, I use:
for each in `ls -1`;do du -sh $each;done
528M cache
39M lib
8.0K local
32K lock
262M log
16K lost+found
4.0K mail
and then cd and follow the big directories to the big files