I have a text file called file.txt which has a list of file as shown below that i need to find and copy to a particular location.
FILENAMES
skter.pdf
abcdf.sas
tereen.lst
abc12.txt
i am using following code and it never works however i try it.
cat file.txt | while read FILENAME;do find . -name "$FILENAME" -type f -print -exec cp '{}' /testdir /; done;
Also point to consider while doing this is there is possibility of have 1 file in many directory in that case need to copy them to target folder like file1 file2
Dear Otheus,
Thank you for your quik, solution but still it does not work and the code dose not seem complete when i execute them. it expects something more not quite sure what.
I was missing "done" in my code. I Corrected the post. But Scrutinizer's post is also correct and more efficient. Both commands will fail, by the way, if there are too many files and the command line is too long. In that case you'd use xargs with my solution:
tar cf $BACKUP
cat file.txt | while read FILENAME;do find . -name "$FILENAME" -type f -print;done |
{ read onefile; tar cf $BACKUP $onefile; xargs -r tar rf $BACKUP ; }
xargs tries to fill the command line with as many parameters as possible, invoking "tar rf $BACKUP " as a prefix to each command. If there are too many command-line parameters, it runs another invocation of the "tar rf $BACKUP" command with the rest of the parameters. The "r" option of tar adds to an existing tar file, so we first have to create it using the first file on the list. So we "read onefile" to get the first filename from the find operation. We then create the archive file using $onefile as the only argument. Now xargs can do its job properly.