How to Use Linux’s man Command (With Examples from Beginner to Advanced)

How to Use Linux’s man Command (With Examples from Beginner to Advanced) blog banner image

The Linux CLI is a powerful interface that allows users to interact directly with the operating system. It is essential for system administration, software development, and many other technical tasks. The CLI provides flexibility and control over system operations, making it a crucial tool for professionals.

Man pages are comprehensive documentation resources that describe the functionality, options, and usage of commands and programs in Linux. They serve as an essential reference for users, offering detailed information and examples that help in understanding and utilizing various tools effectively.

The man command is the gateway to accessing these manual pages. By providing a simple interface to query and read man pages, man helps users quickly find the information they need. This is especially useful for troubleshooting, learning new commands, and mastering the Linux environment.

This guide aims to provide a detailed understanding of the man command in Linux, showcasing its importance in accessing manual pages (or “man pages”) for various commands and programs. It will equip readers with practical knowledge on how to utilize the man command efficiently with real-world examples, enhancing their command-line proficiency.

What is the man command in Linux?

The man command originated in the early 1970s with the Unix operating system developed at Bell Labs by Ken Thompson, Dennis Ritchie, and others. Initially, Unix documentation was sparse, prompting the need for a standardized system to document commands and functions. The first Unix Programmer’s Manual was published in 1971, and it included brief descriptions and usage examples for various commands and programs.

The man command was designed to access the electronic copies of these printed manual pages directly from the command line, making it easier for users to find the documentation they needed, lessening the need for consulting printed manuals. This system was further refined and expanded in subsequent versions of Unix.

When Linux was first developed by Linus Torvalds in the early 90s, it inherited quite a few features from Unix, including the man command. The Linux man-pages project was started to ensure that Linux-specific documentation was readily available to users, covering a wide range of topics from basic commands to advanced system calls and configuration files.

When a user types man followed by a command name, the system searches the manual page database and retrieves the relevant documentation. This process involves looking through predefined directories where man pages are stored and building an up-to-date systemwide database, ensuring that users get accurate and comprehensive information.

(The updating of one’s “man db” (database) is often triggered as a post-install step in many Linux flavors’ package management systems; this helps to ensure that updates, installations, and uninstallations all leave your documentation as fresh, cruft-free, and (theoretically) as bug-free as your software itself.

Using the man: Basic to advanced examples

As touched on above, you can access man pages for common commands (e.g., ls, grep, or ssh) directly from the command line using man [command] for the command(s) interesting you. More advanced features such as keyword searches are covered below.

Man page navigation

Navigating man pages is straightforward. Users of vim-compatible editors might recognize the navigation key conventions:

Action Key Command Description
Display help. h Displays the help screen with a list of all navigation commands.
Search for a term. / followed by term Initiates a search for the specified term within the man page.
Move to next search occurrence. n Moves to the next occurrence of the search term within the man page.
Move to previous search occurrence. N Moves to the previous occurrence of the search term within the man page.
Exit man page viewer. q Exits the man page viewer and returns to the command prompt.
Scroll down. Down arrow or j Scrolls down one line at a time.
Scroll up. Up arrow or k Scrolls up one line at a time.
Page down. Space / Ctrl + F Scrolls down one page at a time.
Page up. b or Ctrl + B Scrolls up one page at a time.
Go to beginning of document. g Moves to the beginning of the man page.
Go to end of document. G Moves to the end of the man page.
Refresh the screen. Ctrl + L Redraws the screen, useful if the display gets corrupted.
Move to next section. } Jumps to the next section heading.
Move to previous section. { Jumps to the previous section heading.

Advanced examples:

  • Accessing specific sections of man pages: Some commands have multiple sections in their documentation. For example, man 5 passwd accesses the configuration file format of the passwd command, while man 1 passwd covers the user command.
  • Using keywords to search within man pages: The man -k [keyword] command searches for all man pages containing the specified keyword. For instance, man -k network lists all man pages related to networking.

Tips and tricks for mastering man Command

Customizing the man command output:

  • Combining man with other commands for enhanced utility: You can combine man with other commands to process man page output. For example, man ls | grep -i sort filters the ls man page to show only lines containing the term “sort”.
  • Customize the display format of man pages: By default, the less command is used to display the man page contents in a navigable, searchable format. By setting environment variables like MANPAGER and MANWIDTH, the output can be altered. For instance, setting export MANPAGER=”less -S” configures the man pages to be displayed in less with horizontal scrolling enabled.

A typical man page includes sections such as NAME, SYNOPSIS, DESCRIPTION, OPTIONS, and EXAMPLES. Organizing man pages into these sections provides a consistent structure that makes it easier for users to find the information they need quickly.

This organization enhances readability and ensures that important details are not overlooked, making the documentation more user-friendly and efficient for both new and experienced users.

Each section serves a specific purpose:

  • NAME: Brief description of the command. This provides an immediate understanding of the command’s purpose.
  • SYNOPSIS: Summary of command syntax. This section outlines how to use the command, including its parameters and options.
  • DESCRIPTION: Detailed explanation of the command’s functionality. It provides an in-depth look at what the command does and how it works.
  • OPTIONS: List of available options and their descriptions. This section helps users understand the different flags and parameters that modify the command’s behavior.
  • EXAMPLES: Practical usage examples. These examples demonstrate common use cases and help users see how the command can be applied in real-world scenarios.

Advanced man Use Cases

Accessing and Extracting Specific Sections

  • Filtering by section number: To access specific sections of man pages, For example, man 3 printf retrieves the documentation for the printf function in the C standard library. Here is a list of the numerical section numbers for man pages:
    • 1: User commands (executable programs or shell commands)
    • 2: System calls (functions provided by the kernel)
    • 3: Library calls (functions within program libraries)
    • 4: Special files (usually found in /dev)
    • 5: File formats and conventions (e.g., /etc/passwd)
    • 6: Games and screensavers
    • 7: Miscellaneous (including macro packages and conventions)
    • 8: System administration commands (usually only for root)
    • 9: Kernel routines (non-standard)
  • Isolating detailed sections: Extract detailed sections like the EXAMPLES section using tools like awk or sed. Example: man ls | sed -n ‘/^EXAMPLES/,/^$/p’ extracts the EXAMPLES section from the ls man page.

Creating custom man pages

  • Writing custom documentation: Create custom man pages for proprietary scripts and commands. This involves writing a man page in a text file and installing it using the man command. Example: sudo cp mycommand.1 /usr/local/man/man1/ installs a custom man page.
  • Embedding man pages in scripts: Include embedded documentation in scripts that can be accessed via man. Use the cat command to output the embedded man page text. Example: man ./myscript displays the embedded man page of myscript.

Integrating man with other tools

  • Combining man with apropos: Use apropos to find commands related to a keyword and then pipe the results to man. Example: apropos network | xargs -n 1 man -k searches for network-related commands and displays their man pages.
    • Note: man vs apropos vs info: The man command provides detailed manual pages for commands and programs in a structured format, apropos searches the manual page descriptions for keywords to help locate commands related to a specific topic, and info displays documentation in a hypertext format, often with more detailed information and navigation compared to man.
  • Automating documentation lookups: Create scripts that automate the lookup and display of man pages based on user input. Example: #!/bin/bash read -p “Enter command: ” cmd; man $cmd prompts the user for a command and displays its man page.

Customizing and enhancing output

  • Formatting output for better readability: Use environment variables and options to customize man page output. Example: export MANWIDTH=80; man ls sets the output width for better readability on narrow screens.
  • Converting man pages to other formats: Convert man pages to HTML or PDF for offline reading or printing. Example: man -t ls | ps2pdf – ls.pdf converts the ls man page to a PDF file.

Advanced searching techniques

  • Full-text search within man pages: Perform a full-text search across all installed man pages using tools like grep. Example: grep -r ‘keyword’ /usr/share/man/man* searches for a keyword in all man pages.
  • Using regular expressions: Use regular expressions to find complex patterns in man pages. Example: man ls | grep -E ‘\-l | \-a’ searches for both -l and -a options in the ls man page.

Localization and internationalization

  • Accessing man pages in different languages: Set the LANG environment variable to access man pages in different languages. Example: LANG=es_ES.UTF-8 man ls displays the ls man page in Spanish.
  • Creating multilingual man pages: Write and install multilingual man pages for custom commands, making them accessible in different locales.

Integrating man pages with IDEs and Editors

  • IDE integration: Configure Integrated Development Environments (IDEs) to display man pages for highlighted commands or functions. Example: Setting up Vim with the :Man command to view man pages directly within the editor.
  • Editor Plugins: Use editor plugins that provide quick access to man pages. Example: Installing the vim-man plugin to enable man page lookups in Vim with a simple command.

Utilizing man in scripting and automation

  • The man utility can be used in scripts to automate the extraction of information from man pages. For example, man ls | col -b | grep -A2 ‘^ *-l ‘ extracts the description of the -l option for the ls command. This technique is useful for creating automated documentation tools or integrating command help into custom scripts.

Empowering your Linux journey

The man command is an invaluable tool for Linux users, providing instant access to comprehensive documentation for commands and programs. It enhances users’ ability to understand and effectively use various tools in the Linux environment.

Users are encouraged to explore man and experiment with other Linux commands to deepen their understanding and proficiency. The more familiar users become with these tools, the more effectively they can manage and troubleshoot their systems.

Final thoughts on the role of man in effective Linux command-line usage: Mastering the man command is a fundamental skill for anyone using the Linux command-line interface. It not only provides essential documentation but also empowers users to become more self-reliant and confident in their ability to navigate and utilize the Linux operating system.

Next Steps

Building an efficient and effective IT team requires a centralized solution that acts as your core service deliver tool. NinjaOne enables IT teams to monitor, manage, secure, and support all their devices, wherever they are, without the need for complex on-premises infrastructure.

Learn more about Ninja Endpoint Management, check out a live tour, or start your free trial of the NinjaOne platform.

You might also like

Ready to become an IT Ninja?

Learn how NinjaOne can help you simplify IT operations.

×

See NinjaOne in action!

By submitting this form, I accept NinjaOne's privacy policy.

NinjaOne Terms & Conditions

By clicking the “I Accept” button below, you indicate your acceptance of the following legal terms as well as our Terms of Use:

  • Ownership Rights: NinjaOne owns and will continue to own all right, title, and interest in and to the script (including the copyright). NinjaOne is giving you a limited license to use the script in accordance with these legal terms.
  • Use Limitation: You may only use the script for your legitimate personal or internal business purposes, and you may not share the script with another party.
  • Republication Prohibition: Under no circumstances are you permitted to re-publish the script in any script library belonging to or under the control of any other software provider.
  • Warranty Disclaimer: The script is provided “as is” and “as available”, without warranty of any kind. NinjaOne makes no promise or guarantee that the script will be free from defects or that it will meet your specific needs or expectations.
  • Assumption of Risk: Your use of the script is at your own risk. You acknowledge that there are certain inherent risks in using the script, and you understand and assume each of those risks.
  • Waiver and Release: You will not hold NinjaOne responsible for any adverse or unintended consequences resulting from your use of the script, and you waive any legal or equitable rights or remedies you may have against NinjaOne relating to your use of the script.
  • EULA: If you are a NinjaOne customer, your use of the script is subject to the End User License Agreement applicable to you (EULA).