I often want to read output from the terminal from the beginning but by default I have to scroll back up. This can be laborious when there is a lot of text. Is there anything I can do about this?
Thanks :)
Pipe it through less. Example:
dmesg | lessThe pipe character basically takes the output (STDOUT to be specific) of one command and provides it as input (STDIN) to the next command. It’s one of the many ways of redirecting in linux.
Some relevant examples:
dmesg > dmesg.txt …(over)writes the ouput to a file
dmesg >> dmesg.txt …appends the output to a file
ps aux | grep bash …pipe, as described above. The tidbit is that grep only prints lines matching the pattern specified
find / -name somefilename 2>&1 >result.txt …redirects warnings and errors (STDERR) to STDOUT, so that you don’t have to treat those separately.It’s interesting that ‘less’ and ‘more’ have both been mentioned - when I was first starting with Linux, the fact ‘less’ is the newer, more advanced version of ‘more’ is the kind of jokey stuff that I couldn’t get my head around.
Anyway, depending on the output, another useful option can be piping it to ‘column’. There was a fun video about this the other day at https://piefed.social/post/401865 (the video itself is hosted by PeerTube).
One option that hasn’t been mentioned yet:
head
. It will only show the first 10 (or is it 20?) lines of the output you pipe into it by default, but you can also pass a parameter to change the number of lines shown. The complement of this command istail
, to show only the last N lines. Both these commands are handy when you only want to see the first/last lines of output.Pipe it through more, for example
dmesg | more
Depending upon your terminal, you might find shortcuts for that.
I have Shift+Home set for this purpose.
Yep, I think this is the best solution, with foot terminal you can navigate going to the top/bottom or scrolling with keybinds. Unless of course the output is too big or you want to save the output, then the other comments on the post have you covered.
For cases where you really just want to jump between different prompts without piping out to another program/file, many terminals also support jumping through prompts as long as your shell marks them with an OSC-133;A escape code (looks like fish does this automatically now). Some terminals that support this are foot, tmux, kitty, and Emac’s vterm, but it’s undoubtably available in many others.
For example let’s say you’re using the foot terminal and the zsh shell. Just add the following code to your zshrc and then you can jump through each prompt using CTRL-z and CTRL-x
precmd() { print -Pn "\e]133;A\e\\" }
Using less is good, but only if your command output is done in one go. If you have a command that keeps running, use
reset; command
to first clear all output, and then run the command. You can easily scroll right to the top.Some people are saying to use less or more. But what you really wanna do is dump the output to a text file then less or more the file. You can delete the file later
I always use my desktop for this bc the icons annoy me so I know I’ll nuke it later.
But anyway this is better in general bc often you’ll forget what the output was or you’ll want to reference it somehow
/tmp
cries from neglect :DI only use tmp for scripts not for random crap I do in my term
random crap I do in my term
/dev/shm is great for that. :) Automatically cleans up regardless of distribution.
Oh for sure. Anything other than the desktop just feels more offical somehow is the thing