How to Display a Configuration File Without Empty Lines and Comments in Linux

When administering Linux servers, you often need to quickly inspect a configuration file while excluding empty lines and comments from the output. This is especially useful when working with large configuration files for Nginx, Apache, MySQL/MariaDB, PHP, SSH, and other services.

For example, instead of scrolling through dozens or hundreds of lines of documentation and commented-out options, you can display only the active configuration.

The Simplest Option with

grep

For configuration files where comments begin with the

#

character, you can use:

grep -vE '^[[:space:]]*(#|$)' /etc/example.conf

For example:

grep -vE '^[[:space:]]*(#|$)' /etc/ssh/sshd_config

This command removes:

  • empty lines;
  • lines containing only spaces or tabs;
  • lines beginning with
    #

    ;

  • comment lines preceded by whitespace.

The

-v

option inverts the match, so

grep

displays lines that do not match the specified pattern.

The

-E

option enables extended regular expressions.

The expression:

^[[:space:]]*(#|$)

can be read as follows: from the beginning of the line, allow any number of whitespace characters, followed either by

#

or immediately by the end of the line.

Using

sed

The same result can be achieved with

sed

:

sed -e '/^[[:space:]]*#/d' -e '/^[[:space:]]*$/d' /etc/example.conf

The first expression removes comment lines:

/^[[:space:]]*#/d

The second removes empty lines and lines containing only whitespace:

/^[[:space:]]*$/d

Using

awk

Another compact option is:

awk '!/^[[:space:]]*(#|$)/' /etc/example.conf

The result is similar:

awk

prints only lines that are neither empty nor begin with

#

.

If Comments Begin with

;

Some configuration formats, especially INI-style files, use

;

as the comment character.

In that case:

grep -vE '^[[:space:]]*(;|$)' /path/to/config.ini

If the file may use both

#

and

;

for comments:

grep -vE '^[[:space:]]*([#;]|$)' /path/to/config.ini

Viewing a Large Configuration File

If the output does not fit on one screen, you can pipe it to

less

:

grep -vE '^[[:space:]]*(#|$)' /etc/example.conf | less

On modern Linux systems,

less

is generally more convenient than

more

for interactive viewing. It allows you to scroll both forward and backward, search with

/

, and quickly navigate between matches.

To exit

less

, press:

q

Why You Should Not Simply Use

grep -v "#"

An older version of this command often looks like this:

cat /etc/example.conf | sed '/^$/d' | grep -v "#" | more

It works, but it has several drawbacks.

First,

cat

is unnecessary here because both

grep

and

sed

can read files directly.

Second:

grep -v "#"

removes every line containing

#

, not just comment lines.

For example:

password=my#secret

would also be hidden, even though

#

may be part of the actual value.

For this reason, it is safer to check specifically for comments at the beginning of a line:

grep -vE '^[[:space:]]*#'

What About Inline Comments?

Suppose a configuration file contains:

Port 22
PermitRootLogin no    # Disable root login
PasswordAuthentication no

The previous commands will keep the second line unchanged because it does not begin with

#

.

In theory, you can remove inline comments with

sed

, for example:

sed 's/[[:space:]]#.*$//' /etc/example.conf

However, this approach should be used carefully.

The

#

character may be part of a valid configuration value:

password="abc#123"

Comment syntax depends on the specific configuration file format. Therefore, blindly removing everything after

#

is not always safe.

A Practical Command to Remember

For most Unix/Linux configuration files where full-line comments begin with

#

, this single command is usually enough:

grep -vE '^[[:space:]]*(#|$)' /path/to/config

For larger files:

grep -vE '^[[:space:]]*(#|$)' /path/to/config | less

This is a simple way to quickly inspect active configuration parameters without unnecessary visual noise.

Important: this command only filters the text for easier viewing. It does not determine whether a configuration option is actually active or applied by the service. Configuration files may contain

include

directives, overridden parameters, conditional sections, or default values. To inspect the effective configuration, use service-specific tools such as

nginx -T

,

sshd -T

,

apachectl -S

, and similar commands.

Leave a Reply

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