Linux / UNIX View Only Configuration File Directives ( uncommented lines of a config file )

by on May 29, 2008 · 7 comments· LAST UPDATED February 18, 2011

in , ,

Most Linux / UNIX configuration files are documented using comments, but some time I just need to see line of configuration text in a file. How can I view just the uncommented configuration file directives from squid.conf or httpd.conf file?

To view just the uncommented lines of text in a config file use the grep, sed, awk, perl or any other text processing utility provided by UNIX / BSD / Linux operating systems.

grep Command Example

GNU/grep command can be used as follows:
$ grep -v "^#" /path/to/config/file
$ grep -v "^#" /etc/apache2/apache2.conf

Sample outputs:

 
ServerRoot "/etc/apache2"
 
LockFile /var/lock/apache2/accept.lock
 
PidFile ${APACHE_PID_FILE}
 
Timeout 300
 
KeepAlive On
 
MaxKeepAliveRequests 100
 
KeepAliveTimeout 15
 
 
<IfModule mpm_prefork_module>
    StartServers          5
    MinSpareServers       5
    MaxSpareServers      10
    MaxClients          150
    MaxRequestsPerChild   0
</IfModule>
 
<IfModule mpm_worker_module>
    StartServers          2
    MinSpareThreads      25
    MaxSpareThreads      75
    ThreadLimit          64
    ThreadsPerChild      25
    MaxClients          150
    MaxRequestsPerChild   0
</IfModule>
 
<IfModule mpm_event_module>
    StartServers          2
    MaxClients          150
    MinSpareThreads      25
    MaxSpareThreads      75
    ThreadLimit          64
    ThreadsPerChild      25
    MaxRequestsPerChild   0
</IfModule>
 
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}
 
 
AccessFileName .htaccess
 
<Files ~ "^\.ht">
    Order allow,deny
    Deny from all
    Satisfy all
</Files>
 
DefaultType text/plain
 
 
HostnameLookups Off
 
ErrorLog /var/log/apache2/error.log
 
LogLevel warn
 
Include /etc/apache2/mods-enabled/*.load
Include /etc/apache2/mods-enabled/*.conf
 
Include /etc/apache2/httpd.conf
 
Include /etc/apache2/ports.conf
 
LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent
 
CustomLog /var/log/apache2/other_vhosts_access.log vhost_combined
 
 
 
Include /etc/apache2/conf.d/
 
Include /etc/apache2/sites-enabled/
 

To suppress blank lines, enter:

 
egrep -v "^#|^$" /etc/apache2/apache2.conf
 

sed Command example

GNU / sed command can be used as follows:
$ sed '/ *#/d; /^ *$/d' /path/to/file
$ sed '/ *#/d; /^ *$/d' /etc/apache2/apache2.conf

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 7 comments… read them below or add one }

1 Jeff Schroeder May 29, 2008 at 2:11 pm

Thats a trick I’ve been using for years, but have came up with a better way to do it than you:

egrep -v ‘^#|^$’ /etc/httpd/conf/httpd.conf

Your sed line does the exact same thing in removing empty lines. Egrep is more concise.

Reply

2 sss December 31, 2010 at 11:08 am

# egrep -v ‘^#|^$’ dovecot.conf
-su: ^$’: command not found

not quite foolproof :)

Reply

3 Raj May 29, 2008 at 2:42 pm

Same trick here but with cat -s

grep -v “^#” httpd.conf | cat -s

Indeed, jeff has demonstrated a better way.

Reply

4 Jeff Schroeder December 31, 2010 at 3:18 pm

@sss: Clearly… you aren’t typing the command the same as I did. Why? Because the pipe is being interpreted by the shell and is trying to run it. That tells me that you didn’t actually put it in the single quotes aka ‘ ‘ like your command says. If you did, the shell would not be able to interpret it. Well unless your shell is something really really buggy and I don’t think thats the case as too much would break.

Reply

5 Philip October 14, 2011 at 6:36 pm

Wow, Jeff coming back literally years later to correct sss, hah

Reply

6 Guy Incognito July 18, 2013 at 3:31 pm

Look closer.
sss posted his reply to Jeff’s original comment years after the OP was made. Jeff responded to sss a matter of hours after sss made his reply.

Who me? I’m just passing by… =P

Reply

7 ntk January 28, 2012 at 3:27 pm

hi, is there a table comparing the syntax between regex in sed and grep? Otherwise could you help me to write in sed the command, check a string, that it should contains cafe, it should have no more then 6 alpha characters and not 3 immediate repeating of the number, or char like cafewaterloo, is ok, caffe111waterloo is not allowed, but cafe 101 waterloo is ok for example

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , , , ,

Previous Faq:

Next Faq: