{"id":14413,"date":"2014-10-31T12:55:26","date_gmt":"2014-10-31T17:55:26","guid":{"rendered":"http:\/\/bucktownbell.com\/?p=14413"},"modified":"2014-11-04T13:18:18","modified_gmt":"2014-11-04T19:18:18","slug":"archiving-command-history-in-linux","status":"publish","type":"post","link":"http:\/\/bucktownbell.com\/?p=14413","title":{"rendered":"Archiving command history in Linux"},"content":{"rendered":"<pre>#!\/bin\/bash\r\n umask 077\r\n max_lines=10000\r\n linecount=$(wc -l &lt; ~\/.bash_history)\r\n if (($linecount &gt; $max_lines)); then\r\n         prune_lines=$(($linecount - $max_lines))\r\n         head -$prune_lines ~\/.bash_history &gt;&gt; ~\/.bash_history.archive \\\r\n                &amp;&amp; sed -e \"1,${prune_lines}d\"  ~\/.bash_history &gt; ~\/.bash_history.tmp$$ \\\r\n                &amp;&amp; mv ~\/.bash_history.tmp$$ ~\/.bash_history\r\n fi<\/pre>\n<p>via <a href=\"http:\/\/mywiki.wooledge.org\/BashFAQ\/088\">BashFAQ\/088 &#8211; Greg&#8217;s Wiki<\/a>.<\/p>\n<p>I needed to manage shell command history in a formal fashion in order to turn repeated sequences into scripts without having to type them in again.\u00a0 I also wanted a\u00a0 record of packages installed and in what order. \u00a0 The history of commands is contained in .bash_history file which is read once when a terminal opens.\u00a0\u00a0 Running set -o vi allows for history commands to be recalled using standard vi commands.\u00a0 The above script can be run as a user level cron job to periodically prune the top so many commands and place them into an archive.<\/p>\n<p>The bash statements below set history size and make it so a command will be written to the history file immediately and not simply when a terminal closes.\u00a0\u00a0 These should be placed in .bashrc or whatever file executes when a new terminal opens.<\/p>\n<pre>HISTFILESIZE=400000000\r\n<span id=\"BashFAQ.2F088.line-2\" class=\"anchor\"><\/span>HISTSIZE=10000\r\n<span id=\"BashFAQ.2F088.line-3\" class=\"anchor\"><\/span>PROMPT_COMMAND=\"history -a\"\r\n<span id=\"BashFAQ.2F088.line-4\" class=\"anchor\"><\/span>export HISTSIZE PROMPT_COMMAND\r\n<span id=\"BashFAQ.2F088.line-5\" class=\"anchor\"><\/span>\r\n<span id=\"BashFAQ.2F088.line-6\" class=\"anchor\"><\/span>shopt -s histappend<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>#!\/bin\/bash umask 077 max_lines=10000 linecount=$(wc -l &lt; ~\/.bash_history) if (($linecount &gt; $max_lines)); then prune_lines=$(($linecount &#8211; $max_lines)) head -$prune_lines ~\/.bash_history &gt;&gt; ~\/.bash_history.archive \\ &amp;&amp; sed -e &#8220;1,${prune_lines}d&#8221; ~\/.bash_history &gt; ~\/.bash_history.tmp$$ \\ &amp;&amp; mv ~\/.bash_history.tmp$$ ~\/.bash_history fi via BashFAQ\/088 &#8211; Greg&#8217;s Wiki. &hellip; <a href=\"http:\/\/bucktownbell.com\/?p=14413\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[1345,721,28,575],"class_list":["post-14413","post","type-post","status-publish","format-standard","hentry","category-operating-systems","tag-bash-script","tag-linux-admin","tag-linux-command","tag-useful-tips"],"_links":{"self":[{"href":"http:\/\/bucktownbell.com\/index.php?rest_route=\/wp\/v2\/posts\/14413","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bucktownbell.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bucktownbell.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bucktownbell.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/bucktownbell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=14413"}],"version-history":[{"count":5,"href":"http:\/\/bucktownbell.com\/index.php?rest_route=\/wp\/v2\/posts\/14413\/revisions"}],"predecessor-version":[{"id":14452,"href":"http:\/\/bucktownbell.com\/index.php?rest_route=\/wp\/v2\/posts\/14413\/revisions\/14452"}],"wp:attachment":[{"href":"http:\/\/bucktownbell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=14413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bucktownbell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=14413"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bucktownbell.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=14413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}