Saving ‘less’ Buffer to a File

Posted in kerneltrap.org on March 31, 2009 – 3:09pm

Just today I overwrote an important file accidentally. Fortunately, I had another terminal running `less’ that had been reading the overwritten file up to the state just before it was overwritten (i.e., I read the file in another terminal with `less’ and kept it open after I had overwritten the file). So, I wanted to save the content of the display buffer of `less’ to a file. But, how?

Googgling for `unix less buffer save to a file’ didn’t turn up any useful result. I was aware that pressing `v’ to invoke a text editor would make `less’ read the content of the overwritten file, and therefore, I would lose my original one. So, no, that was not the way to save the buffer to a file. At the end, desperately I started to copy-and-paste the screen manually. Unfortunately, I would lose all of the whitespace formatting characters by doing it that way. But, what else could I do?

After I finished copying-and-pasting the whole file, I deleted the overwritten file and pressed `v’ as to see whether `less’ would complain that the original file was missing and then, perhaps, offer me to save the buffer somewhere else. But, `less’ instead said: `cannot edit standard input’. Standard input? Oh, I did `cat file.txt | less’ then. But, standard input reminded me that `less’ can do piping of its buffer through a shell command! Oh, yes, so I went to the top of the buffer and pressed `|’ followed by `$’. When it asked for the command, I entered: `tee /home/eus/precious_file.txt’. Yeah, it did the trick and saved the day!

In short, to save the buffer that is being displayed by a session of `less’, use its pipe-to-shell-command capability by scrolling to the top of the file and press `|’ followed by `$’ as well as entering `tee DESTINATION_FILE’ when prompted for the shell command.

About these ads
This entry was posted in less, Software Usage and tagged , . Bookmark the permalink.

4 Responses to Saving ‘less’ Buffer to a File

  1. Eus says:

    You could have just pressed
    Posted by Anonymous (not verified) on April 15, 2009 – 3:43pm

    You could have just pressed ‘s’.

    • Eus says:

      Thanks
      Posted by Eus on April 17, 2009 – 5:32pm

      Hi Ho!

      Ah, yes, thanks :-) But, it doesn’t work if the input is not stdin because `less’ will say: `Input is not a pipe’. My approach works for both stdin and not.

      Heh, I missed the `s’ button because pressing `h’ while on `less’ to display the help screen doesn’t show anything about the effect of pressing `s’ button. However, in the man page, it is clearly written that the button will save the input to a file.

      Best regards,
      Eus (FSF member #4445)

      In this digital era, where computing technology is pervasive,
      your freedom depends on the software controlling those computing devices.

      Join free software movement today!
      It is free as in freedom, not as in free beer!

      Join: http://www.fsf.org/jf?referrer=4445

  2. Eus says:

    another
    Posted by Anonymous (not verified) on April 22, 2009 – 6:33am

    if you deleted a archive but is open on another aplication then…

    lsof |grep ‘deleted-filename’

    get the ‘PID’ and the ‘file descriptor’, then…

    cp /proc/’PID’/fd/’file descriptor’ ‘deleted-filename’

    • Eus says:

      That is great!
      Posted by Eus on April 22, 2009 – 5:22pm

      Hi Ho!

      Yes, that’s a good trick that I didn’t know before.

      Unfortunately, that doesn’t work with stdin.
      But, that’s a nice trick to rescue data from another program that does not offer a way to dump its buffer out to a file provided that the program doesn’t get the data from stdin.

      I think there should be a way to read the data directly from the memory used as the buffer itself. Perhaps along the line of hi-jacking the process’ memory map.

      Best regards,
      Eus (FSF member #4445)

      In this digital era, where computing technology is pervasive,
      your freedom depends on the software controlling those computing devices.

      Join free software movement today!
      It is free as in freedom, not as in free beer!

      Join: http://www.fsf.org/jf?referrer=4445

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s