Parse History For specified Commands, Persist to Individual Log Files

for i in [enter list of commands]; do history |grep -i "$i" >> ~/histories/"${i}"_hist.txt;done
To help store and keep important but not often used commands I resorted to this. A basic for loop which when fed separate commands for its input searches the history and any references to that command or string gets appended to a file named [command name]_hist.txt Revising it to the following though ti include root / sudo'd commands is probably critical the output above reflects the change, here below: for i in docker elinks ufw fail2ban awk sed grep diff nginx apt bash for function bower github rsync sshfs who scp sftp tugboat aws pip npm ssh mysql php 8000 8080 3000 python serve s3ql s3cmd s3api s3 bash init wget; do cat /home/ray/.bash_history |grep -i "$i" >> /home/ray/histories/"${i}"_hist.txt;sudo cat /root/.bash_history |grep -i "$i" >> /home/ray/histories/"${i}"_sudo_hist.txt;done then a simple more to look for a particular result more -s -40 -p -f -d tugboat*txt simple, solved my problem and alerted me to a lack of certain appearances of commands that signal a bit of an issue Not so sold on the usefulness as to warrant a bash function or further convenience or logic we shall see. Could use some tweaking but what commands dont!
Sample Output
   0 -rw-r--r--  1 ray ray    0 Dec 15 22:17 3000_hist.txt
4.0K -rw-r--r--  1 ray ray  309 Dec 15 22:17 3000_sudo_hist.txt
   0 -rw-r--r--  1 ray ray    0 Dec 15 22:17 8000_hist.txt
4.0K -rw-r--r--  1 ray ray  324 Dec 15 22:17 8000_sudo_hist.txt
   0 -rw-r--r--  1 ray ray    0 Dec 15 22:17 8080_hist.txt
4.0K -rw-r--r--  1 ray ray  309 Dec 15 22:17 8080_sudo_hist.txt
4.0K -rw-r--r--  1 ray ray 1.3K Dec 15 22:17 apt_hist.txt
4.0K -rw-r--r--  1 ray ray  903 Dec 15 22:17 apt_sudo_hist.txt
4.0K -rw-r--r--  1 ray ray  724 Dec 15 22:17 awk_hist.txt
4.0K -rw-r--r--  1 ray ray   93 Dec 15 22:17 awk_sudo_hist.txt
8.0K -rw-r--r--  1 ray ray 8.0K Dec 15 22:17 aws_hist.txt
4.0K -rw-r--r--  1 ray ray  309 Dec 15 22:17 aws_sudo_hist.txt
4.0K -rw-r--r--  1 ray ray 1.8K Dec 15 22:17 bash_hist.txt
4.0K -rw-r--r--  1 ray ray 1.7K Dec 15 22:17 bash_sudo_hist.txt
4.0K -rw-r--r--  1 ray ray  477 Dec 15 22:17 bower_sudo_hist.txt
4.0K -rw-r--r--  1 ray ray  553 Dec 15 22:17 diff_hist.txt
4.0K -rw-r--r--  1 ray ray  370 Dec 15 22:17 diff_sudo_hist.txt
4.0K -rw-r--r--  1 ray ray 2.4K Dec 15 22:17 docker_hist.txt
4.0K -rw-r--r--  1 ray ray  442 Dec 15 22:17 docker_sudo_hist.txt
4.0K -rw-r--r--  1 ray ray  612 Dec 15 22:17 elinks_hist.txt
4.0K -rw-r--r--  1 ray ray  403 Dec 15 22:17 elinks_sudo_hist.txt
4.0K -rw-r--r--  1 ray ray  601 Dec 15 22:17 fail2ban_hist.txt
4.0K -rw-r--r--  1 ray ray  659 Dec 15 22:17 fail2ban_sudo_hist.txt
4.0K -rw-r--r--  1 ray ray 1.9K Dec 15 22:17 for_hist.txt
4.0K -rw-r--r--  1 ray ray  783 Dec 15 22:17 for_sudo_hist.txt
   0 -rw-r--r--  1 ray ray    0 Dec 15 22:17 function_hist.txt
4.0K -rw-r--r--  1 ray ray  309 Dec 15 22:17 function_sudo_hist.txt
4.0K -rw-r--r--  1 ray ray 1.8K Dec 15 22:17 github_hist.txt
4.0K -rw-r--r--  1 ray ray  602 Dec 15 22:17 github_sudo_hist.txt
4.0K -rw-r--r--  1 ray ray 2.3K Dec 15 22:17 grep_hist.txt
4.0K -rw-r--r--  1 ray ray 2.2K Dec 15 22:17 grep_sudo_hist.txt
4.0K -rw-r--r--  1 ray ray  337 Dec 15 22:17 init_hist.txt
4.0K -rw-r--r--  1 ray ray  438 Dec 15 22:17 init_sudo_hist.txt
4.0K -rw-r--r--  1 ray ray   25 Dec 15 22:17 mysql_hist.txt
4.0K -rw-r--r--  1 ray ray  391 Dec 15 22:17 mysql_sudo_hist.txt
4.0K -rw-r--r--  1 ray ray  200 Dec 15 22:17 nginx_hist.txt
4.0K -rw-r--r--  1 ray ray 1.8K Dec 15 22:17 nginx_sudo_hist.txt
4.0K -rw-r--r--  1 ray ray  735 Dec 15 22:17 npm_hist.txt
4.0K -rw-r--r--  1 ray ray  651 Dec 15 22:17 npm_sudo_hist.txt
4.0K -rw-r--r--  1 ray ray  309 Dec 15 22:17 php_hist.txt
4.0K -rw-r--r--  1 ray ray  309 Dec 15 22:17 php_sudo_hist.txt
4.0K -rw-r--r--  1 ray ray   93 Dec 15 22:17 pip_hist.txt
4.0K -rw-r--r--  1 ray ray  309 Dec 15 22:17 pip_sudo_hist.txt
4.0K -rw-r--r--  1 ray ray   29 Dec 15 22:17 python_hist.txt
4.0K -rw-r--r--  1 ray ray  343 Dec 15 22:17 python_sudo_hist.txt
4.0K -rw-r--r--  1 ray ray  938 Dec 15 22:17 rsync_hist.txt
4.0K -rw-r--r--  1 ray ray  309 Dec 15 22:17 rsync_sudo_hist.txt
   0 -rw-r--r--  1 ray ray    0 Dec 15 22:17 s3api_hist.txt
4.0K -rw-r--r--  1 ray ray  309 Dec 15 22:17 s3api_sudo_hist.txt
   0 -rw-r--r--  1 ray ray    0 Dec 15 22:17 s3cmd_hist.txt
4.0K -rw-r--r--  1 ray ray  309 Dec 15 22:17 s3cmd_sudo_hist.txt
8.0K -rw-r--r--  1 ray ray 7.9K Dec 15 22:17 s3_hist.txt
4.0K -rw-r--r--  1 ray ray   75 Dec 15 22:17 s3ql_hist.txt
4.0K -rw-r--r--  1 ray ray  337 Dec 15 22:17 s3ql_sudo_hist.txt
4.0K -rw-r--r--  1 ray ray  367 Dec 15 22:17 s3_sudo_hist.txt
4.0K -rw-r--r--  1 ray ray   96 Dec 15 22:17 scp_hist.txt
4.0K -rw-r--r--  1 ray ray  309 Dec 15 22:17 scp_sudo_hist.txt
4.0K -rw-r--r--  1 ray ray  755 Dec 15 22:17 sed_hist.txt
   0 -rw-r--r--  1 ray ray    0 Dec 15 22:17 sed_sudo_hist.txt
4.0K -rw-r--r--  1 ray ray 3.2K Dec 15 22:17 serve_hist.txt
4.0K -rw-r--r--  1 ray ray  344 Dec 15 22:17 serve_sudo_hist.txt
   0 -rw-r--r--  1 ray ray    0 Dec 15 22:17 sftp_hist.txt
4.0K -rw-r--r--  1 ray ray  309 Dec 15 22:17 sftp_sudo_hist.txt
   0 -rw-r--r--  1 ray ray    0 Dec 15 22:17 sshfs_hist.txt
4.0K -rw-r--r--  1 ray ray  309 Dec 15 22:17 sshfs_sudo_hist.txt
4.0K -rw-r--r--  1 ray ray 1.7K Dec 15 22:17 ssh_hist.txt
4.0K -rw-r--r--  1 ray ray 1.2K Dec 15 22:17 ssh_sudo_hist.txt
4.0K -rw-r--r--  1 ray ray 2.1K Dec 15 22:17 tugboat_hist.txt
4.0K -rw-r--r--  1 ray ray  568 Dec 15 22:17 tugboat_sudo_hist.txt
4.0K -rw-r--r--  1 ray ray  162 Dec 15 22:17 ufw_hist.txt
4.0K -rw-r--r--  1 ray ray 1.1K Dec 15 22:17 ufw_sudo_hist.txt
4.0K -rw-r--r--  1 ray ray 1.9K Dec 15 22:17 wget_hist.txt
4.0K -rw-r--r--  1 ray ray  309 Dec 15 22:17 wget_sudo_hist.txt
4.0K -rw-r--r--  1 ray ray  296 Dec 15 22:17 who_hist.txt
4.0K -rw-r--r--  1 ray ray  405 Dec 15 22:17 who_sudo_hist.txt

0
2014-12-16 03:37:02

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: