Symlink Exclude in Bash Script

1k views Asked by At

I'm trying to write a Bash Script that will create symlinks but exclude certain files.

I already looked up this thread but it doesn't help me:

https://serverfault.com/questions/165484/how-to-symlink-folders-and-exclude-certain-files

So this is the script I'm using at the moment:

#! /bin/bash
target=/home/csgo/game/output
cs=/home/csgo/game/csgo-deagle1

exclude=( "*.conf" "*.cfg" "*txt" "*.ini" "*.smx" "*.mp3" "*.sh" )    

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

The script should look in the exclude list and if the extension is excluded it should not make a symlink; it should copy the file into place instead.

At the moment the script creates a symlink of the directory but everything inside it is copied.

1

There are 1 answers

12
tripleee On

You are comparing against the literal strings in the exclude list. The file name is not literally equivalent to *.conf so the comparison returns false.

Anyway, I would skip the array and just use case.

#! /bin/bash

# I don't see the value of these variables, but more power to them
target=/home/csgo/game/output
cs=/home/csgo/game/csgo-deagle1

# Hmmm, this will only loop over the actual directory name.
# Do you mean for file in "$cs"/* instead?
for file in $cs; do
    case $file in
      *.conf | *.cfg | *txt | *.ini | *.smx | *.mp3 | *.sh )
        cp "$file" "$target";;
      * )
        ln -s "$file" "$target";;
    esac
done

Notice also the proper quoting of "$file" and, generally, any variable which contains a file name.

(There is now nothing Bash-specific in this script, so you could change the shebang to #!/bin/sh, too.)

A less intrusive change would be to use =~ instead, but then you have to switch to regexes instead of glob patterns for the exclude list. But the approach above is more efficient as well, as it avoids the explicit inner loop.