Skip to content

Instantly share code, notes, and snippets.

@alfeugds
Last active February 20, 2024 23:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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() {
printf -v date '%(%Y-%m-%d_%H-%M-%S)T' -1
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