linux history command - linux

If I type history command, I can view only 1000 lines. I want to view more than that. It will be nice If I can view all commands typed between certain dates or months.
Is this possible? If so, how to do this?

You want to set the bash variables HISTSIZE and HISTFILESIZE in your .bashrc.
HISTSIZE determines how many comands are retained in history. HISTFILESIZE determines how many lines of history are stored in the file in which they are persisted, usually .bash_history.

add some commands to .bash_logout to organize your history files
e.g.
today=`date +%y%m%d`
cp .bash_history >.bash_history_$today
echo >.bash_history # to clear out file
However, one problem: .bash_logout doesn't seem to run in a lot of X-based environments. On a ssh account, such as with a virtual server, it works fine.

Related

Save history for a specific command elsewhere than .bash_history

By default, command history is stored in ~/.bash_history file.
I want to save the history for a specific command elsewhere than ~/.bash_history every time the command is called, like in a file ~/.specific-command-history.
I don't want to run grep to search for a specific command neither exclude it from ~/.bash_history, I just wanted to have a history for a specific command to be automatically stored in other file as well as in ~/.bash_history.
Any thoughts about that?
You could create and run an anacron script, even if at a set time the server was switched off, when the server is switched on, anacron will run the script.
The anacron script would grep the file with the command you are looking for, and then use cut, and then append the file you would like to paste the lines into.

Show all sourced files in ZSH

I want to see which files got sourced while starting the Z-shell. Does it even keep track and if yes is there a way to list all the files?
You could do a
setopt SOURCE_TRACE
at the beginning of the very first file which gets sourced. See man zshoptions.
To understand exactly how zsh configuration files are read and in what order, you must read the "STARTUP/SHUTDOWN FILES" section of man zsh.
Here are the most relevant excerpts:
Commands are first read from /etc/zshenv [...].
Commands are then read from $ZDOTDIR/.zshenv. If the shell is a login shell, commands are read from /etc/zprofile and then $ZDOTDIR/.zprofile. Then,if the shell is interactive, commands are read from /etc/zshrc and then $ZDOTDIR/.zshrc. Finally, if the shell is a login shell, /etc/zlogin and $ZDOTDIR/.zlogin are read.
[...]
If ZDOTDIR is unset, HOME is used instead. Files listed above as being in /etc may be in another directory, depending on the installation.
As that last note suggest, on some distributions, the system-wide configuration files listed above may be in /etc/zsh/ instead of directly in /etc/.

Bash History Scope

Defined in .bashrc
function gitpullsite(){
echo "Enter GIT username";
read gituname;
echo "Enter GIT password";
read -s gitpword;
giturl=https://$gituname:$gitpword#github.com/whateveruser/whateverrepo.git
repopath=/home/whateveruser/html/whateverrepo/;
sudo rm -r $repopath;
sudo git clone $giturl $repopath;
}
and then run in terminal gitpullsite I want to remove gituname and gitpword from history but I cannot find such entries in history anywhere. I have no process in place to remove such entries so where is this history stored ? Is it ignored as within the scope of the function and not the shell ?
What I don't want is an unknown trail of usernames and passwords stored somewhere I don't know about - an obvious security issue should a system be compromised.
The aim is to add further different 'nested' repos but the user only needs to enter credentials once.
You may want to try https://github.com/dvorka/hstr which, in addition to history management i.e. deleting particular command(s) from history, allows for "suggest box style" filtering.
It can be easily bound to Ctrl-r and/or Ctrl-s
One possible solution would be to not log part of your commands at all.
Multiple ways to do so :
1
To stop logging bash history is:
set +o history
and to reset, that is to start logging again:
set -o history
2
You can also use add ignorespace to your HISTCONTROL environment variable. Then any command line that begins with a space won't be entered into your history.
methods for avoiding bash history logging
Can you prevent a command from going into the bash shell command history?

combine history across tty

At any given time I have 3 logins to the same server. Sure, I often use screen, but assume this purpose allows me to have:
1 session running something
1 session looking at output
1 session shuffling files to/from various places
If at any point I lose all three connections, I lose 2/3 of my history, as when I log back in, I get the history from a random 1 of my three connections.
Is there a way to stop this and combine all three history files into 1?
Alternatively, is there a way to declare each login as "ttyN" thus keeping each with its separate history separate, but retrievable / re-connectable?
Thanks!
Possible solution?
After you have opened your Terminal /Screen
start your shell with a History-File setting:
HISTFILE=$HOME/session1-history bash
and continue to work with this bash.
To run a different session history
HISTFILE=$HOME/session2-history bash
etc.
Just add this in your .bashrc file.
# Avoid duplicates..
export HISTCONTROL=ignoredups:erasedups
# Append history entries..
shopt -s histappend
# After each command, save and reload history
export PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND$'\n'}history -a; history -c; history -r"
Found this answer at this Unix Stackexchange post.
I tried this and it seems to work on multiple terminals simultaneously, only catch is you've to execute a command for the terminal to fetch the latest history from .bash_history.
So for example, if I open the first terminal and type echo check 1 and then open a second terminal and type echo check 2. Now if I go back to the first terminal and press the up key, I won't get echo check 2, since the last time the history was fetched from the history file was when I executed the previous command. So I can just press Enter without specifying a command and it will fetch the last history entries. Now if I press the up key, it will show up echo check 2, which is the expected behavior.
There is an open source shell history logger for bash and zsh which would solve your problem (disclaimer: I wrote and maintain it).
https://github.com/barabo/advanced-shell-history
The idea is that your commands are written into a sqlite3 database using a builtin bash hook. If you really needed to go back and figure out what commands you entered / how long they ran / what their exit codes were - you should give it a try.
It's saved me many times.
There are numerous options you can set to prevent Bash instances from overwriting each others history. See this FAQ for a full explanation:
In your .bashrc, add
HISTFILESIZE=400000000
HISTSIZE=10000
PROMPT_COMMAND="history -a"
export HISTSIZE PROMPT_COMMAND
shopt -s histappend
If I've understood your requirements right you have multiple terminal sessions open and want commands entered in one session to be available via history in other sessions.
You need to put these lines on top of your ~/.bashrc:
export HISTFILESIZE=100000
export HISTSIZE=100000
# avoid & erase dups
export HISTCONTROL=ignoredups:erasedups
shopt -s histappend
# save & reload history after each command enter in shell
export PROMPT_COMMAND="\history -a; \history -c; \history -r; $PROMPT_COMMAND"
Most important is histappend which makes sure that new history lines are appended to the history file each time.
PS: You need to press enter to see most recently added history in each session.
For more robust solution see: http://ptspts.blogspot.in/2011/03/how-to-automatically-synchronize-shell.html

Retrieving command line history

I use ubuntu 11.04, and the question must be common to any bash shell. Pressing the up arrow key on your terminal will retrieve the previous command you had executed at your terminal.
My question is where(in which file) will all these command history be stored? Can I read that file?
the history filename was stored in variable : $HISTFILE
echo $HISTFILE
will give you the right file.
Usually in bash it would be ~/.bash_history, however it could be changed by configuration.
also notice that sometimes the very last commands is not stored in that file. running
history -a
will persistent.
history -r
will clean those command not yet written to the file.
For bash, it is by default in ~/.bash_history (check the HISTFILE environment variable if it isn't). You can directly cat the file or use the history command.

Resources