If I've piped the results of a command to less and then decided that I want to save the contents to a file, is this possible?

I've tried setting a mark a at the end of the buffer, and then returning to the top and using |avi to send the whole contents to vi, but that doesn't work.

  • 1
    Your original technique would work if you instructed vi to load from stdin, by doing |avi -. – Joe Shaw Apr 18 '14 at 17:07
up vote 97 down vote accepted

On my system, man less says

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

Works for me!

  • nice, thanks! Works a treat. – Jonathan Day May 31 '11 at 11:43
  • 3
    Somehow, this doesn't work for me - typing 's' moves the window by one line. I'm on a Mac. – benroth Feb 19 '14 at 0:47
  • 1
    @benroth: You probably have a lesskey file that changes the normal commands. See man lesskey – RedGrittyBrick Feb 19 '14 at 9:44
  • 5
    s does not work for me either, but -o file does. – user1338062 Jan 13 '16 at 7:54
  • 1
    And why on earth isn't this mentioned when I type 'h' to get interactive help? +1 if you've read the interactive help and therefore didn't think to consult man less... – Peter V. Mørch Sep 17 '16 at 11:34

The accepted answer doesn't work on the Mac -- as @benroth says, pressing s just moves down a line -- but you can use a different method.

In less --help:

|Xcommand            Pipe file between current pos & mark X to shell command.

and

A mark is any upper-case or lower-case letter.
Certain marks are predefined:
     ^  means  beginning of the file
     $  means  end of the file

So if you go to the top of the buffer (<) and then:

|$cat > /tmp/foo.txt

the contents of the buffer will be written out to /tmp/foo.txt.

  • This method can be especially useful when you want to pipe the current buffer being viewed through another command. – Doron Behar Jun 11 '17 at 17:25

When your less is opened, you can save the complete output to a file. Like vim, less supports commands.

Just type the key s, then less will ask you the name of the file where you wish to save the content, just type the file name and then type Enter.

Cheers

Use the > operator. For example: less foo.bar > output.txt.

  • Thanks @Dror, but I'm already in the less application, not at the bash prompt any more – Jonathan Day May 31 '11 at 9:53
  • A valuable contribution to the Q/A. Rather than start a totally separate question, people needing a method to script multiple files through the Less Viewer can benefit a lot. Saved me hours of work (after a lot of searching). Thanks! – L. D. James Jun 14 at 6:57

My answer comes a tad too late I believe. But just for reference, in response to benroth's concern above: For OSX users there's always the option to dump the contents of the pager to a log file by using the option "-l" (read DASH ELL) at the colon prompt.

The pager will ask for a log file. Key it and press [CR]

No if you have started less, but if you know before yu want to send it to less and a file then you can use the tee command

command | tee out_file | less
  • Thanks Mark, but I'm specifically looking how to do it if I'm already in less – Jonathan Day May 31 '11 at 9:53

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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