Code and comments

Practical and theoretical aspects of software development

Posts Tagged ‘less

Viewing multiple files with less

leave a comment »

When you want to see the contents of a text file in unix, the first two things to come to mind are cat and less. I choose cat when the files are small, but the pagination and navigation in less makes it the better choice most of the time.

Suppose you find a directory with five files that are unknown to you. Printing the contents of all five is easy enough with cat and a pipe:

$ ls | xargs cat

 
But if you have five files, chances are you don’t just want everything scrolling past you. So we try:

$ ls | xargs less

 
Hmm … when you get to the bottom of file1.txt we find the message (END) - Next: file2.txt. What to do?

As always, the answer is in the man page: :n for next file, :p for previous file.

Of course, this isn’t only useful when piping input into less, it also works when specifying multiple arguments:

$ less file1.txt file2.txt file3.txt

 

Advertisements

Written by Eric Wilson

October 17, 2011 at 2:26 pm

Posted in how-to

Tagged with , ,