skip to Main Content

I need a short version of this script

cp apache-conf/site1.conf /etc/apache2/sites-available
cp apache-conf/site2.conf /etc/apache2/sites-available
cp apache-conf/site..N.conf /etc/apache2/sites-available
a2ensite site1
a2ensite site2
a2ensite site..N

I can cp apache-conf/*.conf /etc/apache2/sites-available but how about enabling each of them?

2

Answers


  1. Please Use the following command

    find /etc/apache2/sites-available/ -type f -and -not -name "*default*" -exec a2ensite {} ;

    This finds all your configuration files that are not having “default” in their name and activates them.

    If you face any errors, please have a look at this thread
    Credits: https://askubuntu.com/questions/916377/how-to-enable-all-site-confs-with-a2ensite-while-passing-over-000-default-conf/917701

    Login or Signup to reply.
  2. You can make use of the powerhouse built-in utils in bash that allows globbing with the number on the part of the filename

    # To avoid un-expanded globs being treated as valid entries
    shopt -s nullglob
    
    for file in apache-conf/site[0-9]*.conf; do
        cp -- "$file" /etc/apache2/sites-available
    done
    

    Assuming N is static and not known at run-time, you can do something as below. Consider a case of being it 10

    for file in apache-conf/site{0..10}.conf; do
        cp -- "$file" /etc/apache2/sites-available
    done   
    

    and for running the command a2ensite on the destination path

    for file in /etc/apache2/sites-available/*.conf; do
        a2ensite "$(basename -- "$file")"
    done
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search