TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

How and Why to Log Your Bash History

376 点作者 mattnedrich将近 9 年前

42 条评论

howeyc将近 9 年前
I do something very similar, but without the prompt settings. I have settings in .bashrc[0] to have the history file based on date. I then use fzf[1] (fzf-tmux is great) and a grep-like tool(sift[2]) to use for ctrl-r that fuzzy-searches history and orders by usage frequency[3]. This way I can easily search for the command I&#x27;m thinking of fairly quickly. Particularly useful for those times I want to run a command again that was quite long or had more than a couple options&#x2F;flags.<p>---<p>[0] HISTFILE=&quot;${HOME}&#x2F;Sync&#x2F;Dotfiles&#x2F;history&#x2F;$(date -u +%Y-%m-%d.%H.%M.%S)_${HOSTNAME_SHORT}_$$&quot;; export HISTFILE<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;junegunn&#x2F;fzf" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;junegunn&#x2F;fzf</a><p>[2] <a href="https:&#x2F;&#x2F;sift-tool.org&#x2F;" rel="nofollow">https:&#x2F;&#x2F;sift-tool.org&#x2F;</a><p>[3] __fzf_history__() {<p>sift --no-color -e &quot;^[^#]&quot; --files &quot;<i>_${HOSTNAME_SHORT}_</i>&quot; -N --no-filename $HOME&#x2F;Sync&#x2F;Dotfiles&#x2F;history | sort | uniq -c | sort | $(__fzfcmd) +s --tac +m -n1..,.. --tiebreak=index --toggle-sort=ctrl-r | sed &quot;s&#x2F; <i>[0-9]</i> *&#x2F;&#x2F;&quot;<p>}
评论 #11809242 未加载
评论 #11808768 未加载
评论 #11809832 未加载
SEJeff将近 9 年前
This entire blog post could be reduced to use the HISTFORMAT variable, which the author doesn&#x27;t apparently know about. Put this in your ~&#x2F;.bashrc:<p><pre><code> export HISTTIMEFORMAT=&#x27;%Y-%m-%d %H:%M.%S | &#x27; </code></pre> An example of the &quot;history | tail&quot; results:<p><pre><code> $ history | tail 50198 2016-05-31 10:15.57 | cd docker 50199 2016-05-31 10:16.03 | cd rpms&#x2F; 50200 2016-05-31 10:16.11 | scp docker* omniscience:&#x2F;tmp&#x2F; 50201 2016-05-31 10:14.06 | screen -ls 50202 2016-05-31 08:33.34 | screen -x 50203 2016-05-31 19:06.53 | grep HIST .bashrc 50204 2016-05-31 19:07.46 | history | tail 50205 2016-05-31 19:08.10 | task ls 50206 2016-05-31 19:08.23 | docker ps -qa 50207 2016-05-31 19:08.30 | history | tail </code></pre> A few from my bashrc:<p><pre><code> $ grep HIST .bashrc HISTCONTROL=ignoredups:ignorespace # for setting history length see HISTSIZE and HISTFILESIZE in bash(1) export HISTFILESIZE=99999 export HISTSIZE=99999 export HISTTIMEFORMAT=&#x27;%Y-%m-%d %H:%M.%S | &#x27; export HISTIGNORE=&quot;ls:exit:history:[bf]g:jobs&quot; </code></pre> And then a cronjob that simply backs up the data.
评论 #11811368 未加载
teamhappy将近 9 年前
I used to do the same thing to figure out which commands I should create short aliases for. Sounded like a good idea at the time but then I realized that I&#x27;m creating a file with an awful lot of interesting information in it and I not getting a lot in return. So I set my HISTSIZE to 1000 which is more than enough for interactive shell use and I don&#x27;t have to worry about having stuff like &quot;youtube-dl fuckmesilly.com&#x2F;${insane_porn_title}&quot; lying around on servers I have access to. (Or which server you can connect to with my private key -- you might as well remove HashKnownHosts from your ssh config if you log it all yourself.)<p>TLDR: Overwriting history is a feature.
评论 #11806918 未加载
评论 #11807134 未加载
评论 #11807487 未加载
评论 #11811182 未加载
评论 #11809146 未加载
评论 #11808683 未加载
yomly将近 9 年前
I&#x27;m a big fan of saving history - trying to remember all the shell based commands we use is a nightmare!<p>If you&#x27;re using zsh, a tip I picked up from [0] was to alias all your common commands like<p><pre><code> &#x27;cd&#x27;, &#x27;ls&#x27; &#x27;fg&#x27;... </code></pre> to:<p><pre><code> &#x27; cd&#x27; &#x27; ls&#x27; &#x27; fg&#x27; ... </code></pre> Then add the following line to your zshrc to ignore lines prepended with a space:<p><pre><code> setopt HIST_IGNORE_SPACE </code></pre> This keeps your history cleaner from any ls, cd, fg inputs you use.<p>[0] <a href="http:&#x2F;&#x2F;chneukirchen.org&#x2F;blog&#x2F;archive&#x2F;2012&#x2F;02&#x2F;10-new-zsh-tricks-you-may-not-know.html" rel="nofollow">http:&#x2F;&#x2F;chneukirchen.org&#x2F;blog&#x2F;archive&#x2F;2012&#x2F;02&#x2F;10-new-zsh-tric...</a><p>EDIT: formatting
评论 #11806987 未加载
评论 #11807369 未加载
评论 #11811310 未加载
评论 #11807497 未加载
matt-attack将近 9 年前
Just a point on security, many advocate that logging commands is a major security weakness. Similar to why SSH now hashes entries in ~&#x2F;.ssh&#x2F;known_hosts by default. The idea is you don&#x27;t want to provide hints on which remote systems you connect to, as these can offer a springboard to the intruder.
评论 #11806994 未加载
评论 #11806995 未加载
joshstrange将近 9 年前
Just for those out there using zsh or fish I used to following in my .zshrc to get this working:<p><pre><code> precmd() { eval &#x27;if [ &quot;$(id -u)&quot; -ne 0 ]; then echo &quot;$(date &quot;+%Y-%m-%d.%H:%M:%S&quot;) $(pwd) $(history | tail -n 1)&quot; &gt;&gt;! ~&#x2F;Dropbox&#x2F;Logs&#x2F;Bash&#x2F;Macbook&#x2F;bash-history-$(date &quot;+%Y-%m-%d&quot;).log; fi&#x27; } </code></pre> I Store my logs in dropbox but you can put them wherever
评论 #11808959 未加载
评论 #11807390 未加载
ag_47将近 9 年前
I do the opposite and maybe weird - I disable history, and keep the history file contents hand curated. Whenever I come up&#x2F;across&#x2F;use a command I&#x27;d like to keep for future or use frequently, it gets appended to the history file.
评论 #11808450 未加载
评论 #11808469 未加载
jakozaur将近 9 年前
Well given that we copy and paste a lot, by mistake there can be credentials and sensitive data in the bash history...<p>Moreover, bash history doesn&#x27;t work nicely across different terminal tabs or computers.<p>Does anybody know a tool providing better command line history?
评论 #11806634 未加载
评论 #11807123 未加载
评论 #11806623 未加载
评论 #11806599 未加载
评论 #11812233 未加载
ashitlerferad将近 9 年前
I would just do this:<p>export HISTFILESIZE=5000000 export HISTSIZE=5000000 export HISTTIMEFORMAT=&quot;%F %T &quot; shopt -s histverify shopt -s histappend
0942v8653将近 9 年前
Isn&#x27;t this going to be pretty slow? It starts 5 subshells for every prompt. You could at least change $(pwd) to $PWD and switch the square brackets to double square brackets.
jrochkind1将近 9 年前
I don&#x27;t get it, isn&#x27;t your bash history already logged to `.bash_history`? What&#x27;s the point of using PROMPT_COMMAND to send it in addition to another file?
评论 #11807152 未加载
all_usernames将近 9 年前
A warning: do NOT do what one engineers I used to work with did, and upload his &quot;dot files&quot; onto GitHub for portability. A lot of secrets can be stored in a few months&#x27; worth of bash history.
评论 #11807766 未加载
评论 #11808741 未加载
tymm将近 9 年前
For the zsh users out there: I wrote a zsh plugin [0] a while ago which does a similar thing (writes time, directory and command to a file) + gives you a better (directory sensitive) history search.<p>[0] <a href="https:&#x2F;&#x2F;github.com&#x2F;tymm&#x2F;zsh-directory-history" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;tymm&#x2F;zsh-directory-history</a>
gdulli将近 9 年前
I go one big step further than this and log everything that comes across the screen.<p>One time it saved me from a crontab -r that wiped out a 100+ line crontab. I had viewed it recently so I just copied it out of my history.<p>On a day to day basis it&#x27;s more about looking up old queries I typed out, the results of those queries at that time, bash commands and their results, the state of a file I edited at a certain time, a stack trace, the output of an ls -l command, etc. Anything I ever do in a session.<p>Some of those have their own logs or ways to capture history, but a way to capture everything at once is much more comprehensive and less sensitive to forgetting to set the size of the bash history on a given machine, archiving the bash&#x2F;psql history files when a machine goes away, etc.<p>And besides not having to worry about the availability of distributed history&#x2F;log data, being able to grep everything at once is invaluable.
评论 #11807065 未加载
评论 #11807141 未加载
评论 #11808381 未加载
评论 #11811328 未加载
jawns将近 9 年前
This is useful if you are developing locally, but what if a lot of your command line usage is on remote servers that you have SSH&#x27;d into?<p>I believe iTerm2 has some support for logging remote commands, but I&#x27;m not sure it quite does the trick. Anybody know of another tool that will log both local and remote commands?
评论 #11806657 未加载
评论 #11806664 未加载
efyx将近 9 年前
Why logging to a file when you could just set the HITSIZE variable in your .bashrc ? (plus this will give you ctrl+r search which is a must)
评论 #11806688 未加载
评论 #11807006 未加载
评论 #11806846 未加载
yardie将近 9 年前
Use screen instead. Ctrl-a and &quot;H&quot; to enable and the same to disable. Log is saved in user directory as screen.&lt;session&gt;<p>That way I have control of when I log and what gets logged. And way easier to find than hunting for dot files.
评论 #11808513 未加载
dahart将近 9 年前
Damnit, this is so much smarter and easier than what I did!<p>I&#x27;ve been trying to leverage bash&#x27;s history, and wanted to avoid putting a file write into my prompt hook. But I have a big kludgy system for exporting bash history separately for each day, de-duping the overlapping history, and trying to make sure bash always logs the command I typed.<p>I&#x27;m still fighting with bash sometimes not saving my history commands. I&#x27;ve done everything recommended in all corners of the Internet, but certain keyboard combos will still cause history lines to not get saved. It&#x27;s maddening, and the solution here avoids it completely.
TorKlingberg将近 9 年前
This looks like quite a dirty hack, but I&#x27;ll try it.<p>I wish bash history wasn&#x27;t so broken. I suppose once upon a time it was ok to assume people only had one shell session at a time.
评论 #11806836 未加载
评论 #11806691 未加载
评论 #11806702 未加载
ZeroGravitas将近 9 年前
Possibly a good opportunity to ask:<p>On Mac OS X and iTerm2 I&#x27;ve noticed a thing where pasting in commands means they don&#x27;t get added to the history and using up arrow or search to find them doesn&#x27;t work. Anyone seen that? It&#x27;s one of those things that annoys me every time it happens, but I&#x27;m always in the middle of a task when it occurs so I never get a chance to figure it out.
评论 #11806727 未加载
评论 #11806723 未加载
jchoksi将近 9 年前
I implemented bash audit logging[0] by making use of Ryan Caloras&#x27;s bash-prexec[1] project which provides a fairly robust and resilient way to implement ZSH&#x27;s preexec and precmd functionality.<p>Some of the features of my solution are that it creates a sub folder in the user&#x27;s home directory called ~&#x2F;.bash_history and underneath this it will create sub folders for each month (YYYY-MM) and under each of those sub folders will reside a daily audit log file of all the bash command history (YYYY-MM-DD). The audit script logs both login and log outs, as well as each command executed in bash.<p>---<p>[0] <a href="https:&#x2F;&#x2F;github.com&#x2F;onelittlehope&#x2F;bash-prompt" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;onelittlehope&#x2F;bash-prompt</a> [1] <a href="https:&#x2F;&#x2F;github.com&#x2F;rcaloras&#x2F;bash-preexec" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;rcaloras&#x2F;bash-preexec</a>
saurik将近 9 年前
<a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=10695305" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=10695305</a><p>^ Here is my comment on a previous post for how I am logging all of my bash history in a logical manner (keeping track of terminals and timestamps, etc.) to an sqlite database.
评论 #11807112 未加载
评论 #11806978 未加载
aceperry将近 9 年前
&quot;For the last three and a half years, every single command I’ve run from the command line on my MacBook Pro has been logged... the return I’ve gotten on that small investment is immense.&quot;<p>I&#x27;m still not sure why it&#x27;s helpful to have three and a half years of your bash shell logs.
thinkersilver将近 9 年前
The logger in me appended hostname and the tty to the file name and the log message. You could also add a user name to this as well[0].<p>I love this idea because you can easily analyse your commands then modify or automate your workflow where it makes sense. I&#x27;ve done this in the past to create a two letter aliases to frequently used bash commands.<p>[0] export PROMPT_COMMAND=&#x27;if [ &quot;$(id -u)&quot; -ne 0 ]; then echo &quot;$(date &quot;+%Y-%m-%d.%H:%M:%S&quot;) $(hostname) $(tty) $(pwd) $(history 1)&quot; &gt;&gt; ~&#x2F;.logs&#x2F;bash-history-$(date &quot;+%Y-%m-%d&quot;).log.`hostname`; fi&#x27;
andmarios将近 9 年前
I&#x27;ve written a bash history server in go and it has saved me more time than it took me to write it.<p>It stores commands in a sqlite database. For each command it also saves the time, the user and the host. It permits global search with some useful features (grep A&#x2F;B&#x2F;C switches, regexp, etc).<p>I wrote it to scratch an itch, thus the documentation isn&#x27;t user friendly. If anyone is interested, shameless plug: <a href="https:&#x2F;&#x2F;github.com&#x2F;andmarios&#x2F;bashistdb" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;andmarios&#x2F;bashistdb</a>
dallas-stuart将近 9 年前
ICYMI: Sensible Bash addresses this well:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;mrzool&#x2F;bash-sensible" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;mrzool&#x2F;bash-sensible</a>
spike021将近 9 年前
A long time ago I set my history file to save up to the previous 1000 commands, and I ended up mostly learning grep for the first time because I wanted to search quickly for various recent commands. Stuff like &quot;history | grep _some ip_&quot; or things like that.<p>Storing the entire history would no doubt be helpful, but I feel similarly to what other people have said so far, which is that it may not be the safest thing to do? Probably depends on usecase.
ojjzhna将近 9 年前
I have bash script in my bash_profile to support separate history files named with tty, month and year (so separate files for each tty). It looks for recent history files for the given tty and reloads old history on first startup.<p>Have cron job to signal bash session to flush history with command timestamps&#x2F;dates to disk daily.<p>Handy for recalling commands&#x2F;command switches you ran months ago.
andy_boot将近 9 年前
I like this idea but I know that I have accidentally entered my password or my password with a typo in it on the terminal on at least one occasion.<p>The only way round this that I can see is to grep out the password - but then my password (or a substring of it) is now in plaintext in my .zshrc&#x2F;.bashrc<p>I can&#x27;t think of a solution to this. Does anyone have anything?
评论 #11811613 未加载
civilian将近 9 年前
I wrote a command called &#x27;lc&#x27;, which list commands in a directory. I&#x27;ve found that what I really want is context-specific command history,and this provides it.<p>Linky: <a href="https:&#x2F;&#x2F;github.com&#x2F;pconerly&#x2F;lc-listcommands-bash" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;pconerly&#x2F;lc-listcommands-bash</a>
tezza将近 9 年前
At work unfortunately the sys admins disable customisation. Especially HIST_SIZE etc :(<p>At regular intervals, especially when I have important commands to save I do a:<p><pre><code> cp ~&#x2F;.bash_history ~&#x2F;bash_history_bak_2016-05-31.txt </code></pre> That way I can do a:<p><pre><code> cat ~&#x2F;bash_history* | grep &lt;important-command&gt; </code></pre> to find it
评论 #11809065 未加载
rbc将近 9 年前
I frequently review my shell history to pickup past command invocations, but I think it has limits.<p>At some point, you have to clean things up and create a script for repetitive tasks, or write wrappers to tame complicated command line syntax. I think curl and groff are my favorite examples of commands that have too many options.
LeonB将近 9 年前
In powershell there is a built in cmdlet for logging your history: start-transcript. Details here: <a href="http:&#x2F;&#x2F;til.secretgeek.net&#x2F;powershell&#x2F;Transcript.html" rel="nofollow">http:&#x2F;&#x2F;til.secretgeek.net&#x2F;powershell&#x2F;Transcript.html</a>
swehner将近 9 年前
Doing the same. Also including the git branch (if applicable)<p>It&#x27;s not obvious, but with this mechanism commands are only logged once they complete. So the log file does not include commands while they are still running. Run into that from time to time.
agumonkey将近 9 年前
I recently had a bash hiccup causing the history to vanish. Not a problem for 90% of things but for a few long rsync commands (the one time crafts for you) it was a PITA. Or an opportunity in RTFM says the noob.
评论 #11831104 未加载
ccvannorman将近 9 年前
with El Capitan, my bash history isn&#x27;t logged, with each fresh session letting me know Permission Denied on creating ~&#x2F;.bash_history. The internet searching tells me to disable csrutil which I did, to no effect.<p>:-[
kayoone将近 9 年前
I use the reverse lookup and zsh history heavily and it has served me well. Whats the benefit of this, logs that go back for a longer time ? I am not sure what the default setting on zsh is tbh.
jldugger将近 9 年前
My job would be substantially harder if I couldn&#x27;t ask bash history the question &quot;how did someone solve this problem last time?&quot;
DoubleMalt将近 9 年前
ctrl-r does a fuzzy search in the history and provides most of what I want out of the box. I wonder why this hadn&#x27;t been mentioned yet.
yoshuaw将近 9 年前
Don&#x27;t go overloading `require()` just because you can - the modules API has been locked down for a couple of years now, messing with the semantics causes all sort of fallout. - <a href="https:&#x2F;&#x2F;nodejs.org&#x2F;api&#x2F;modules.html" rel="nofollow">https:&#x2F;&#x2F;nodejs.org&#x2F;api&#x2F;modules.html</a> (I&#x27;m looking at you webpack, babel)<p>Instead better would&#x27;ve been to use a custom method; internally the `require.resolve()` API can still be used to get the same semantics as `require()`
jl6将近 9 年前
I also use script to log both input and output of every terminal, and the timing option to record the timing of that input and output too. Naturally stored with caution, and with a shortcut to a non-logging terminal for dealing with anything sensitive.
alex_duf将近 9 年前
export HISTFILESIZE=50000<p>export HISTSIZE=50000
评论 #11806852 未加载