Linux – How to symlink folders and exclude certain files

excludehierarchylinuxsymlink

I'm not a server guru (unfortunately) but have a decent knowledge of linux & bsd.
I'm trying to symlink multiple instances of HLDS (game server) but need to exclude certain folders & config files to achieve this properly. I need to do it this way as HLDS loads many mods automatically, and putting an exception to disable the mods doesnt work for all of them.

so basically i want:

/home/user/hlds-install (the base install)
/home/user/server1
/home/user/server2
etc…

and then be able to manually put any configs/mods ive excluded into the server dir's so that each server can be configured individually.

Can anyone tell me how to do this, perhaps some sort of bash script so that I can just change the targets to run it each time i want to create a new one. I have quite a number to make so doing the whole thing manually for each one definately isn't an option and im all for working smarter, not harder!

Thanks 🙂

Best Answer

I assume you really mean "symlink everything inside the folder"...

Conceptually, you want something like:

for each file in base folder not in exclude list
    create symlink in target folder pointing back to base folder
done

bash logic to achieve this goes something like

#! /bin/bash

exclude=( "foo.conf" "bar.conf" )

for file in *; do
    for (( index = 0; index < ${#exclude[@]}; index++ )); do
        if [[ ${file} != ${exclude[${index}]} ]]; then
            ln -s ${file} ${target}/${file}
        fi  
    done
done

Assuming you don't know already, the Advanced Bash-Scripting Guide should give you enough to work out what globbing you want (perhaps find) and how to convert that to a function (perhaps a recursive one).