Is is possible to have the history of a specific user in one more file other than the default file mentioned as HISTFILE?
I would like to have as backup file if main file was removed and let it be like a backup for that one.
Regards,
Sriharsha Kalluru.
You can create a hardlink to the file
cp --link --verbose /home/$USER/.bash_history /somewhere/else/users_history
When the original file in his home is removed the file is still there and preserves the content from being lost.
Many times I've found myself using Ctrl-R in Bash to get a old command four times the terminal width, just to find out that too many days have passed and it's no longer in the .bash_history file. Here are two lines that will keep track of every command line you type at the bash prompt and use no external processes at all, just plain bash.
My first approach to this problem was increasing the maximum number of lines in the history to a very large quantity. But no matter how large it was, there was always a moment when I needed a long command I typed many months ago and it had already left the history. The current solution came to my mind when I learned about the PROMPT_COMMAND variable, a command that bash executes before showing each prompt. Here are the two lines:
export HISTTIMEFORMAT="%s "
PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND ; }"'echo $$ $USER \
"$(history 1)" >> ~/.bash_eternal_history'
One goal I set to myself was to achieve it without using any external process, so bash wouldn't have to fork a new process after every ENTER pressed at its prompt. The first line sets the format of history lines to include the date as a Unix timestamp, so you can now when you typed every command. The second line, which is the core of the solution, first ensures that, if a previous PROMPT_COMMAND was set, it gets executed before our stuff and then appends a line of the format:
PID USER INDEX TIMESTAMP COMMAND
to a file called .bash_eternal_history in the current user home.
Adding the username, which at first seemed unnecesary, became useful later to distiguish between "sudo -s" sessions and normal sessions which retain the same value for "~/", and so append lines to the same .bash_eternal_history file.
I hope some of you find these two lines as useful as I do. :-)
Would hard links solve your problem?
Also you can read the man page here.
i would write a cronjob that copies the original histfiel to a backuplocation every minute or so. the you don't have to worry about defining a second histfile.
otherwise you could write every command the user enters to a alternate file
for this approach take a look here:
bash, run some command before or after every command entered from console
Related
Often when I run a command that writes to stdout, and that command fails, I have to scroll up (using uncomfortable key-bindings) looking for the place where I pressed Enter, to see what the first error was (out of hundreds others, across many screens of text). This is both annoying and time-consuming. I wish there was a feature which allowed me to pin my current terminal to the place where I am now, then start the command, see only the first lines of the output (as many as fits below my cursor) and let the rest of the output be written but not displayed. In other words I would like a feature to allow me automatically scroll up to the place where I gave the command, to see the first lines of the output (where usually the origin of the failure is displayed).
I searched for it but I didn't find it. Do you know if such feature exists? Or have an idea how to implement it with some tricks or workarounds?
If you have a unique shell prompt you could bind a key to jump between shell prompts, for example something like this will make C-b S jump to the previous shell prompt then S subsequent ones:
bind S copy-mode \; send -X search-backward 'nicholas#myhost:'
bind -Tcopy-mode S send -X search-backward 'nicholas#myhost:'
Or similarly you could search for error strings if they have a recognisable prefix. If you install the tmux 3.1 release candidate, you can search for regular expressions.
Alternatively, you could use capture-pane to load the entire history into an editor with key bindings you prefer, for example:
$ tmux capturep -S- -E- -p|vim -
Or pipe to grep or whatever. Note you will need to use a temporary file for this to work with emacs.
Or try to get into the habit of teeing commands with lots of output to a file to start with.
I'm trying to make a simple and reliable script, preferably in bash, that is executed every minute using crontab. The script simply has to read the contents of the last couple of lines of an open screen and store them as a var so that I can search for a sub-string. Does anyone know of an easy way to do this, thanks.
You can send the hardcopy command to the screen and read the last line using the tail command:
screen -X hardcopy "~/test.log"
tail -n1 ~/test.log
I am using rlwrap to give command history to sqlplus/rman/etc on a linux server. I can see the command history files such as .sqlplus_history being generated and because these are plain text files that could possible contain passwords, e.g. if a user enters 'connect username/password' inside of sqlplus, there is a security risk to keeping them indefinitely.
Is there an easier way then some kind of cron job that would remove the logs at regular intervals?
I was hoping for some kind of keywords that would trigger the removal of the log such as if the 'exit' or 'quit' commands are read the user leaves sqlplus and the .sqlplus_history log is removed. Is anything like this possible?
rlwrap has many options to keep command lines out of the history list (and hence out of the history file), although not exactly in the way you describe (which I would not find as useful anyway)
rlwrap --forget-matching connect sqlplus will not remember any input lines that contain the string connect
Specifying a negative history size, like rlwrap --histsize -3000 will treat the history file as read-only (the password will still be visible in the history, but it will not be written to a file)
Entering a line with CTRL+O will keep this particular line out of the history list. (This action can be re-bound to another key, see the manpage)
For a really fancy password censor, one could write a filter censor_passwords like this:
#!/usr/bin/env perl
use lib ($ENV{RLWRAP_FILTERDIR} or ".");
use RlwrapFilter;
use strict;
my $filter = new RlwrapFilter;
$filter -> help_text("This filter removes the password from SQL 'identified by' clauses\n");
$filter -> history_handler(sub { s/(identified\s+by\s+)(\S+)/$1 xXxXxXxX/ig; $_});
$filter -> run;
.. and then use it like rlwrap -z censor_passwords sqlplus.
Any input containing IDENTIFIED BY yd6e7#te6 will then be remembered as IDENTIFIED BY xXxXxXxX
I've been using screen for quite some time now and I agree, it improves my productivity.But one thing that I really miss is the command history. Anything I type in a screen session doesn't get logged in command history. When I googled for the same I found something related to this issue:
http://www.linuxquestions.org/questions/slackware-14/aliases-lost-when-using-screen-723624/
But surprisingly in my case all the aliases are intact and I'm able to use them without any issues. As far as I know opening a new screen session actually opens a new sub-shell. If this is true, could someone help me how to get the commands typed in screen session to be logged in the command history so that if I open a new terminal/screen later on I'll be able to access the commands from command history using CTRL+R . Any solution that helps me make screen log commands in command history would be very much helpful. Appreciate your time. Thank you.
Assuming a bash shell is being used within the screen.
Insert the 2 statements into ~/.bashrc:
shopt -s histappend
PROMPT_COMMAND="$PROMPT_COMMAND;history -a"
The first command appends the commands to the history file, rather than overwrite it while the second command saves each command right after it has been executed, not at the end of the session.
To expand on my answer.. the history for each bash session that you have open is stored in memory until you logout/close the session. Then it will overwrite the bash history file.
These commands will append to the history file, and then flush to the file after every command.
It's easy to use shared history between sessions in Zsh, and this blog post by Derek Reeve explains how to do it. In short, add this to your ~/.zshrc:
setopt share_history
HISTSIZE=1000
SAVEHIST=1000
HISTFILE=~/.history
setopt APPEND_HISTORY
I also found instructions for doing the same thing on Bash, but I've only tried this on Zsh.
If you have written a really long command, say cd /very/long/path, and then you do ctrl+c or ctrl+u (if the cursor is at the end), and then you realise that you want the command back, is there any way to get the full line back without re-typing. Is there any trick to change .bashrc so that bash_history keep track of keys pressed on the shell and not just after the enter is hit.
I have answered a question at In bash, how does one clear the current input? and realised if we have some option like this it would be very helpful.
To undo, use either
Ctrl+X, Ctrl+U; or
Ctrl+_ (underscore).
See bind -P for a full list of keybindings in bash.