I am wondering if there are any tools for use in bash scripts that can easily allow you to manipulate (add, remove, update) directives inside conf files?

e.g. fail2ban.conf has grouped directives, each under their own sections.

enabled  = true
port     = ftp,ftp-data,ftps,ftps-data

enabled  = true
port     = smtp,465,submission

whereas pagespeed.conf has mixed directives, some in their own sections and some arrayed all over.

<Location /pagespeed_admin>
        Order allow,deny
        Allow from localhost
        Allow from
        SetHandler pagespeed_admin
    <Location /pagespeed_global_admin>
        Order allow,deny
        Allow from localhost
        Allow from
        SetHandler pagespeed_global_admin

    ModPagespeedMessageBufferSize 100000
    ModPagespeedStatisticsLogging on
    ModPagespeedEnableCachePurge on
    ModPagespeedPurgeMethod PURGE
    ModPagespeedFileCacheSizeKb 2048000
    ModPagespeedFileCacheCleanIntervalMs 3600000
    ModPagespeedFileCacheInodeLimit 500000

You get the idea.

Are there any tools that allow you to : e.g. manipulate the 'enabled' directive in the 'proftpd' section of fail2ban.conf?

or manipulate the 'Allow from' directive in the 'Location /pagespeed_admin' section of pagespeed.conf?

or manipulate the 'ModPagespeedMessageBufferSize' directive in the 'no particular' section of pagespeed.conf?


closed as too broad by Romeo Ninov, muru, rush, mosvy, jimmij May 28 at 17:22

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Your question is way too broad. And probably you don't want to manipulate config files from bash. Check automated solutions for it like ansible, salt, etc. These tools allow much more flexibility, allow you to manage multiple servers at a time and you can move almost anything to variable to rule your configuration. – rush May 27 at 9:32
  • I disagree. The question is quite to the point. If there is nothing out there in the form of a simple script or command that you are aware of, then fine. But to suggest that we need a paid solution to modify conf files is overkill. Thanks for the suggestion but this is not what I asked for. – conanDrum May 27 at 10:32
  • 1
    ansible and salt are not a paid solution. They are open source industry wide used configuration management tools. – rush May 27 at 10:38
  • 1
    git-config and postconf are the 2 tools I know of. Normally I use regex; perl -pi -e 's///g' file – user1133275 May 27 at 11:27
  • 1
    the way people usually do it is by using a unified conf dir with files in a simple format, and generate from it temporary conf files in the idiosincratic format expected by each app, which could be easily done even in shell. Look at OpenWRT with its UCI. A configuration tool able to modify all kind of configuration files (xml, ini, etc) will be more complicated and brittle than everything ever written. – mosvy May 28 at 1:18

No, for the simple reason that there are just too many formats a configuration file might use. The best you can do, I think, is to roll your own using text-processing tools.

There may be libraries for some formats (JSON, for example) but tool authors are free to use whatever format they feel like so a general tool will not be possible. Just to illustrate the point, here are a few lines from a few conf files:

  1. emacs

    ;; Are we running XEmacs or Emacs?
    (defvar running-xemacs (string-match "XEmacs\\|Lucid" emacs-version))
    ;; disable menu bar when running in terminal
    (when (not (display-graphic-p))
      (menu-bar-mode -1))
    ;;My libraries, ebib, wordcount etc
    (add-to-list 'load-path "~/.emacs-lisp/")
  2. vi

      highlight Normal guibg=grey90
      highlight Cursor guibg=Green guifg=NONE
      highlight lCursor guibg=Cyan guifg=NONE
      highlight NonText guibg=grey80
      highlight Constant gui=NONE guibg=grey95
      highlight Special gui=NONE guibg=grey95
  3. tint

    # general panel settings
    rounded = 7
    border_width = 2
    #background_color = #B4B2B2 10
    background_color = #000000 30 
    border_color = #8F0004 0 
  4. vlc

    # Trigger button (string)
    [motion] # motion control interface
    [oldrc] # Remote control interface
    # Show stream position (boolean)
  5. sshd

    AuthorizedKeysFile  .ssh/authorized_keys
    ChallengeResponseAuthentication no
    UsePAM yes

etc., etc.

  • again I disagree. what you posted above is just a simple scenario of key-value pairs. Nothing complicated to accomplish. Look for key.... paste value. done. – conanDrum May 27 at 10:46
  • Maybe my question is being misinterpreted. I am not asking for an all knowing tool. I am asking for a tool which can take these key value pairs and it will go in the conf and replace them. – conanDrum May 27 at 10:47
  • 1
    @conanDrum what conf? What format? You show two very different formats in your question. I am just trying to illustrate that there is no one standard format, so what you're asking for is not very clear. Conf files can use completely arbitrary formats devised by their tool's author. – terdon May 27 at 10:48
  • thanks, and I am trying to say that we don't care about format if the user of the tool is aware of it and provides it as input to the tool. I am guessing noone thought of that before. – conanDrum May 27 at 10:50
  • @conanDrum you are assuming that is possible, but it isn't. Not unless the tool is using some sort of standard format like pagespeed.conf which is using XML. However, many conf files will have arbitrary formats, so you would have no way of providing the format as input. There are tools that let you manipulate XML, that's something. – terdon May 27 at 10:52

Not really, but you could build something for a specific config layout. Something like

sed -E "/\[proftpd]/,/\[/{s/(enabled\s*=\s*).*/\1false/}" input.file

could be designed as a function to change fail2ban.conf, as an example.

I could expand if at all interesting.

  • doing this already thank you for commenting – conanDrum May 27 at 13:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.