Logging (and retrieving) command-line commands

K

KenJackson

Guest
Whenever I'm trying to recollect a command I've used in days or years gone by, I use my greplog command. Say I know that I generated an obfuscated HTML email link in the past, then I execute
greplog obfusc
and I can see all the commands I've logged that use that word plus the dates executed.

There was enough interest in history feature of bash in the Something to try thread that I though I'd post how I log my commands. This is a related capability that may be of interest. But whereas the history file rolls over after a fixed size of commands, my log is never automatically trimmed or deleted. The logging isn't automatic either. I have complete control.


When I execute a command I might want to remember, or to remember when I ran it, I do this to append it with the date to my log file:
Code:
CTRL-P          # Or UP-arrow to pull up the last command
CTRL-A          # To move the cursor to the front of the command
log             # Add "log " (with a space), sometimes I also have to add backslashes
<ENTER>


Using the logged commands is easiest if I just want to see them, in which case I simply use greplog or taillog. Note that I can use switches for grep and tail also, so I can use greplog -i <text> for case insensitivity or taillog -n20 to see the last twenty commands.

It's a little awkward to re-execute a command, but if the command is complicated enough, it's worth it to do something like the following. Maybe someday I'll figure out a way to simplify it.
Code:
$ greplog obfusc                       # Just so I can see it
01/13/12  echo '<a href="mailto:[email protected]">ken</a>' | pandoc -f html -t html --email-obfuscation=references
$ greplog -o 'ech.*obfusc.*'           # Up-arrow and edit to verify I stripped off the date
echo '<a href="mailto:[email protected]">ken</a>' | pandoc -f html -t html --email-obfuscation=references
$ eval $(greplog -o 'ech.*obfusc.*')   # To generate the obfuscated email link
<a href="&#x6d;&#x61;&#x69;&#108;&#116;&#x6f;&#58;&#x6b;&#x65;&#110;&#106;&#x61;&#x63;&#x6b;&#x73;&#x6f;&#110;&#64;&#x6d;&#x79;&#x6f;&#114;&#x67;&#46;&#x63;&#x6f;&#x6d;">&#x6b;&#x65;&#110;</a>


To implement my logging feature, I have lines very similar to this in my standard ~/.bashrc file which I replicate on every platform I have access to.
Code:
function log
{
    local LOG="$HOME/text/mylog.txt"
    echo "$(date +%D)  $*" >> $LOG
}
function greplog { test -n "$1"  &&  grep "$@" "$HOME/text/mylog.txt"; }
function taillog { tail $* "$HOME/text/mylog.txt"; }
 


"Maybe someday I'll figure out a way to simplify it." When you do, let us know. Otherwise, it's simply trivia.
 

Members online


Latest posts

Top