Open your terminal and run history to see your most recent commands, then save that output to a file with history > ~/bash_history_backup.txt. This takes about 2 minutes and needs only a terminal. You will use a few built-in tools and a simple script to turn that raw history into a habit log.
Why the Default Bash History Isn’t Enough
Bash already records every command you type, but it stores them in one long list with no timestamps by default. That makes it hard to see patterns like “I run git status every 10 minutes” or “I spend my mornings on ssh into servers.” The default also truncates older entries, so you lose the long view.
The fix is to add timestamps and a bit of structure. You can do this without installing anything extra.
Before You Start: What to Check
- Your shell is Bash (run
echo $SHELL; if it shows/bin/bash, you are good). - You have write permission to your home directory (you almost certainly do).
- You are comfortable editing a file called
.bashrcin your home folder.
Step-by-Step: Add Timestamps and Save a Habit Log
- Open your
.bashrcfile with a text editor, for examplenano ~/.bashrc. - Add these two lines at the end of the file:
export HISTTIMEFORMAT="%F %T "
export HISTSIZE=10000
The first line makes Bash record each command with a date and time. The second keeps the last 10,000 commands, which is about a month of heavy use. - Save and reload the file with
source ~/.bashrc. Your next commands will now carry timestamps. - Create a habit log directory (once):
mkdir -p ~/bash_habits - Write a small script to extract your daily command counts. Create a file
~/bash_habits/track.shwith this content:#!/bin/bash history | awk '{print $2}' | sort | uniq -c | sort -rn > ~/bash_habits/command_counts.txt history | awk '{print $1}' | cut -c1-10 | sort | uniq -c > ~/bash_habits/daily_counts.txtThe first line counts how often each command appears. The second counts how many commands you ran per day (the date is the first field after the timestamp).
- Make the script executable:
chmod +x ~/bash_habits/track.sh - Run it whenever you want a snapshot:
~/bash_habits/track.sh
How to Read Your Habit Log
Open ~/bash_habits/command_counts.txt to see your most frequent commands. The number before each command is the count. For example, 1234 git means you typed git 1,234 times in your current history.
The daily_counts.txt file shows your activity per day, which helps you see which days you are most active in the terminal. If you want to know your busiest hour, change the script to print the hour instead of the date: replace cut -c1-10 with cut -c12-13.
These files are plain text, so you can open them in any editor or view them in the terminal with cat.
How Long Does This Tracking Last?
The HISTSIZE=10000 setting keeps your last 10,000 commands. If you run about 200 commands a day, that covers about 50 days. After that, the oldest entries drop off. To keep a permanent record, run the script once a week and copy the two output files to a folder like ~/bash_habits/archive/ with a date in the filename.
For example, add this to your weekly routine: cp ~/bash_habits/*.txt ~/bash_habits/archive/$(date +%Y-%m-%d)/ (create that folder first). That way you build a long-term history without losing data.
What If the Timestamps Don’t Appear?
If you run history and still see no dates, your Bash might be configured to ignore the HISTTIMEFORMAT variable. Check if your .bashrc has a line like unset HISTTIMEFORMAT or if you are using a different shell (like Zsh). Run echo $0 to confirm you are in Bash. If not, switch to Bash by typing bash.
Also, the history command only shows commands from the current session unless you have HISTFILE set correctly. Make sure your .bashrc has export HISTFILE=~/.bash_history (it usually does by default).
Turning the Log into a Habit
Once you have a few weeks of data, look for patterns. Do you run git commit more on Fridays? Do you spend most of your time in python or vim? Use that to decide what to automate or what to practice less.
For example, if you notice you run ls hundreds of times a day, you might create an alias like alias ll='ls -la' to get more information in one go. If you see you frequently type the same long command, add a function in your .bashrc.
Tracking is not about judging yourself; it is about noticing what you already do so you can make small changes that feel natural.
How Often Should I Run the Script?
Run it daily if you want a daily habit check, or weekly for a broader view. The script is fast and safe to run as often as you like.
Can I Track Specific Commands Only?
Yes. Modify the script to filter by command name. For example, to count only git commands, replace the first line with history | grep ' git ' | awk '{print $2}' | sort | uniq -c.
Does This Work on macOS?
Yes, if you use Bash. macOS ships with Bash 3.2, which supports HISTTIMEFORMAT. If you use the default Zsh, the commands are slightly different, but the idea is the same.
Related
- How to Track Mood and Habits Together
- How to Track Personal Finance Habits
- What Happens When You Track Habits for 90 Days
General information for everyday wellbeing, not medical advice. If something affects your health, sleep or mood for more than a short while, talk to a qualified professional. Read more
Spotted an error? Tell us and we will correct it (corrections policy).