How to View Configuration Files Without Comments in Linux

Are you looking through an extremely lengthy configuration file, one with hundreds of lines of comments, but only want to filter the important settings from it. In this article, we will show you different ways to view a configuration file without comments in Linux.

Read Also: ccat – Show ‘cat Command’ Output with Syntax Highlighting or Colorizing

You can use the grep command to for this purpose. The following command will enable you view the current configurations for PHP 7.1 without any comments, it will remove lines starting with the ; character which is used for commenting.

Note that since ; is a special shell character, you need to use the \ escape character to change its meaning in the command.

$ grep ^[^\;] /etc/php/7.1/cli/php.ini
View Files Without Comments

View Files Without Comments

In most configuration files, the # character is used for commenting out a line, so you can use the following command.

$ grep ^[^#] /etc/postfix/main.cf

What if you have lines starting with some spaces or tabs other then # or ; character?. You can use the following command which should also remove empty spaces or lines in the output.

$ egrep -v "^$|^[[:space:]]*;" /etc/php/7.1/cli/php.ini 
OR
$ egrep -v "^$|^[[:space:]]*#" /etc/postfix/main.cf
View Files Without Spaces

View Files Without Spaces

From the above example, the -v switch means show non-matching lines; instead of showing matched lines (it actually inverts the meaning of matching) and in the pattern “^$|^[[:space:]]*#”:

  • ^$ – enables for deleting empty spaces.
  • ^[[:space:]]*# or ^[[:space:]]*; – enables matching of lines that starting with # or ; or “some spaces/tabs.
  • | – the infix operator joins the two regular expressions.

Also learn more about grep command and its variations in these articles:

  1. What’s Difference Between Grep, Egrep and Fgrep in Linux?
  2. 11 Advanced Linux ‘Grep’ Commands on Character Classes and Bracket Expressions

That’s all for now! We would love to hear from you, share with us any alternative methods for viewing configuration files without comments, via the feedback form below.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

Ravi Saive

I am Ravi Saive, creator of TecMint. A Computer Geek and Linux Guru who loves to share tricks and tips on Internet. Most Of My Servers runs on Open Source Platform called Linux. Follow Me: Twitter, Facebook and Google+

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

8 Responses

  1. Rishi says:

    Many thanks for this great and useful tip.

  2. Stoyan Stoyanov says:

    egrep -v “^#|^$” file.conf

  3. David says:

    You can exclude blank lines with the following statement:

    # grep -v '^$' /etc/chrony.conf
    

Got something to say? Join the discussion.

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

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