I've been trying to get this working for a while now. I want to search for a list of files and then copy them all to a certain path.
If I run the command separately, then it works like so:
find <path> -name 'file1' -exec cp '{}' <path2> \;
But I've been unable to run it inside a for loop.
#this works
for f in file1 file2 file3...; do find <path> -name "$f" \; done;
#none of these work (this code tries to find and copy the files named file and list from the path)
for f in file1 file2 file3...; do find <path> -name "$f" -exec cp '{}' <path2> \; done;
for f in file1 file2 file3...; do find <path> -name "$f" -exec cp {} <path2> \; done;
I've tried a few other stuff that weren't likely to work. The first line in the code quote just gets stuck and the others don't copy anything even though they don't get stuck.
I haven't been able to run anything with exec inside a for loop after a search and at this point I'm not sure what to do.
I have solved the immediate problem by searching the files and logging the results to another file and then running a copy inside a for loop separately but I'd still like to know how to do this.
for
loop to work... and I don't understand why you would need to do this! If you pass the file list, why do you need tofind
the files too? why not,for f in file list; do cp "$f" <path2>; done
?for f in file1 file2 file3; do find /path -name "$f" -exec cp -vt /path/to/test/dir -- {} +; done
?