Shell – Can we find page count of a file in Unix/Linux

shell-scripttext processing

I want to count number of pages in the file /var/log/messages, so that I can use pr command to start printing from the page I want to.

For example, suppose that /var/log/messages file contains 300 pages, then I can use the following to print last 5 pages.

pr +295 /var/log/messages

Note: pr command will list the page count when we specify page number that exceeds number of pages available in the file as mentioned below.

pr +400 /var/log/messages
pr: starting page number 400 exceeds page count 313 

Having said that I would like know if there is a specific command to find the page count.

Best Answer

pr default page length for text file is 56. So you can do like this:

$ perl -MPOSIX=ceil -nle 'END{print ceil($./56)}' file

If you want to count many files, try:

$ perl -MPOSIX=ceil -nle '
    if (eof) {
        print ceil($./56);
        close ARGV;
    }
' file1 file2 file3
Related Question