I am writing a script to obtain a list of file names from a text file, so that it can find the files and copy them to a specific folder.
When I run the script, I get the following error:
./findfile.sh: line 8: spam.txt: syntax error: invalid arithmetic operator (error token is ".txt")
Why would Bash think that this should be an integer, and how can this be fixed?
#!/bin/bash
#Find files from a list in a file and copy them to a common folder
mapfile -t filelist < filelist.txt
for i in ${filelist[i]}
do
xargs find ~ -name '${filelist[i]}' | cp --parents ~/Documents/foundfiles/${filelist[i]}
done
Thanks, Jesse. Changing ${filelist[i]} to ${filelist[@]} fixed the integer problem!
Now, I have a brand new problem. The CP command wants a destination file name. I want the command to keep the old file name. The script does not recognize my code as a file name. What am I missing?
cp: missing destination file operand after '/home/crashwny/Documents/foundfiles/${filelist[@]}'