LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-13-2017, 10:42 AM   #1
KenJackson
Member
 
Registered: Jul 2006
Location: Maryland, USA
Distribution: Fedora and others
Posts: 757

Rep: Reputation: 145Reputation: 145
How I log commands for a permanent history


The history command is very helpful to show the last commands that were entered on the command line, but it rolls over, so the commands you entered a while ago are gone.

This is a solution I came up with which I use everywhere I have access. I have directory $HOME/bin on my path, and this script named log in that directory:
Code:
#!/bin/sh
LOG="$HOME/logs/cmdlog.txt"

test $# -lt 1  &&  echo "Log what?"  &&  exit
test -f $LOG  ||  { mkdir -p ${LOG%/*} && touch $LOG; }  ||  exit 127

date "+%D  $*" >> $LOG
After I get a command to work right that I know I'm going to want to reference, I hit up arrow, CTRL-A to get to the front of it, type "log " and hit enter. (I may need to add quotes and/or backslashes if it's a complicated command.) Now it's a permanent entry in my personal log file.

I also have these two functions declared in my standard .bashrc file, which I copy to all machines I have access to. They make it easy to find things in the log file.
Code:
greplog() { test -n "$1"  &&  grep "$@" "$HOME/logs/cmdlog.txt"; }
taillog() { tail $* "$HOME/logs/cmdlog.txt"; }
 
Old 10-13-2017, 11:47 AM   #2
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 7,666
Blog Entries: 19

Rep: Reputation: 4490Reputation: 4490Reputation: 4490Reputation: 4490Reputation: 4490Reputation: 4490Reputation: 4490Reputation: 4490Reputation: 4490Reputation: 4490Reputation: 4490
The tee command lets you save a copy of your input too.
 
Old 10-13-2017, 11:57 AM   #3
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
i use these in my bashrc:
Code:
HISTSIZE=50000
HISTFILESIZE=2000000
HISTCONTROL=ignoredups:erasedups
shopt -s histappend
i.e. a ridiculously large history.

has never failed me so far.
 
Old 10-13-2017, 12:20 PM   #4
KenJackson
Member
 
Registered: Jul 2006
Location: Maryland, USA
Distribution: Fedora and others
Posts: 757

Original Poster
Rep: Reputation: 145Reputation: 145
Quote:
Originally Posted by ondoho View Post
i.e. a ridiculously large history.

has never failed me so far.
Is it large enough to show you commands you entered in 2009? Here's a smattering of commands I saved from yesteryear. It's not that these are crucial, it's just that I found them useful at some point and I want to be able to remember how I did something or see when I did a particular thing.
Code:
04/30/09  grep -i '^(EE)\|^(WW)\|dri[^a-z]\|direct\|chipse' /var/log/Xorg.0.log
06/06/09  sudo brctl addbr br0
06/06/09  sudo brctl addif br0 eth0
11/23/09  sudo semanage port -a -t http_port_t -p tcp 8864
06/13/10  sudo preupgrade-cli "Fedora 13 (Goddard)"
05/28/11  sudo setsebool -P httpd_read_user_content 1
05/30/11  sudo setsebool -P wine_mmap_zero_ignore 1  # See wine bug 15569
10/18/11  su -c 'echo /usr/local/lib > /etc/ld.so.conf.d/local.conf'
12/17/11  sudo systemctl start httpd.service
12/19/11  sudo systemctl --system enable httpd.service
 
Old 10-13-2017, 12:49 PM   #5
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
i wasn't even using linux in 2009

no, my reckoning is if i didn't reuse it by the time it gets rotated, it can't have been too important.

this concept hasn't failed me so far.

or, if there should ever be such a command that i entered only once in my linux careeer, yet it is still very, very important, i'd probably have made a blog post about it...
 
Old 10-13-2017, 01:07 PM   #6
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,738

Rep: Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222
KenJackson: Thank you for sharing a pretty good idea. I will probably use it.

When I've struggled with creating something at the command line, I usually just save it as a script once I have it worked out.
I tend to do the same thing after finding something in history for the upteenth time. My ~/bin has lots in it, and it's in my $PATH.
I've been doing things like that since at least 1999.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to clear permanent history in linux??? kunal_777 Linux - Newbie 10 05-23-2017 01:59 PM
History commands u2u2u2 Linux - Newbie 1 04-13-2011 03:26 PM
copy history commands ZAMO Linux - General 4 01-04-2008 01:30 PM
History commands files anjanesh Linux - Newbie 2 05-28-2006 09:40 AM
where is my history of commands? learnfast Linux - Newbie 3 03-14-2005 09:28 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration