Skip to content

Instantly share code, notes, and snippets.

@alfeugds
Last active June 6, 2024 18:40
Show Gist options
  • Save alfeugds/4d6e0d7c1bacef1b8a6ae9b327013d05 to your computer and use it in GitHub Desktop.
Save alfeugds/4d6e0d7c1bacef1b8a6ae9b327013d05 to your computer and use it in GitHub Desktop.
Bash utils
#################
# log it!
#
# Use it to log a command to a file. Useful when you run commands in the terminal and want to keep history of its output.
#
# usage: log_it [args]
#
# example: log_it df -h
#
# the above command will add both stdout and stderr to a log file called 2024-02-20_16-08-53-df_-h.log
#################
log_it() {
DATE=$(date '+%Y-%m-%d_%H-%M-%S')
log_file_suffix=$(echo "$@" | sed -e 's/[^A-Za-z0-9._-]/_/g')
log_file="$DATE-$log_file_suffix.log"
echo "running command: $@" | tee -a "$log_file"
echo "logging on $log_file" | tee -a "$log_file"
echo "--" | tee -a "$log_file"
# stdout and stderr
$@ 2>&1 | tee -a "$log_file"
}
@thiagosanches
Copy link

That's cool!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment