How can I save the current contents of less to a file?

94

16

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.

Jonathan Day

Posted 2011-05-31T09:46:56.050

Reputation: 1 261

1Your original technique would work if you instructed vi to load from stdin, by doing |avi -. – Joe Shaw – 2014-04-18T17:07:06.467

Answers

106

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!

RedGrittyBrick

Posted 2011-05-31T09:46:56.050

Reputation: 70 632

8s does not work for me either, but -o file does. – user1338062 – 2016-01-13T07:54:08.113

1And 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 – 2016-09-17T11:34:07.497

See man less for the lesskey details (not man lesskey). – Rob Bednark – 2016-12-31T22:14:05.590

@PeterV.Mørch Maybe this was added since you wrote your comment but I can say that it's in the version I have (530, 05 Dec 2017). – comfreak – 2019-05-03T09:43:25.040

1@comfreak: Yes it is in less 487 in Ubuntu 18.04 but not in less 481 in Debian 9/stretch/stable – Peter V. Mørch – 2019-05-07T09:29:22.463

5Somehow, this doesn't work for me - typing 's' moves the window by one line. I'm on a Mac. – benroth – 2014-02-19T00:47:10.253

2@benroth: You probably have a lesskey file that changes the normal commands. See man lesskey – RedGrittyBrick – 2014-02-19T09:44:58.613

24

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.

Joe Shaw

Posted 2011-05-31T09:46:56.050

Reputation: 356

This method can be especially useful when you want to pipe the current buffer being viewed through another command. – Doron Behar – 2017-06-11T17:25:18.187

4

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

Magnos Hammes

Posted 2011-05-31T09:46:56.050

Reputation: 41

0

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]

superk

Posted 2011-05-31T09:46:56.050

Reputation: 101

0

I wanted to do a slightly different thing: write just the currently visible portion of the text to a file. I found that I could do so using the "|" operator, specifying "." as the mark, and cat >/tmp/mycopy as the shell command.

user54690

Posted 2011-05-31T09:46:56.050

Reputation: 31

0

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

Dror

Posted 2011-05-31T09:46:56.050

Reputation: 1 510

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 – 2018-06-14T06:57:00.673

Thanks @Dror, but I'm already in the less application, not at the bash prompt any more – Jonathan Day – 2011-05-31T09:53:12.477

0

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

user151019

Posted 2011-05-31T09:46:56.050

Reputation: 5 312

Thanks Mark, but I'm specifically looking how to do it if I'm already in less – Jonathan Day – 2011-05-31T09:53:34.410