I try to find files in a directory and copy them to somewhere else, this is my script:
#!/bin/bash
# fileSearch
array=(a b c d)
for var in array
do
find ~/ -name $var* -exec cp {} ./test/ \;
done
What am i doing wrong? He won’t find any files…
I think it probably will be something with quotes.
Can anyone help me?
Thanks
Try this:
Output:
In other words, you’re just looping through a word-list which contains a single word “array”. The correct version should expand the array, and add a
$-symbol (meaning to reference the variable):Also it’s a good habit to quote your variables, i.e., whenever you use a variable like
$var, use"$var"instead. This prevents many problems for when your variables contain strings with spaces (or other “strange” symbols) that should be treated as a single string.