Home FAQ How To Find Longest Man Page In Linux
How To Find Longest Man Page In Linux

How To Find Longest Man Page In Linux

By sk
30 Views

All commands' man pages are stored in /usr/share/man/ directory in Linux. The man pages compressed in gzip (.gz) format. Curious to know which command has the biggest manual page on your Linux system? Here biggest means, I am talking about the number of lines in a man page. It is not about the file size or number pages when printed. If you ever wondered how to find longest man page in Linux and Unix distributions, follow any one of the methods given here.

How To Find Longest Man Page In Linux

We can find the biggest and longest man pages by counting the number of lines and words in them. The wc command is used to count number of lines and words in a file. For instance, to count the number of lines in ls command's man page, run:

$ man ls | wc -l

Similarly, to count the number of words in ls man page, use -w flag like below.

$ man ls | wc -w

Find number of lines and words in a man page in linux

However, counting the number of lines in each man page is tedious, time consuming and probably a dumb idea to find out longest man pages in the whole system. We can easily find this using a for loop like below.

for i in {1..8}; do f=/usr/share/man/man$i/$(ls -1S /usr/share/man/man$i/ | head -n1); printf "%s: %9d\n" "$f" $(man "$f" 2>/dev/null | wc -l); done

This command will analyze man directories (1 to 8) and display the longest man page in each directory in descending order. It will take a few minutes depending upon the number of man pages in your system.

Here is the output from my Ubuntu 18.04 system:

/usr/share/man/man1/ffmpeg-all.1.gz: 25468
/usr/share/man/man2/perf_event_open.2.gz: 1732
/usr/share/man/man3/Net::SSLeay.3pm.gz: 8738
/usr/share/man/man4/st.4.gz: 423
/usr/share/man/man5/proc.5.gz: 3087
/usr/share/man/man6/espdiff.6.gz: 50
/usr/share/man/man7/groff_mdoc.7.gz: 1826
/usr/share/man/man8/dnsmasq.8.gz: 1337

Find Longest Man Page In Linux And Unix

As you can see, the longest man page is ffmpeg-all with 25468 lines in total.

You can cross check it with wc command like below.

$ man ffmpeg-all | wc -l
25468

If you want to find the longest man based on number of words instead of lines, run:

for i in {1..8}; do f=/usr/share/man/man$i/$(ls -1S /usr/share/man/man$i/ | head -n1); printf "%s: %9d\n" "$f" $(man "$f" 2>/dev/null | wc -w); done

Sample output:

/usr/share/man/man1/ffmpeg-all.1.gz: 130475
/usr/share/man/man2/perf_event_open.2.gz: 11031
/usr/share/man/man3/Net::SSLeay.3pm.gz: 37886
/usr/share/man/man4/st.4.gz: 4358
/usr/share/man/man5/proc.5.gz: 22939
/usr/share/man/man6/espdiff.6.gz: 294
/usr/share/man/man7/groff_mdoc.7.gz: 11919
/usr/share/man/man8/dnsmasq.8.gz: 16875

Check if it is correct using command:

$ man ffmpeg-all | wc -w
130475

A simple Script To Find Longest Man Page In Linux

Here is a simple BASH script to find the longest man page in a Linux system.

Create a file, for example longman.sh (short for longest man page):

$ nano longman.sh

Add the following lines in it:

#!/usr/bin/env bash
cd /usr/share/man/
for fn in $(find -maxdepth 1 -type d -iname 'man[1-8]'); do
  fn=${fn#./}
  ls -1Sp "$fn" | head -n 10 | sed -Ee "s,^,$PWD/$fn/," | while read manpage; do
    shortname=$(echo "$manpage" | sed -e "s,$PWD/man,," -e "s,/,"$'\t'",")
    printf '%9d\t%s\n' $(man "$manpage" 2>/dev/null | wc -l) "$shortname"
  done
done | sort -n

If you want to count number of words instead of lines, use wc -w in the above script. This script takes the top 10 in every man dir, compiles a table and sorts it by the number of lines. It also excludes any of the numbered sections 1-8 if they do NOT exist on your system.

Save and close the file and make it executable using command:

$ chmod +x longman.sh

Now, run this script to find the longest man page in your system:

$ ./longman.sh

The longest man page is displayed at the bottom. If you want to display longest man page on the top, use "sort -rn" in the above script.

Sample output:

[...]
14236	1	ffmpeg-filters.1.gz
15470	1	x86_64-linux-gnu-g++-7.1.gz
15470	1	x86_64-linux-gnu-gcc-7.1.gz
20579	1	ffplay-all.1.gz
20797	1	ffprobe-all.1.gz
24856	1	ffserver-all.1.gz
25468	1	ffmpeg-all.1.gz

A Bash Script To Find Longest Man Page In Linux

The number in the middle indicates the man page section.

To know more details about man pages, run:

$ man man
$ man -k man
$ info man

Suggested read:


Hope this was useful.

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You May Also Like

3 comments

Jalal April 23, 2020 - 10:13 am

Hi,
Thanks a lot

Reply
Sven April 23, 2020 - 6:19 pm

#!/usr/bin/env bash
find -L /usr/share/man/man[1-8] -maxdepth 1 -type f -ls|sort -nk7|tail -80|sed -E ‘s,.*(/usr/share/man/man)([0-9])/([[:graph:]]+).*,printf “%9d\t%d\t%s\n” $(man “\1\2/\3″|wc -l) \2 “\3”,’|bash 2>/dev/null|sort -n

I knew it was ffmpeg-all before I even read the article or rather did I suspect that it had to be very high up in the top. It then also depends on what you have installed of course. When you don’t have ffmpeg installed then you won’t find it to have the longest manpage…

Seeing your script, I’d like to note a couple of things about it.

1. Don’t write fn=${fn#./} but write fn=${fn##*/} While your substitution works is the later better, because the later works universally as a replacement for getting a file’s basename and is much more worth remembering, because it will come in handy many times in future shell scripts. What here works, too, is to the -printf option to find. Using -printf “%f\n” will give you the basename.

2. When you use ls (or find) then never forget that many distros use synbolic links in quite a few places. Use find and ls with the -L option when you know you do not want to deal with these symbolic links themselves.

3. Avoid using sed for too many replacements. Firstly, only use sed -E when you actually do use extended regular expressions, which you are not doing here. As you then assign the output to a variable can you use bash’s own builtin substitutions (see the BASH manual on ${VARNAME/pattern/replacement} for this), or since you echo the filename anyway, could you just prepend the path with the echo statement itself.

4. Using loops in shell scripts can be slow and a faster way can be xargs for example. Try to pipe the output into xargs or a subshell and get it done in one line.

You can then do it all in one line:

find -L /usr/share/man/man[1-8] -maxdepth 1 -type f -ls|sort -nk7|tail -80|sed -E ‘s,.*(/usr/share/man/man)([0-9])/([[:graph:]]+).*,printf “%9d\t%d\t%s\n” $(man “\1\2/\3″|wc -l) \2 “\3”,’|bash 2>/dev/null|sort -n

The one-liner will give you more large manpages and not only the Top-10 of each section. You will see that section 1 of the manpages holds far more large ones and that some sections many not even show up in a Top-100, because these only hold rather small manpages.

Reply
sk April 23, 2020 - 7:27 pm

Thank you. Much appreciated.

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More