I've piped a command to less, and now I want to save the command's output to a file. How do I do that?

In this case, I don't want to use tee, I want a solution directly from less, so that I don't have to rerun a long-running command if I forgot to use tee.

This question is similar to this one, the only difference is that I want to save all of the lines, not a subset: Write lines to a file from less


From less, type s then type the file name you want to save to, then Enter.
From the man page, under COMMANDS:

s filename
      Save the input to a file.  This only works if the input is a pipe, not an ordinary file.

man page also states that, depending on your particular installation, the s command might not be available. In that case, you could go to line 1 with:

g or < or ESC-<
      Go to line N in the file, default 1 (beginning of file).

and pipe the whole content to cat with:

| <m> shell-command
      <m> represents any mark letter. Pipes a section of the input file to the
      given shell command. The section of the file to be piped is between the
      first line on the current screen and the position marked by the letter. 
      <m> may also be ^ or $ to indicate beginning or end of file respectively.

so either:

g|$cat > filename


<|$cat > filename

i.e. type g or < (g or less-than) | $ (pipe then dollar) then cat > filename and Enter.
This should work whether input is a pipe or an ordinary file.

| improve this answer | |

One way to do it without tee is as follows.
less /path/to/input > /path/to/output
The above will overwrite /path/to/output if it already exists.

To do the same, but append to /path/to/output is as follows.
less /path/to/input >> /path/to/output

To save the text in less after you have opened it, you can use the Save File feature.
When in less, press s. Then, type the file name.
If you used a pipe, the file will automatically be written to Working Directory.
If you did not use a pipe, you will open a file with the text (for which you can save).
I believe the file will be opened in vim.

| improve this answer | |
  • 7
    I want a solution from less once I've already opened less. – Flimm Dec 10 '14 at 21:02
  • I see. Sorry for the misunderstanding. I've updated my answer. – nehcsivart Dec 10 '14 at 21:10

You don't use less, you use tee and then pipe tee to less.

./program | tee program.out | less

| improve this answer | |
  • 5
    In the question, I specifically asked for a solution that worked without tee once less had already been opened. – Flimm Aug 4 '16 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.