How can I point the 'history' command to an alternate file? - linux

On my development machine, I have a number of different environments, each of which maintaining an own history file with a dedicated name. This is a customer requirement which I can not change.
If I have activated one of these environments, a 'history' command will always list the contents of the current environments history-file which is ok.
However, if I want to look up something in the history of another environment, I can only do this by opening a new shell and loading the respective environment. But what I want to do is to tell 'history' from which file it should read it's input without opening a new session and loading the environment in question.
Just 'grep'ing the alternative history-file is not a satisfying option as I want the entire output (e.g. timestamps) to be formatted properly.
The question is: How can I point 'history' to process an alternative file? Trying with
$ HISTFILE=/path/to/other-histfile history
didn't help, nor did
$ export HISTFILE=/path/to/other-histfile; history
I could write a perl script to do produce the oputput I want. But perhaps there is a way to do this out of the box?
EDIT: An environment is activated by a script which sources all settings which are relevant for the history processing.
useEnv() is an alias which basically looks like this:
useEnv()
{
export HISTFILE=/some/directory/.bash_history.$ENVNAME
PROMPT_COMMAND="history -a; $PROMPT_COMMAND"
export HISTCONTROL=ignoreboth
}
EDIT, SOLUTION: KamilCuk had the solution which works for me, see his answer below. Here is my summary:
You can write an alias or a script which first clears [-c] the history list (but leaves $HISTFILE untouched), and then reads [-r] the alternative history file.
Upon the next history call the content of the alternative file is displayed.
Then, clear the history again and read back $HISTFILE
like so:
$ history -c -r /path/to/otherfile; history; history -c -r $HISTFILE

HISTFILE is the file where the command history is saved (and read on startup, and also it's the default for history command when no filename argument is given, but mostly it's for saving).
Clear current history list and then read another history file to the current history list:
history -c
history -r /path/to/other-histfile
You might want to modify HISTFILE too, so it does not get saved.

Related

Where are all the locations where command history is saved on linux server

I know that the history can be seen by just running the command 'history' but I am wondering where else commands may be stored.
You can check the location of the (only) history file of your shell using echo $HISTFILE. Most likely, this is what you are looking for.
Single commands may store additional information. For example, sudo may store the commands executed as root in the system log. If you run the mysql CLI, it would store its history (by default) in ~/.mysql_history.
By default it's stored in ~/.bash_history
Command history is a shell-specific feature stored on a per-user basis. The history command displays what the current instance has in memory.
The history command takes options -r, -w, and -a to read from, write to, or append to (respectively) a default history file, which is ~/.bash_history or the name stored in the HISTFILE variable.
Generally speaking, though, the shell could be configured to read and write history to an arbitrary file; there's no way to know where history could be stored without being familiar with a particular user's shell configuration files or scripts.

Save Directory Specific history for zshell

I am trying to save my directory specific command line history. AKA, when I am in a directory called Happy, and I arrow up, I don't want to see all of my past command line history, I only want to see the history that was done in the specific Happy directory.
I also want all of this to get saved to a file so that I can access it later. Perhaps save it to files organized by directory, or by date, I don't know what would be best. Does anyone have anything like this set up?
Thank you!
It sounds like you could use this script on GitHub. Excerpt from README:
Per-Directory-History
Per directory history for zsh, as well as global history, and the
ability to toggle between them with ^G.
This is a implementation of per directory history for zsh, some
implementations of which exist in bash. It also implements
a per-directory-history-toggle-history function to change from using
the directory history to using the global history. In both cases the
history is always saved to both the global history and the directory
history, so the toggle state will not effect the saved histories.
Being able to switch between global and directory histories on the
fly is a novel feature as far as I am aware.
This is a standalone repository for the script, however it is also
included in oh-my-zsh as a plugin.

One-way diff file

I would like to generate diffs for the sake of doing incremental backups of an sql database.
Using the standard unix 'diff' tool generates unnecessarily large files, since they include the full text of deleted lines. I only need support to be able to patch in one direction (to generate the current db dump from the full dump and an incremental patch).
How would I go about doing this? I have tried so far using diff -e and patch -e, but it doesn't seem to be working correctly, as the resulting file is corrupt (possibly an issue with the 'ed' tool used in cygwin)
back in the old days, before Vim, there used to be a line-oriented UNIX editor called 'ed' ..
diff has an option built in ( -e option ) , with which you can create an edit script from the diff.
Check here: and look for the section "Edit Script"
http://en.wikipedia.org/wiki/Diff
http://docs.freebsd.org/info/diff/diff.info.ed_Scripts.html
here's an example:
http://www.araxis.com/merge/topic_diff_report_editscript.html
another way to do this is to create a patch file (see 'man patch')

How do I get GNU screen to read .bash_profile/.bash_rc changes?

After I make changes in .bash_rc or .bash_profile, when I start GNU screen, it doesn't recognize those changes.
I can
source ~/.bash_profile
and it works for the current screen window I have open, but I have to do that for every screen window I have open.
How do I get screen to read my latest changes in my bash configuration?
If you want screen to always treat your shell as a login shell, and source the same files that would be read if just started a new shell normally, add the following to ~/.screenrc (or maybe ~/.byobu/.screenrc, as pointed out in the comment):
shell -$SHELL
This way, you don't need to manually tell it to source your files each time you start a new screen. Though you would have to if you just made changes and wanted those changes to be reflected in your current screen.
The documentation for this (and lots of other screen details) can be found here. Basically, shell is a command to screen telling it to run the following when it needs to create a new shell. $SHELL is the usual variable holding the path to your preferred shell. And the dash - in front of $SHELL indicates that it should be run as a login shell (which will typically mean it sources your ~/.bash_profile, etc.).
It's worth pointing out, however, that screen defaults to just inheriting most environment variables from the shell where you start screen; and a login sub-shell may alter some environment variables in unexpected ways. I ran into a situation where elements of my $PATH were basically permuted. I solved the problem thanks to this particularly excellent answer on superuser.
You may notice the source command available. It's important to note that this sources a file of screen commands, rather than shell commands. Other relevant (screen) commands include eval and exec.
You have to do it in each screen that you have open since they are all different shells. If you need the change every time a new shell is opened, I suggest you put the changes in ~/.bashrc instead.
Apparently, you can send a command to all windows at once using this syntax:
C-a :
at "#" stuff "source ~/.bash_profile^M"

Keeping track of history of commands of multiple sessions of ksh

I am using multiple sessions of ksh on a linux machine. On one terminal when I do history, I only see history of commands typed on that terminal.
When I log out all sessions, I want to make sure that history of commands typed on all the terminal get appended on one file say .bash_history. Is it possible?
Even when I don't log out, are all these commands dynamically get appended to this history file?
I am wary of losing my commands like when someone helps me do something, I want to make sure that I can refer to that command later on.
How do I set the size of .bash_history file
Thanks,
HISTSIZE will set the size of your history, default is 512; HISTFILE should be set to the name of your history file, default is ~/.sh_history.
I suppose you could set HISTFILE to ~/.bash_history, but that might be confusing later on.
From what I can see of the docs on ksh, it should be merging your history from multiple invocations. Try opening three separate sessions and issuing a distinct different command on each then logging out. Open a new instance of your term and view your history file - are they all there?
Tested out ok under ksh on Mac OS X

Resources