FactorPad
Build a Better Process

Linux pg Command Summary with Examples

Linux pg allows you to view files page-by-page.
  1. Purpose - Learn what pg is for and how to find help.
  2. Options - Review a few common options and arguments.
  3. Examples - Walk through code examples with pg.
  4. A tip - Finish off with one more insight.
face pic by Paul Alan Davis, CFA
Updated: February 24, 2021
In this tutorial on Linux pg, 54 of 100, below find a 3-4 minute introductory video, a text-based tutorial and all of the code examples from the video.

Outline Back Tip Next

/ factorpad.com / tech / linux-essentials / pg-command.html


An ad-free and cookie-free website.


Examples of the Linux pg Command

Beginner

Learn to view pages in a file at the Linux command line.

Video Tutorial

Videos can also be accessed from the Linux Essentials Playlist on YouTube.

Linux pg Command Summary with Examples (3:53)

Video Script

The Command and Why You Need It

Our fifty-fourth word, or command to memorize is pg from our category Text.

pg allows you to view text files page-by-page.

Common Linux pg Options
-options description
--help Print help screen
-c Clear screen before each page
-NUMBER Set the NUMBER of lines on a page

Recall from back in video 5, on less, that was my preferred pager, and I mentioned Linux includes a variety of pagers and pg is one of them. In the next few videos, to be fair, I want to introduce you to other text programs.

Before we start, it helps to think of commands as mini programs and most follow this structure: command -option(s) argument(s).

The pg command has 12 options and the argument is the text file to view, and because pg is a separate program it has its own internal commands, and there are about 15 of those.

Like most commands, help is available with double-dash --help. The -c option clears the screen before each page, and since pages sometimes don't fit on the screen, use dash and a number to change this.

So why is pg an important command? Well, you'll work with text often in Linux and functionality is highly personal so it's up to you to find your preferred pager. And now you know how to do that.

Demonstration

Okay, the best way to embed this in your memory is by typing in your own terminal window.

Find this on your Mac using a program called Terminal. On Linux use Terminal or Konsole, and currently Microsoft is adding this functionality to Windows.

Here we go. Let's pull the manual page on pg and redirect it > to a text file called video54.txt so we have something to work with.

$ man pg > video54.txt $ _

Next type pg and that file name video54.txt.

PG(1) User Commands PG(1) NAME pg - browse pagewise through text files SYNOPSIS pg [-amount] [-p prompt] [-cefnrs] [+line] [+/pattern/] [file...] DESCRIPTION pg displays a text file on a CRT one screenful at once. After each page, a prompt is displayed. The user may then either press the newline key to view the next page or one of the keys described below. If no filename is given on the command line, pg reads from standard input. If standard output is not a terminal, pg acts like cat(1) but precedes each file with its name if there is more than one. If input comes from a pipe, pg stores the data in a buffer file while reading, to make navigation possible. OPTIONS pg accepts the following options: +number Start at the given line number. +/pattern/ Start at the line containing the Basic Regular Expression pattern given. :_

And then, to page to the next page, hit Enter.

Start at the line containing the Basic Regular Expression pattern given. -number The number of lines per page. By default, this is the number of CRT lines minus one. -c Clear the screen before a page is displayed, if the terminfo entry for the terminal provides this capability. -e Do not pause and display (EOF) at the end of a file. -f Do not split long lines. -n Without this option, commands must be terminated by a newline character. With this option, pg advances once a command letter is entered. -p string Instead of the normal prompt :, string is displayed. If string contains %d, its first occurrence is replaced by the number of the current page. -r Disallow the shell escape. -s Print messages in standout mode, if the terminfo entry for the terminal provides this capability. -V, --version Disaplay version information and exit. -h, --help Display help text and exit. :_

Enter

Display help text and exit. COMMANDS The following commands may be entered at the prompt. Commands preceded by i in this document accept a number as argument, positive or negative. If this argument starts with + or -, it is interpreted relative to the current position in the input file, otherwise relative to the begin- ning. i<Enter> Display the next or the indicated page. id or ^D Display the next halfpage. If i is given, it is always interpreted relative to the current position. il Display the next or the indicated line. if Skip a page forward. i must be a positive number and is always interpreted relative to the current position. iw or iz As <Enter> except that i becomes the new page size. . or ^L Redraw the screen. $ Advance to the last line of the input file. i/pattern/ :_

And then notice how it's hard to know when it all ends?

i/pattern/ Search forward until the first or the i-th occurrence of the Basic Regular Expression pat- tern is found. The search starts after the current page and stops at the end of the file. No wrap-around is performed. i must be a positive number. i?pattern? or i^pattern^ Search backward until the first or the i-th occurrence of the Basic Regular Expression pat- tern is found. The search starts before the current page and stops at the beginning of the file. No wrap-around is performed. i must be a positive number. The search commands accept an added letter. If t is given, the line containing the pattern is dis- played at the top of the screen, which is the default. m selects the middle and b the bottom of the screen. The selected position is used in following searches, too. in Advance to the next file or i files forward. ip Reread the previous file or i files backward. s filename Save the current file to the given filename. h Display a command summary. !command Execute command using the shell. q or Q Quit. If the user presses the interrupt or quit key while pg reads from the input file or or writes on the :_

Let's work on that.

If the user presses the interrupt or quit key whil pg reads from the input file or writes on the terminal, pg will immediately display the prompt. In all other situations these keys will termi- nate pg. ENVIRONMENT VARIABLES The following environment variables affect the behavior of pg: COLUMNS Overrides the system-supplied number of columns if set. LANG, LC_ALL, LC_COLLATE, LC_CTYPE, LC_MESSAGES See locale(7). LINES Overrides the system-supplied number of lines if set. SHELL Used by the ! command. TERM Determines the terminal type. SEE ALSO cat(1), more(1), sh(1), terminfo(5), locale(7), regex(7), term(7) NOTES pg expects the terminal tabulators to be set every eight positions. Files that include NUL characters cannot be displayed by pg. AVAILABILITY The pg command is part of the util-linux package and is available from ftp://ftp.ker- nel.org/pub/linux/utils/util-linux/. util-linux July 2014 PG(1) (EOF):_

(hit qto return to the command prompt)

So, with the options -c and -20, for lines per page.

$ pg -c -20 video54.txt

This improves viewing.

PG(1) User Commands PG(1) NAME pg - browse pagewise through text files SYNOPSIS pg [-amount] [-p prompt] [-cefnrs] [+line] [+/pattern/] [file...] DESCRIPTION pg displays a text file on a CRT one screenful at once. After each page, a prompt is displayed. The user may then either press the newline key to view the next page or one of the keys described below. If no filename is given on the command line, pg reads from standard input. If stan dard output is not a terminal, pg acts like cat(1) but precedes each file with its name if there is more than one. If input comes from a pipe, pg stores the data in a buffer file while reading, to make navigation possible. :_

And I'll page through here (Enter) so you can see what I'm talking about.

possible. OPTIONS pg accepts the following options: +number Start at the given line number. +/pattern/ Start at the line containing the Basic Regular Expression pattern given. -number The number of lines per page. By default, this is the number of CRT lines minus one. -c Clear the screen before a page is displayed, if the terminfo entry for the terminal provides this capability. -e Do not pause and display (EOF) at the end of a file. -f Do not split long lines. :_

Hit q Enter to go back to the command prompt.

And last, a command within pg.

$ pg -c -20 video54.txt

One of those internal commands I mentioned.

PG(1) User Commands PG(1) NAME pg - browse pagewise through text files SYNOPSIS pg [-amount] [-p prompt] [-cefnrs] [+line] [+/pattern/] [file...] DESCRIPTION pg displays a text file on a CRT one screenful at once. After each page, a prompt is displayed. The user may then either press the newline key to view the next page or one of the keys described below. If no filename is given on the command line, pg reads from standard input. If standard output is not a terminal, pg acts like cat(1) but precedes each file with its name if there is more than one. If input comes from a pipe, pg stores the data in a buffer file while reading, to make navigation possible. :_

Type i and notice how the prompt changes, then enter 3 Enter for page 3.

-n Without this option, commands must be terminated by a newline character. With this option, pg advances once a command letter is entered. -p string Instead of the normal prompt :, string is displayed. If string contains %d, its first occurrence is replaced by the number of the current page. -r Disallow the shell escape. -s Print messages in standout mode, if the terminfo entry for the terminal provides this capa- bility. -V, --version Display version information and exit. -h, --help Display help text and exit. COMMANDS :_

Again, type q Enter to leave.

A Final Tip

Okay now you know how to use pg. And you know the syntax for commands, options and arguments.

One last tip about the pg command. So I suggest playing around with pg and see if you prefer it over less, and I'll introduce you to more next.

Okay, thanks for visiting today. I hope this was a helpful introduction to the pg command.


Learn More About The Series

For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:


What's Next?

Have you tried out our YouTube Channel? Subscribing there is a good way to be reminded as new content arrives.

Outline Back Tip Next

/ factorpad.com / tech / linux-essentials / pg-command.html


linux pg command
pg linux
linux pg
unix pg
pg unix
linux pg examples
linux pager
alternative to less
linux file viewer
more pager
linux pager options
pg tutorial
display text
linux text viewer
linux pg how to
linux pg summary
linux pg tutorial
linux pg video tutorial

A newly-updated free resource. Connect and refer a friend today.