Hot answers tagged command-history
58
So, this is all my history-related .bashrc thing:
export HISTCONTROL=ignoredups:erasedups # no duplicate entries
export HISTSIZE=100000 # big big history
export HISTFILESIZE=100000 # big big history
shopt -s histappend # append to history, don't overwrite it
# Save and reload the history after each ...
26
You might want $HISTIGNORE: "A colon-separated list of patterns used to decide which command lines should be saved on the history list." This line in your ~/.bashrc should do the job:
HISTIGNORE='rm *:svn revert*'
Also, you can add a space at the beginning of a command to exclude it from history. This works as long as $HISTCONTROL contains ignorespace or ...
25
Ctrl+R is usually the best way, as descriptor said. You can also use !string, which runs the most recent command starting with string, or !?string?, which runs the most recent command that contains string.
(I think that's the only stuff relevant to this question, but I covered much more of the history commands in this answer)
24
Here is my attempt at Bash session history sharing. This will enable history sharing between bash sessions in a way that the history counter does not get mixed up and history expansion like !number will work with some constraints.
Using Bash version 4.1.5 under Ubuntu 10.04 LTS (Lucid Lynx).
HISTSIZE=9000
HISTFILESIZE=$HISTSIZE
...
24
Another usage is for commands that you don't want to accidentally repeat, such as rm -rf *. I make extensive use of history and occasionally hit Enter accidentally when the command I've retrieved from history is not the one I was looking for. Granted, the real solution is to always read commands carefully before executing them. But being a bit clumbsy, I ...
21
In ZSH:
First set HIST_IGNORE_SPACE in your profile and then prefix the commands you don't want stored with a space.
From the man page, the following 3 options can be used to say that certain lines shouldn't go into the history at all
HIST_IGNORE_SPACE don't store commands prefixed with a space
HIST_NO_STORE don't store history (fc -l) command
...
21
Short answer:
Type this at the prompt:
$ kill -9 $$
This will kill your shell right away without the shell being able to do anything such as trap the signal, save history, execute ~/.bash_logout, warn about stopped jobs, or any of that good stuff.
Long answer:
Note: These options are not mutually exclusive; they can be all used at once.
Option 1:
If ...
18
I find very useful the following readline commands
history-search-backward,
history-search-forward
(be aware they are different from the usual reverse-search-history, forward-search-history, tied to Ctrl-R, Ctrl-S).
I have these commands associated to Ctrl-Up and Ctrl-Down putting the following lines into ~/.inputrc:
"\e[1;5A": history-search-backward
...
17
If you're running the command over and over and your shell is bash, the HISTIGNORE variable will do this. Lets say you have secret.server.com that you ssh to, FTP files to, etc. that you don't want any line that mentions secret.server.com saved:
HISTIGNORE="*secret.server.com*"
You can list multiple patterns with a colon separating them. Make sure sure ...
17
bash actually remembers the times until you close the shell.
So try running
HISTTIMEFORMAT='%x %X ' history
If you also put
HISTTIMEFORMAT=<some format>
in your ~/.bashrc, it will also get written to ~/.bash_history on exit, so you can check what happened in previous shell sessions too.
16
What's going on is that Bash is getting confused about the number of printing characters in your prompt. It sends cursor positioning sequences to the terminal to position the cursor properly for doing command history and such. It needs to have a good idea of where the cursor actually is after printing the prompt.
Try setting your prompt to this:
...
15
Your shell's history is saved in the file indicated by the HISTFILE variable. So:
unset HISTFILE
This also applies to zsh, but not to ksh which keeps saving to the file indicated by $HISTFILE when the shell starts (and conversely, you decide to save your history in ksh once you've started the shell).
14
I'm not aware of any way using bash. But it's one of the most popular features of zsh.
Personally I prefere zsh over bash so I recommend trying it.
Here's the part of my .zshrc that deals with history:
SAVEHIST=10000 # Number of entries
HISTSIZE=10000
HISTFILE=~/.zsh/history # File
setopt APPEND_HISTORY # Don't erase history
setopt EXTENDED_HISTORY # Add ...
13
This is actually a really interesting behavior and I confess I have greatly underestimated the question at the beginning. But first the facts:
1. What works
The functionality can be achieved in several ways, though each works a bit differently. Note that, in each case, to have the history "transfered" to another terminal (updated), one has to press Enter ...
12
In bash, use the HISTCONTROL variable.
Set it to HISTCONTROL=ignorespace (or HISTCONTROL=ignoreboth). From now, when you begin a line with a space and it will not be saved in the history. This avoids to include the not-to-be-disclosed-command in some configuration file.
Even like that it happens to forget to add the space and then want to go back. To ...
11
The a attribute means that the file is append-only: you can't overwrite it or delete it, only append data to it. This is explained in the chattr man page. Only root can remove the attribute.
The practical consequence is that you can't erase your old history lines. This is presumably intended as a security measure by your system administrator. I'm not ...
11
If it refers to commands run just recently, a more efficient way is to reference them with negative numbers:
!-4; !-3; !-2; !-1
Also, once you do it, your last history entry will contain the whole chain of commands, so you can repeat it with !!.
Edit:
If you haven't already, get familiar with the great builtin function fc, mentioned by Gilles. (Use ...
11
You can access this via the forward-search-history function which is bind per default to ctrl+s. Unfortunately ctrl+s is used to signal xoff per default which means you can't use it to change the direction of the search. There are two solutions for solving the problem, one disabling sending the xoff/xon signaling and the other change the keybinding for ...
10
! is a feature that originally appeared in the C shell, back in the days before you could count on terminals to have arrow keys. It's especially useful if you add the current number to the prompt (PS1="\!$ ") so you can quickly look at your screen to get numbers for past commands.
Now that you can use arrow keys and things like Ctrl-R to search the command ...
10
The only way I can think of to do this would be to use history -d in $PROMPT_COMMAND. The problem with this or any approach is that it's impossible to tell if a command exited with an error or completed successfully with a non-zero exit code.
$ grep non_existent_string from_file_that_exists
$ echo $?
1
10
You can run bindkey with no arguments to get a list of existing bindings, eg:
# Enter vi mode
chopper:~> bindkey -v
# Search for history key bindings
chopper:~> bindkey | fgrep history
"^[OA" up-line-or-history
"^[OB" down-line-or-history
"^[[A" up-line-or-history
"^[[B" down-line-or-history
In emacs mode, the binding you want is ...
10
With setopt histignorespace, the command is removed from the current session history. If you tested by pressing Up and seeing that the command line is still there, it's a feature.
Note that the command lingers in the internal history until the next command is entered before it vanishes, allowing you to briefly reuse or edit the line. If you want to ...
9
If your question is about accessing command history, then try this well-named command
history
You can also try Ctrl + r, and start typing a command you're trying to remember that you've recently typed.
(reverse-i-search)`cd ': cd mydir/data/
Hit ESC to select the command or exit. This works for me on SuSE at least; not sure about other distros.
9
To request that the command be printed rather than executed after history substitution, add the :p modifier, e.g. !42:p. The resulting command will also be entered in the history, so you can press Up to edit it.
If you have the histverify option set (shopt -s histverify), you will always have the opportunity to edit the result of history substitutions.
The ...
8
Pressing Ctrl+R will open the reverse history search. Now start typing your command, this will give the first match.
By pressing Ctrl+R again (and again) you can cycle through the history.
mysq(Ctrl+R)
Would give:
mysqldump --add-drop-table -e -q -n -C -u
Ctrl+R again:
mysql -u ben.dauphinee -p
8
It rather depends on what you intend by 'command'. From one perspective a command is simply an executable called from the command line, therefore ls and ls -alh /tmp/foo/bar/*tar.bz are both the command ls. Another perspective is that a command is the sum of its parts, meaning that the above example is 2 distinct commands.
If your intention is to count ...
8
If you are using bash, you can use the fc command to display your history in the way you want:
fc -ln -1
That will print out your last command. -l means list, -n means not to prefix lines with command numbers and -1 says to show just the last command. If the whitespace at the start of the line (only the first line on multi-line commands) is bothersome, ...
8
I don't think you really want that. My usual workflow goes like this:
Type a command
Run it
Notice it failing
Press UP key
Edit the command
Run it again
Now, if the failed command weren't saved into history I couldn't get it easily back to fix and run again.
8
The history command just operates on your history file, $HISTFILE (typically ~/.history or ~/.bash_history). It'll be much easier if you just remove the lines from that file, which can be done many ways. grep is one way, but you have to be careful not to overwrite the file while still reading it:
$ grep -v searchstring "$HISTFILE" > /tmp/history
$ mv ...
8
In:
cat */* > bigtextfile.txt
The shell will expand */* into the sorted list of (non-hidden) matching files, and will execute cat with those file paths as arguments.
cat will open each file in turn and write on its stdout what it reads from the file. cat will not hold more than one buffer full of data (something like a few kilo bytes) at a time in ...
Only top voted, non community-wiki answers of a minimum length are eligible

