Remove or ignore all comment lines from Linux config files

Files on a Linux system often have many lines that are reserved just for comments. In other words, lines that begin with a # character. This is especially true for Linux configuration files.

When sifting through these files to see what settings they contain, the amount of comment lines can interfere, as they often outnumber the lines that actually contain settings. In such a case, it is nice to have a way to view the file without the comment lines in your output.

There can also be situations where you need to uncomment all of these lines, or in other words remove the leading # character from every comment line. There are several Linux commands that we can use for this. In this tutorial, you will see how to remove or ignore all comment lines from Linux config files.

In this tutorial you will learn:

  • How to view only the uncommented lines of a file in Linux
  • How to uncomment all lines of a file
  • How to remove all empty lines from a file
The wc command shows that our config file contains 53 lines, but we are able to see only the uncommented lines by using grep command
The wc command shows that our config file contains 53 lines, but we are able to see only the uncommented lines by using grep command
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software sed, grep
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

How to Remove or ignore all comment lines from Linux config files




The commands below will show you how to view the uncommented lines of a file and ignore the commented lines. You will also see how to permanently remove the commented lines from a file, as well as remove all empty lines. The idea is to make it easier to sift through a configuration file for the relevant settings by suppressing all of the commented lines.

  1. Suppose that you would like to read a config file without comments. The following grep command assumes that each comment starts with a single # character at the beginning of the line. To remove or ignore all comments from a file, the following grep command can be used:
    $ grep -v ^\# config.conf | grep  .
    

    Note that we also pipe to grep . in this example in order to suppress the output of all empty lines. This way we only see the settings in our terminal.

  2. A second way to remove all commented lines:
    $ grep -o '^[^#]*' file config.conf
    
  3. What if you want to remove all commented lines plus write the changes to a file? In that case you can redirect output with > operator to a new file.
    $ grep -o '^[^#]*' file config.conf > updated_config.conf
    
  4. Or write the changes directly back to the original file with sponge.
    $ grep -o '^[^#]*' file config.conf | sponge config.conf
    
  5. Yet another possibility is to use the sed command. Add the -i option if you want to make permanent changes to the file, rather than just seeing the output without commented lines.
    $ sed '/^[[:blank:]]*#/d;s/#.*//' config.conf
    


Closing Thoughts

In this tutorial, we saw how to view a config file with only its uncommented lines on a Linux system. We also learned how to remove commented lines completely by using grep or sed commands, as well as removing empty lines from a file. These commands help Linux administrators to quickly sift through configuration files to only see the relevant lines, while suppressing the commented lines from output.



Comments and Discussions
Linux Forum