0

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
  • 1
    for i in "${filelist[@]}" – Jesse_b May 5 '18 at 21:47
  • Please don’t move the goalposts. Ask a question, get an answer. Have a new question, write a new question. The tricky part is deciding how much of your goal to put into one question to keep it from being too broad, but you can’t go too far wrong by describing your overall goal and then explaining where you’re currently stuck. – Jeff Schaller May 5 '18 at 22:59
  • sorry! didn't know! – user289380 May 5 '18 at 23:04
0

I'm not exactly sure what you are trying to do with xargs and it seems you are missing an argument on your cp. Is ~/Documents/foundfiles/${filelist[i]} where you want to copy to?

Here is code that works for me:

#!/usr/bin/env bash

mapfile -t filelist < ~/tmp/filelist.txt

for file in "${filelist[@]}"; do
    find ~ -name "$file" -exec cp --parents {} ~/tmp/dest/ \;
done
  • I used xargs because the file list i am working with is hundreds of files, and the script would not work otherwise. Your script appears to run without errors, but is not finding or moving files. – user289380 May 5 '18 at 22:16
  • did you change ~/tmp/dest to the location you want your files copied to? – Jesse_b May 5 '18 at 22:27
  • yes. I understand more of my problem now. Was thinking that i could just pipe the found file into a CP command, and it doesn't work that way. Thank you for your help – user289380 May 5 '18 at 23:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy