1

I have a lot a commands that I want to run again, but there are a lot of them, and going up to them with the up arrow key is too long. Is it possible to get it in a cat/tail command and pipe it to a file ?

2
  • You don't need the up arrow. You can also just type Ctrl-r and then start typing one of your previous commands. Then type Ctrl-r repeatedly to cycle through a list of commands that match.
    – zedoo
    Feb 2, 2013 at 10:07
  • It is stored in ~/.bash_history See also stackoverflow.com/questions/8168676/… Feb 2, 2013 at 10:23

3 Answers 3

3

You can use the history command:

history > my_file
1
  • FYI, the history command will output the contents of the .bash_history file, plus recently executed commands not yet stored in that file.
    – chepner
    Feb 2, 2013 at 13:31
1

Type Ctrl-R and then type the first few letters of the command.

1

Adding this to ~/.inputrc would make the up and down arrows cycle through commands that match the start of the line:

"\e[A": history-search-backward
"\e[B": history-search-forward

I also use these functions a lot:

h() {
    if [ $# = 0 ]; then
        history 33 | sed '$d'
    else
        history | grep -iEF "$*" | grep -v '^ *[0-9]* *h '
    fi
}

r() {
    history ${1-200} | sed 's/^ *[0-9]* *//' | tail -r > /tmp/recent
    open /tmp/recent -e
}

If you add " ": magic-space to .inputrc, you can insert command 1234 by typing !1234 and a space. But it also makes space stop work in irb and the interactive mode in gnuplot.

1
  • Very interesting !
    – epsilones
    Feb 3, 2013 at 15:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .