Unix command line tricks for Linux, Mac, and Windows: displaying larger files with the more command

in #technology3 years ago

See Unix command line tricks for Linux, Mac, and Windows: table of contents for an introduction to this series and links to all the entries.

Last week we looked at cat, a command that displays the contents of a file on your screen. I wrote "I use cat every time I want to look at the contents of a file that is 25 lines or less, which means I use it dozens of times a day." If you use cat with a file longer than that, the top of the file will scroll out of the window and you only see the last 25 lines, so that's where the more command (which I also use dozens of times a day) comes in. To test it, I'll use the file 40lines.txt, which starts like this

Here is line1
Here is line2
Here is line3
Here is line4
Here is line5

and then after 30 more lines ends like this:

Here is line36
Here is line37
Here is line38
Here is line39
Here is line40

After typing this command,

more 40lines.txt

the more command shows me enough lines to fill up the screen, an indicator of how far it got into the file (in this case, 56% of the way in), and waits for me to press the space bar to display another screenful:


Besides the space bar, there are many keystrokes that let you do useful navigation of the file displayed by more. The ones I use the most often are b and the slash key /. If you think of the space bar as being like a "page down" key to look at the next screenful of content, then b ("back") is the "page up" key that lets you look at the screenful before the one that is currently displayed. The slash key (/) lets you search for a string of text within the file while more is displaying that file. If your search finds the text you were looking for, pressing the n key over and over will look for additional instances of that string.

The only other more keystroke command to remember when more is showing you a file is the question mark (?), which tells you about all the others.

If I had really typed cat 40lines.txt and then realized that I was not seeing the top of the file and that I should have used more instead of cat to look at 40lines.txt, then instead of using the command shown above, I would actually press my cursor-up key to retrieve that cat command to my terminal window's prompt and then add a pipe symbol and "more" to the command, like this:

cat 40lines.txt | more

In other words, I would pipe the output of the cat command to more so that I could view its output a screenful at a time. Doing it by retrieving the previous command and adding the new part would save me the trouble of writing out the filename again. I often pipe output to more from other commands that display more content than I thought I'd see on the screen—for example, the ls command to list filenames and associated information.

Like all the commands we've looked at, more has command line options that let you customize its behavior. (Remember, these are extra options to include at the terminal command line when you invoke the more command, not keystrokes that control what more does while you're using it, like the slash and question mark keystrokes described above.) For example, the + option tells more to start displaying at the first line that has a particular string. Here, we're telling it to start displaying at the first line that has the text "line30" in it:

more +line30 40lines.txt

As always, the man command will tell you more about this command:

man more

Congratulations @bobdc! You have received a personal award!

1 Year on Steemit
Click on the badge to view your Board of Honor.

Do you like SteemitBoard's project? Then Vote for its witness and get one more award!

Congratulations @bobdc! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Coin Marketplace

STEEM 0.16
TRX 0.03
JST 0.027
BTC 13031.35
ETH 406.22
USDT 1.00
SBD 0.99