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;
}
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
add a comment |
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
add a comment |
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
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
bash command-history working-directory
edited Aug 24 '16 at 22:32
Gilles
548k13011131631
548k13011131631
asked Aug 24 '16 at 18:37
tafelplankjetafelplankje
635
635
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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.
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
add a comment |
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
}
add a comment |
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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
}
add a comment |
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
}
add a comment |
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
}
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
}
edited Apr 13 '17 at 12:37
Community♦
1
1
answered Aug 24 '16 at 23:13
GillesGilles
548k13011131631
548k13011131631
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
edited 5 hours ago
Rui F Ribeiro
42.1k1484142
42.1k1484142
answered Aug 24 '16 at 18:53
GrogglerGroggler
1
1
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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