Saving 'less' Buffer to a File

Submitted by Eus
on March 31, 2009 - 1:09am

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.

Keywords: unix less buffer save to a file

You could have just pressed

Anonymous (not verified)
on
April 15, 2009 - 1:43am

You could have just pressed 's'.

Thanks

Eus
on
April 17, 2009 - 3:32am

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

another

Anonymous (not verified)
on
April 21, 2009 - 4:33pm

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'

That is great!

Eus
on
April 22, 2009 - 3:22am

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

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.