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
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Other||Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – 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.
- Suppose that you would like to read a config file without comments. The following
grepcommand 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
grepcommand 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.
- A second way to remove all commented lines:
$ grep -o '^[^#]*' file config.conf
- 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
- Or write the changes directly back to the original file with
$ grep -o '^[^#]*' file config.conf | sponge config.conf
- Yet another possibility is to use the
sedcommand. Add the
-ioption 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
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
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.