37

I make extensive use of the functions to navigate through the command history in my terminal; I wonder if there is some way to navigate the command output history as well? (besides keeping a large histsize and leaning on the pgup key)

I cannot just 'rerun the command', as in my particular situation the source of the commands changes; I frequently need to look back at the output of a previous change.

Hacks welcome.

Thanks!

Edit 2010:

I've given 'tee' the credit; among all the methods to log your session elsewhere, it's the most broadly applicable (i.e. it's part of gnu coreutils, and doesnt require a specific shell or utility to work). It wasn't really what I was looking for, but I realize that such a thing doesn't really exist.

The closest approximation I can think of is to use something like screen (byobu/tmux) that logs to file, and write (and keybind) a custom command to page/search through that logfile (think less paging by prompt rather than screen). Thanks everyone.

Edit 2012:

@Dustin Kirkland's answer is clearly best, at least generally; the terminal's scrollback buffer is least-intrusive way to navigate command output history. Byobu appears to have a large default number of history lines saved (10K; tmux has 2K, GNU screen 100), and it allows regex searches (a cursory look indicates that GNU screen has no scrollback search, and tmux has only plaintext search).

0

6 Answers 6

6

You mention using byobu...

That's certainly one way of doing it. Byobu stores 10K lines of scrollback history per window or per split (pane).

You can enter scrollback easily by pressing Alt-PageUp, Alt-PageDown, or F7. Once you're in scrollback mode, you can search forward and backward using vi-like commands. Use /regex to search forward, and ?regex to search backward.

1
  • Where is scrollback history stored, and is there a way to make it persist between sessions? I.e.: Open terminal with Byobu, run some commands, call exit, close terminal, open a new terminal with Byobu and still have the previous session. Jul 31, 2019 at 16:53
13

You can use tee to send your command output to a file and to the terminal at the same time.

2
  • Hello from the future! Could you elaborate on how to use the tee command in this context?
    – Alexandre
    Oct 28, 2022 at 14:33
  • I think @Carl Norum might be thinking that you could tee pipe your output to any subsequent commands, as well as a log file? Feb 10, 2023 at 16:10
9

You can use script to log your terminal session to a file. If you always want this to happen then add a suitable script command to your .login.

$ man script

7

use screen -L

after exiting shell session (exit command) you get a logfile in the same directory you started screen

then you can view the output with more or less -R

2
  • and how does one stop this logging after doing screen -L ?
    – LeNan18
    Feb 8 at 6:04
  • I guess it is screen -X log off. Feb 9 at 19:53
5

bash does keep the history of what you did, but not the output from the commands. It could potentially be huge and frequently isn't useful.

5

screen can be set to log output.

One way to start it, within a running screen session, is to press Ctrl-a, then :, then enter log.

From man screen:

log [on|off]

Start/stop writing output of the current window to a file "screenlog.n" in the 
window's default directory, where n is the number  of  the  current  window.
This filename can be changed with the `logfile' command. If no parameter is
given, the state of logging is toggled. The session log is appended to the
previous contents of the file if it already  exists.  The current contents and
the contents of the scrollback history are not included in the session log.
Default is `off'.

logfile filename
logfile flush secs

Defines  the  name  the  log files will get. The default is "screenlog.%n". The 
second form changes the number of seconds screen will wait before flushing the
logfile buffer to the file-system. The default value is 10 seconds.
1

You must log in to answer this question.

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