Moritz Schmid
Moritz Schmid

Reputation: 395

Finding files from list and copying them into new directory

I want to find files via the command line terminal for OS X and then copy the files that were found to a new directory. The filenames of the files I want to find are in a listexample.txt file and there are about 5000 file names in there.

The file listexample.txt looks like this:

1111 00001 55553.bmp
1113 11312 24125.bmp
…

I tried around with thing like this:

find /directory -type f "`cat listexample.txt`" -exec cp {} …

but can't get it to run.

I have now this, but it doesn't work:

cat listexample.txt | while read line; do grep "$line" listexample.txt -exec find /directorya "$line" -exec cp {} /directoryb \; done

The idea is to read the lines of list example.txt, then take the line with grep, find the file in directory a and then copy the found file over to a new directory b. I think because of the nature of my file names, see above, there is a problem with spaces in the name as well.

I also started this approach to see what is going on, but didn't get far.

for line in `cat listexample.txt`; do grep $line -exec echo "Processing $line"; done

Upvotes: 0

Views: 4892

Answers (1)

Moritz Schmid
Moritz Schmid

Reputation: 395

Here's the solution for a find and copy script (copy.sh) in case somebody has a similar problem:

First, give rights to the script with: chmod +x fcopy.sh Then run it with: ./fcopy.sh listexample.txt

Script content:

#!/bin/bash
target="/directory with images"

while read line
do
    name=$line
    echo "Text read from file - $name"
    find "${target}" -name "$name" -exec cp {} /found_files \;    

done < $1

Cheers

Upvotes: 6

Related Questions