Create history log per working directory in bash Announcing the arrival of Valued Associate...

Dating a Former Employee

How to answer "Have you ever been terminated?"

What does an IRS interview request entail when called in to verify expenses for a sole proprietor small business?

What exactly is a "Meth" in Altered Carbon?

Why do we bend a book to keep it straight?

Overriding an object in memory with placement new

2001: A Space Odyssey's use of the song "Daisy Bell" (Bicycle Built for Two); life imitates art or vice-versa?

In predicate logic, does existential quantification (∃) include universal quantification (∀), i.e. can 'some' imply 'all'?

Using et al. for a last / senior author rather than for a first author

Why do people hide their license plates in the EU?

Can a non-EU citizen traveling with me come with me through the EU passport line?

Echoing a tail command produces unexpected output?

Short Story with Cinderella as a Voo-doo Witch

porting install scripts : can rpm replace apt?

Book where humans were engineered with genes from animal species to survive hostile planets

Fundamental Solution of the Pell Equation

The logistics of corpse disposal

Use BFD on a Virtual-Template Interface

What does this icon in iOS Stardew Valley mean?

At the end of Thor: Ragnarok why don't the Asgardians turn and head for the Bifrost as per their original plan?

Single word antonym of "flightless"

How to run gsettings for another user Ubuntu 18.04.2 LTS

How to deal with a team lead who never gives me credit?

Identifying polygons that intersect with another layer using QGIS?



Create history log per working directory in bash



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
2019 Community Moderator Election Results
Why I closed the “Why is Kali so hard” questionExecute bash scripts on entering a directoryBash history: “ignoredups” and “erasedups” setting conflict with common history across sessionsHow to preserve bash history in multiple terminal windowsAutomatic cleanup of Bash historyBash history with timestampsPer-directory history in zshbash history for current sessionGet checksum of directory on bashMirror Bash historyWhen is a multiline history entry (aka lithist) in bash possible?Viewing bash history of separate active TTY





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







3















I was wondering if it is possible to keep a file containing history per current working directory. So for example if I was working in /user/temp/1/ and typed a few commands, these commands would be saved in /user/temp/1/.his or something. I am using bash.










share|improve this question































    3















    I was wondering if it is possible to keep a file containing history per current working directory. So for example if I was working in /user/temp/1/ and typed a few commands, these commands would be saved in /user/temp/1/.his or something. I am using bash.










    share|improve this question



























      3












      3








      3


      1






      I was wondering if it is possible to keep a file containing history per current working directory. So for example if I was working in /user/temp/1/ and typed a few commands, these commands would be saved in /user/temp/1/.his or something. I am using bash.










      share|improve this question
















      I was wondering if it is possible to keep a file containing history per current working directory. So for example if I was working in /user/temp/1/ and typed a few commands, these commands would be saved in /user/temp/1/.his or something. I am using bash.







      bash command-history working-directory






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 24 '16 at 22:32









      Gilles

      548k13011131631




      548k13011131631










      asked Aug 24 '16 at 18:37









      tafelplankjetafelplankje

      635




      635






















          3 Answers
          3






          active

          oldest

          votes


















          4














          Building off the answer provided by Groggle, you can create an alternative cd (ch) in your ~/.bash_profile like.



          function ch () {
          cd "$@"
          export HISTFILE="$(pwd)/.bash_history"
          }


          automatically exporting the new HISTFILE value each time ch is called.



          The default behavior in bash only updates your history when you end a terminal session, so this alone will simply save the history to a .bash_history file in whichever folder you happen to end your session from. A solution is mentioned in this post and detailed on this page, allowing you to update your HISTFILE in realtime.



          A complete solution consists of adding two more lines to your ~/.bash_profile,



          shopt -s histappend
          PROMPT_COMMAND="history -a;$PROMPT_COMMAND"


          changing the history mode to append with the first line and then configuring the history command to run at each prompt.






          share|improve this answer





















          • 1





            You should note that this configuration is highly dangerous. Sooner or later you're going to leak some confidential commands in a publicly exposed archive or repository. On a multiuser machine, as soon as you change into another user's directory and run a few commands there, they can modify your files and probably gain a backdoor to your account. And other users, or people whose archives you download, can easily trick you into running commands by injecting them into your shell history.

            – Gilles
            Aug 24 '16 at 23:15













          • Great, I didn't think about that, thanks very much.

            – tafelplankje
            Aug 25 '16 at 13:24



















          2














          You can set up code to be executed when changing directories. In zsh, this is as simple as defining a function and adding it to the chpwd_functions array. In bash this takes a little more work but you can do it as well; see Execute bash scripts on entering a directory for how to define a chpwd function that is executed on each directory change.



          For what you want to do, the chpwd function needs to write the current history to the old file. To save the history to the right place when exiting bash, set the HISTFILE variable to the history file for the new directory, and read the new history.



          chpwd () {
          history -a
          mkdir -p "$HOME/.bash_history.d/${PWD%/*}"
          HISTFILE=~/.bash_history.d/$PWD
          history -r
          }


          Note that I write the history to a hierarchy under your home directory, not to the current directory. Writing shell history to the current directory would be extremely dangerous and disruptive: you'd end up with privacy leaks all over the place, you'd risk modifying unintended files (what if the history file name is an existing symlink to some other place?), etc.



          It would probably be better to write out the history after each command. In bash, you can enable this with the histappend option. With this option, you don't need to run history -a explicitly.



          shopt -s histappend
          chpwd () {
          mkdir -p "$HOME/.bash_history.d/${PWD%/*}"
          HISTFILE=~/.bash_history.d/$PWD
          history -r
          }





          share|improve this answer

































            0














            Depending on how frequently you change directories my solution might not work. You can export the HISTFILE which sets where bash history is stored, so the idea is that you can make separate profiles in the directories that you're working in and then automatically load them using something like direnv.



            HISTFILE="$(pwd).bash_history"


            Direnv
            https://github.com/direnv/direnv






            share|improve this answer


























              Your Answer








              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "106"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f305524%2fcreate-history-log-per-working-directory-in-bash%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              4














              Building off the answer provided by Groggle, you can create an alternative cd (ch) in your ~/.bash_profile like.



              function ch () {
              cd "$@"
              export HISTFILE="$(pwd)/.bash_history"
              }


              automatically exporting the new HISTFILE value each time ch is called.



              The default behavior in bash only updates your history when you end a terminal session, so this alone will simply save the history to a .bash_history file in whichever folder you happen to end your session from. A solution is mentioned in this post and detailed on this page, allowing you to update your HISTFILE in realtime.



              A complete solution consists of adding two more lines to your ~/.bash_profile,



              shopt -s histappend
              PROMPT_COMMAND="history -a;$PROMPT_COMMAND"


              changing the history mode to append with the first line and then configuring the history command to run at each prompt.






              share|improve this answer





















              • 1





                You should note that this configuration is highly dangerous. Sooner or later you're going to leak some confidential commands in a publicly exposed archive or repository. On a multiuser machine, as soon as you change into another user's directory and run a few commands there, they can modify your files and probably gain a backdoor to your account. And other users, or people whose archives you download, can easily trick you into running commands by injecting them into your shell history.

                – Gilles
                Aug 24 '16 at 23:15













              • Great, I didn't think about that, thanks very much.

                – tafelplankje
                Aug 25 '16 at 13:24
















              4














              Building off the answer provided by Groggle, you can create an alternative cd (ch) in your ~/.bash_profile like.



              function ch () {
              cd "$@"
              export HISTFILE="$(pwd)/.bash_history"
              }


              automatically exporting the new HISTFILE value each time ch is called.



              The default behavior in bash only updates your history when you end a terminal session, so this alone will simply save the history to a .bash_history file in whichever folder you happen to end your session from. A solution is mentioned in this post and detailed on this page, allowing you to update your HISTFILE in realtime.



              A complete solution consists of adding two more lines to your ~/.bash_profile,



              shopt -s histappend
              PROMPT_COMMAND="history -a;$PROMPT_COMMAND"


              changing the history mode to append with the first line and then configuring the history command to run at each prompt.






              share|improve this answer





















              • 1





                You should note that this configuration is highly dangerous. Sooner or later you're going to leak some confidential commands in a publicly exposed archive or repository. On a multiuser machine, as soon as you change into another user's directory and run a few commands there, they can modify your files and probably gain a backdoor to your account. And other users, or people whose archives you download, can easily trick you into running commands by injecting them into your shell history.

                – Gilles
                Aug 24 '16 at 23:15













              • Great, I didn't think about that, thanks very much.

                – tafelplankje
                Aug 25 '16 at 13:24














              4












              4








              4







              Building off the answer provided by Groggle, you can create an alternative cd (ch) in your ~/.bash_profile like.



              function ch () {
              cd "$@"
              export HISTFILE="$(pwd)/.bash_history"
              }


              automatically exporting the new HISTFILE value each time ch is called.



              The default behavior in bash only updates your history when you end a terminal session, so this alone will simply save the history to a .bash_history file in whichever folder you happen to end your session from. A solution is mentioned in this post and detailed on this page, allowing you to update your HISTFILE in realtime.



              A complete solution consists of adding two more lines to your ~/.bash_profile,



              shopt -s histappend
              PROMPT_COMMAND="history -a;$PROMPT_COMMAND"


              changing the history mode to append with the first line and then configuring the history command to run at each prompt.






              share|improve this answer















              Building off the answer provided by Groggle, you can create an alternative cd (ch) in your ~/.bash_profile like.



              function ch () {
              cd "$@"
              export HISTFILE="$(pwd)/.bash_history"
              }


              automatically exporting the new HISTFILE value each time ch is called.



              The default behavior in bash only updates your history when you end a terminal session, so this alone will simply save the history to a .bash_history file in whichever folder you happen to end your session from. A solution is mentioned in this post and detailed on this page, allowing you to update your HISTFILE in realtime.



              A complete solution consists of adding two more lines to your ~/.bash_profile,



              shopt -s histappend
              PROMPT_COMMAND="history -a;$PROMPT_COMMAND"


              changing the history mode to append with the first line and then configuring the history command to run at each prompt.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Apr 13 '17 at 12:37









              Community

              1




              1










              answered Aug 24 '16 at 20:59









              Chuck HorowitzChuck Horowitz

              564




              564








              • 1





                You should note that this configuration is highly dangerous. Sooner or later you're going to leak some confidential commands in a publicly exposed archive or repository. On a multiuser machine, as soon as you change into another user's directory and run a few commands there, they can modify your files and probably gain a backdoor to your account. And other users, or people whose archives you download, can easily trick you into running commands by injecting them into your shell history.

                – Gilles
                Aug 24 '16 at 23:15













              • Great, I didn't think about that, thanks very much.

                – tafelplankje
                Aug 25 '16 at 13:24














              • 1





                You should note that this configuration is highly dangerous. Sooner or later you're going to leak some confidential commands in a publicly exposed archive or repository. On a multiuser machine, as soon as you change into another user's directory and run a few commands there, they can modify your files and probably gain a backdoor to your account. And other users, or people whose archives you download, can easily trick you into running commands by injecting them into your shell history.

                – Gilles
                Aug 24 '16 at 23:15













              • Great, I didn't think about that, thanks very much.

                – tafelplankje
                Aug 25 '16 at 13:24








              1




              1





              You should note that this configuration is highly dangerous. Sooner or later you're going to leak some confidential commands in a publicly exposed archive or repository. On a multiuser machine, as soon as you change into another user's directory and run a few commands there, they can modify your files and probably gain a backdoor to your account. And other users, or people whose archives you download, can easily trick you into running commands by injecting them into your shell history.

              – Gilles
              Aug 24 '16 at 23:15







              You should note that this configuration is highly dangerous. Sooner or later you're going to leak some confidential commands in a publicly exposed archive or repository. On a multiuser machine, as soon as you change into another user's directory and run a few commands there, they can modify your files and probably gain a backdoor to your account. And other users, or people whose archives you download, can easily trick you into running commands by injecting them into your shell history.

              – Gilles
              Aug 24 '16 at 23:15















              Great, I didn't think about that, thanks very much.

              – tafelplankje
              Aug 25 '16 at 13:24





              Great, I didn't think about that, thanks very much.

              – tafelplankje
              Aug 25 '16 at 13:24













              2














              You can set up code to be executed when changing directories. In zsh, this is as simple as defining a function and adding it to the chpwd_functions array. In bash this takes a little more work but you can do it as well; see Execute bash scripts on entering a directory for how to define a chpwd function that is executed on each directory change.



              For what you want to do, the chpwd function needs to write the current history to the old file. To save the history to the right place when exiting bash, set the HISTFILE variable to the history file for the new directory, and read the new history.



              chpwd () {
              history -a
              mkdir -p "$HOME/.bash_history.d/${PWD%/*}"
              HISTFILE=~/.bash_history.d/$PWD
              history -r
              }


              Note that I write the history to a hierarchy under your home directory, not to the current directory. Writing shell history to the current directory would be extremely dangerous and disruptive: you'd end up with privacy leaks all over the place, you'd risk modifying unintended files (what if the history file name is an existing symlink to some other place?), etc.



              It would probably be better to write out the history after each command. In bash, you can enable this with the histappend option. With this option, you don't need to run history -a explicitly.



              shopt -s histappend
              chpwd () {
              mkdir -p "$HOME/.bash_history.d/${PWD%/*}"
              HISTFILE=~/.bash_history.d/$PWD
              history -r
              }





              share|improve this answer






























                2














                You can set up code to be executed when changing directories. In zsh, this is as simple as defining a function and adding it to the chpwd_functions array. In bash this takes a little more work but you can do it as well; see Execute bash scripts on entering a directory for how to define a chpwd function that is executed on each directory change.



                For what you want to do, the chpwd function needs to write the current history to the old file. To save the history to the right place when exiting bash, set the HISTFILE variable to the history file for the new directory, and read the new history.



                chpwd () {
                history -a
                mkdir -p "$HOME/.bash_history.d/${PWD%/*}"
                HISTFILE=~/.bash_history.d/$PWD
                history -r
                }


                Note that I write the history to a hierarchy under your home directory, not to the current directory. Writing shell history to the current directory would be extremely dangerous and disruptive: you'd end up with privacy leaks all over the place, you'd risk modifying unintended files (what if the history file name is an existing symlink to some other place?), etc.



                It would probably be better to write out the history after each command. In bash, you can enable this with the histappend option. With this option, you don't need to run history -a explicitly.



                shopt -s histappend
                chpwd () {
                mkdir -p "$HOME/.bash_history.d/${PWD%/*}"
                HISTFILE=~/.bash_history.d/$PWD
                history -r
                }





                share|improve this answer




























                  2












                  2








                  2







                  You can set up code to be executed when changing directories. In zsh, this is as simple as defining a function and adding it to the chpwd_functions array. In bash this takes a little more work but you can do it as well; see Execute bash scripts on entering a directory for how to define a chpwd function that is executed on each directory change.



                  For what you want to do, the chpwd function needs to write the current history to the old file. To save the history to the right place when exiting bash, set the HISTFILE variable to the history file for the new directory, and read the new history.



                  chpwd () {
                  history -a
                  mkdir -p "$HOME/.bash_history.d/${PWD%/*}"
                  HISTFILE=~/.bash_history.d/$PWD
                  history -r
                  }


                  Note that I write the history to a hierarchy under your home directory, not to the current directory. Writing shell history to the current directory would be extremely dangerous and disruptive: you'd end up with privacy leaks all over the place, you'd risk modifying unintended files (what if the history file name is an existing symlink to some other place?), etc.



                  It would probably be better to write out the history after each command. In bash, you can enable this with the histappend option. With this option, you don't need to run history -a explicitly.



                  shopt -s histappend
                  chpwd () {
                  mkdir -p "$HOME/.bash_history.d/${PWD%/*}"
                  HISTFILE=~/.bash_history.d/$PWD
                  history -r
                  }





                  share|improve this answer















                  You can set up code to be executed when changing directories. In zsh, this is as simple as defining a function and adding it to the chpwd_functions array. In bash this takes a little more work but you can do it as well; see Execute bash scripts on entering a directory for how to define a chpwd function that is executed on each directory change.



                  For what you want to do, the chpwd function needs to write the current history to the old file. To save the history to the right place when exiting bash, set the HISTFILE variable to the history file for the new directory, and read the new history.



                  chpwd () {
                  history -a
                  mkdir -p "$HOME/.bash_history.d/${PWD%/*}"
                  HISTFILE=~/.bash_history.d/$PWD
                  history -r
                  }


                  Note that I write the history to a hierarchy under your home directory, not to the current directory. Writing shell history to the current directory would be extremely dangerous and disruptive: you'd end up with privacy leaks all over the place, you'd risk modifying unintended files (what if the history file name is an existing symlink to some other place?), etc.



                  It would probably be better to write out the history after each command. In bash, you can enable this with the histappend option. With this option, you don't need to run history -a explicitly.



                  shopt -s histappend
                  chpwd () {
                  mkdir -p "$HOME/.bash_history.d/${PWD%/*}"
                  HISTFILE=~/.bash_history.d/$PWD
                  history -r
                  }






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Apr 13 '17 at 12:37









                  Community

                  1




                  1










                  answered Aug 24 '16 at 23:13









                  GillesGilles

                  548k13011131631




                  548k13011131631























                      0














                      Depending on how frequently you change directories my solution might not work. You can export the HISTFILE which sets where bash history is stored, so the idea is that you can make separate profiles in the directories that you're working in and then automatically load them using something like direnv.



                      HISTFILE="$(pwd).bash_history"


                      Direnv
                      https://github.com/direnv/direnv






                      share|improve this answer






























                        0














                        Depending on how frequently you change directories my solution might not work. You can export the HISTFILE which sets where bash history is stored, so the idea is that you can make separate profiles in the directories that you're working in and then automatically load them using something like direnv.



                        HISTFILE="$(pwd).bash_history"


                        Direnv
                        https://github.com/direnv/direnv






                        share|improve this answer




























                          0












                          0








                          0







                          Depending on how frequently you change directories my solution might not work. You can export the HISTFILE which sets where bash history is stored, so the idea is that you can make separate profiles in the directories that you're working in and then automatically load them using something like direnv.



                          HISTFILE="$(pwd).bash_history"


                          Direnv
                          https://github.com/direnv/direnv






                          share|improve this answer















                          Depending on how frequently you change directories my solution might not work. You can export the HISTFILE which sets where bash history is stored, so the idea is that you can make separate profiles in the directories that you're working in and then automatically load them using something like direnv.



                          HISTFILE="$(pwd).bash_history"


                          Direnv
                          https://github.com/direnv/direnv







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited 5 hours ago









                          Rui F Ribeiro

                          42.1k1484142




                          42.1k1484142










                          answered Aug 24 '16 at 18:53









                          GrogglerGroggler

                          1




                          1






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Unix & Linux Stack Exchange!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f305524%2fcreate-history-log-per-working-directory-in-bash%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Hudson River Historic District Contents Geography History The district today Aesthetics Cultural...

                              The number designs the writing. Feandra Aversely Definition: The act of ingrafting a sprig or shoot of one...

                              Ayherre Geografie Demografie Externe links Navigatiemenu43° 23′ NB, 1° 15′ WL43° 23′ NB, 1°...