-
Notifications
You must be signed in to change notification settings - Fork 0
Customise bash profile: Logging Your Command History Automatically
Another customisation you can add to your ~/.bash_profile is automatic logging of your command history. This can be very helpful for tracking what you’ve run, useful for debugging, documenting workflows, or reproducing results later.
The function below, myhist(), records each executed command with a timestamp, username, and working directory. It saves a brief record in the current folder and a daily log in a central directory (you can customise this path).
function myhist() {
local logfile="commands.txt"
local user="$USER"
local datetime=$(date '+%Y-%m-%d %H:%M:%S')
local dateonly=$(date +%F)
local cwd="$(env pwd)"
local cmd=$(HISTTIMEFORMAT= history 1 | sed 's/^[ ]*[0-9]\+[ ]*//')
local datelog="/path/to/logs/myhist_${user}_log_${dateonly}.txt"
# Two-line format for per-directory commands.txt
echo -e "[$datetime] [$cwd] [$user]\n CMD: $cmd" | tee -a "$logfile"
# Single-line format for user-level daily log
echo "[$datetime] [$cwd] [$user] CMD: $cmd" >> "$datelog"
}
You can run this manually (e.g. after a key command) by typing:
command; myhist
Or, you can automate the logging process to record every command you run by adding the following line to your ~/.bash_profile:
export PROMPT_COMMAND='
datetime=$(date +%Y-%m-%d)
cmd=$(HISTTIMEFORMAT= history 1 | sed "s/^[ ]*[0-9]\+[ ]*//");
if [[ -n "$cmd" ]]; then
echo "[$(date "+%Y-%m-%d %H:%M:%S")] [$(env pwd)] [$(whoami)] CMD: $cmd" >> /path/to/logs/cmdlog_${USER}_${datetime}.log
fi'
This leverages the built-in Bash variable PROMPT_COMMAND, which executes just before each prompt appears, effectively after every command. Each entry is written to a log file named by date (e.g. cmdlog_swarbred_2025-10-31.log).
Tip: Make sure your log directory exists and is writable:
mkdir -p /path/to/logs/
After editing your ~/.bash_profile, either log out and back in, or reload it immediately with:
source ~/.bash_profile
- Induction
- HPC Best practice
- Job Arrays - RC documentation
- Methods to Improve I/O Performance - RC documentation
- Customising your bash profile for ease and efficiency
- Customise bash profile: Logging Your Command History Automatically
- Using the ei-gpu partition on the Earlham Institute computing cluster
- Using the GPUs at EI
- HPC Job Summary Tool
- EI Cloud (CyVerse)
- Git and GitHub
- Worked examples
- Job Arrays
- Using Parabricks on the GPUs
- dependencies
- Software installations
- Workflow management system
- Transfers
- Local (mounting HPC storage)
- Remote - <1gb (ood)
- Remote - <50gb (nbi drop off)
- Remote - No limit (globus)
- mv command