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.
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
.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.