DEV Community

Anders Hornor
Anders Hornor

Posted on

Terminal History - How to make it work for you.

I recently was looking for some old commands I had made in my terminal and was hit hard when I realized I can only scroll so far back into a window. Well after some sleuthing I found out a way to not only look at old commands from long lost windows but also a was to restore old terminal sessions that may have a bash function that does just what you need it to do or something. So the story...
While looking at the oldest entry of my my newly opened terminal window I noticed something interesting. I saw this line of text:

Last login: Tue Nov 26 11:19:59 on ttys004

and it had me curious. If my terminal is taking some time stamp of my previous commands and saving it till that point maybe it has more or others. Well it does.

Terminal History: What is it?

Your terminal saves a history or all the commands committed over a period of time or a bash history. Each history is stored with a collection of other information as a bash session. Each bash session has a timestamp associated with an id and a store of all the commands that were committed during a set period of time. Dope right!

Each session can also be loaded into your bash_profile in essence restoring that session which is also a neat trick. Any way How do I see/ use all this cool shit? Easy peasy.

history

With the bash command history typed into your terminal a log of the previous (defaulted to 500) commands typed into your terminal will be displayed.

You can update the length of record in a few easy steps.

First you need to access your Terminal by opening that Search in your applications folder in your Finder File-Manager.

Next type history Voila your terminal history.

lol you typed the same command three times 3 different ways then moved files three times and tried the command a fourth way then went back to the original file ls'ed it then typed the fourth command a fourth time.
Oh you goof you! xP

Now checkout your bash_profile because next you need to edit that file.

nano -w ~/.bash_profile

Now you're cookin with gasoline so be careful I guess. There's a way to undo stuff in here (your bash_profile), but maybe you have some dope custom bash commands that you don't want to mess up or you didn't set up your bash_profile and got ran a command at some point that did and you aren't quite sure what all of the commands are that have been added and the file is full' of comments; it's all ugly and you dont know whats what in this jungle of a file open in your two-sizes-too-small, why-is-it-mustard-colored, terminal, so don't fuck with anything.

or maybe it just says path and has a path and that's about it.

Anyway your bash_profile looks you don't want to break anything you just want to either edit or add the lines(choose your own number; blank is ∞):

HISTSIZE=613511236114111118312
HISTFILESIZE=613511236114111118312

hint: use the control + W command to search in file open in a nano editor

Now you have an awesomely long bash history. If you ever need to find something again well you can. You're welcome. Now when you get all pissed off because you defined a variable in your bash window and can't re-assign it or something (I'm coming up with these on the spot, nothing to go off yet :) don't fret. You can delete your bash history...yes it's possible.

with the quick and dirty command

history -c

and it's gone.

Now for next time, .bash_sessions, restoring past histories, restoring past sessions, and saving the world one command at a time.

for fun check out some files in your /Users/<your-username-here>/.bash_sessions file

It's got everything you need to look deeply into your past. Soooo Deep you can almost touch it!

Top comments (0)