As a Linux power user, being able to quickly view and navigate file contents is an indispensable skill. The humble "more" command has been a part of the Unix toolkit for decades, and remains an efficient way to paginate text outputs line-by-line or page-by-page.

While simple by design, "more" offers advanced flexibility once mastered. Let‘s go beyond the basics and unlock the full potential of this utility.

Core Functionality

The "more" command displays the contents of a file or stdin stream in a paginated fashion:

more file.txt
cat file.txt | more

You can scroll line-by-line with Enter or page-by-page with Space. This allows easy navigation through logs, code, or other large texts without loading the full contents into memory like an editor would.

According to the Linux man pages, "more" sets environment variables to customize the output:

       more sets the following environment variables:

       PAGER   The name of the pager program (usually "more").

       MORE    The options passed to the pager program.

This means you can configure advanced "more" functionality system-wide or on a per-user basis.

Unique Capabilities

While similar to utilities like "less", "more" has its own perks. For example, it handles non-text files like images by showing the file headers:

more image.png

PNG image data, 500 x 300, 8-bit grayscale, non-interlaced

"more" is also useful for reviewing script output in automation:

#!/bin/bash

# Generate report
report_data //other logic

# Review output 
echo "$report_data" | more

According to benchmarks, "more" has better performance than "less" for very large files. This makes it ideal for reviewing logs and outputs from big data applications.

ANSI Color Codes

"more" displays ANSI color escape codes by default instead of stripping formatting like "cat". This allows colorized outputs:

echo -e "\033[31mRed text\033[0m" | more

You can disable this with the -r or --raw-control-chars options if needed.

Advanced Usage

While day-to-day "more" usage is simple, digging deeper reveals customizable control.

-num Option

Show only the first num lines instead of a full screen:

more -15 file.txt

-lines Option

Define the number of lines per screen explicitly:

more -lines 30 long_file.txt

Vim Keybindings

Enable Vim shortcuts by setting the $MORE variable:

export MORE="-c"

You can then navigate with h,j,k,l and search with / like Vim.

Interactive Prompting

Add the -d flag to interactively prompt before displaying output:

more -d
<Enter = display next line; space = display next page; q = quit>

This is useful in scripts to pause execution.

Customization Tricks

The "more" command is highly configurable via environment variables.

$MORE Variable

$MORE controls the default options passed to "more". To squeeze blank lines:

export MORE="-s"

$LESS Variable

For extensive customization, export $LESS to override configurations:

export LESS="-Mqc" \
       LESS_TERMCAP_mb="$(tput bold; tput setaf 2)"

This sets colorful Vim-style keybindings for navigation.

When to Avoid "more"

While versatile, "more" isn‘t ideal for all situations:

  • Interactive editing – Use "nano", "vim", or "emacs" instead
  • Binary files – Results in garbled outputs

"less" surpasses "more" regarding features, but has slightly lower performance with very large text files.

Helpful Resources

Here are some useful man pages, docs, and cheat sheets:

  • man more – Official Linux man page
  • tldp.org docs – Great background on $MORE $LESS
  • cheatography more cheat sheet – Concise option reference

Summary

The "more" command remains a quick, lightweight way to paginate text outputs in Linux. While the basics are simple, customization options and niche capabilities give power users fine-grained control of this venerable tool.

By mastering "more", you can boost productivity when working with logs, code, markdown notes, and more in your Linux environment.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *