0

How do I find multiple files of certain criteria and then copy them to a certain directory

I am working of this find -name "AAVD*" -exec cp "{}" ./forShaq1/ \; and this works.

But how do I do it for a find like this:

$ find -name "ACMB*" -o -name "AAVD *" -o -name "SOCK*" -o -name "SMNK*" -o -name "APRN*" -o -name "WAPP*" -o -name "AABS*" -o -name "ATRN*"
./Atempt3A/ACMB-modified.txt
./Atempt3A/APRN-modified.txt
...

appending -exec cp "{}" ./forShaq1/ \; at the end does not seem to work.


Below for my ref

##copy multiple files with find
>>
$ find -name "ACMB*" -o -name "AAVD *" -o -name "SOCK*" -o -name "SMNK*" -o -name "APRN*" -o -name "WAPP*" -o -name "AABS*" -o -name "ATRN*"
./Atempt3A/ACMB-modified.txt
./Atempt3A/APRN-modified.txt
./attempt12_apr_2023/APRN-modified.txt
./Attempt2/164_sites/env_scripts/ACMB-modified.txt
./Attempt2/179ish_sites_20220914/ACMB-modified.txt
./Attempt2/179ish_sites_20220914/APRN-modified.txt
./Attempt2/additional_sites_20220914/APRN-modified.txt
./Attempt5_Dec_2022/scripts/ACMB-modified.txt
./Attempt5_Dec_2022/scripts/APRN-modified.txt
./Attempt5_Dec_2022/scripts - added_morefrom_again_folder/ACMB-modified.txt
./Attempt5_Dec_2022/scripts - added_morefrom_again_folder/APRN-modified.txt
./Attempt6_Jan2023/env_scripts/SMNK-modified.txt
./Attempt6_Jan2023/env_scripts/SOCK-modified.txt
./attempt9_march_2023/env_scripts/AABS-modified.txt
./forShaq1/AABS-modified.txt


$ ls forShaq1/
AABS-modified.txt

##tring to copy multiple files after find
>> $ find -name "ACMB*" -o -name "AAVD " -o -name "SOCK" -o -name "SMNK*" -o -name "APRN*" -o -name "WAPP*" -o -name "AABS*" -o -name "ATRN*" -exec cp "{}" ./forShaq1/ ;

>>
$ ls forShaq1/
AABS-modified.txt

>>
$ find -name "AAVD*"
./attempt9_march_2023/env_scripts/AAVD-modified.txt

## how to copy 1 file at a time after find - note cp: error is cos I am copying files to dir to where I am finding
>>
$ find -name "AAVD*" -exec cp "{}" ./forShaq1/  \;
cp: './forShaq1/AAVD-modified.txt' and './forShaq1/AAVD-modified.txt' are the same file

>>
$ ls forShaq1/
AABS-modified.txt  AAVD-modified.txt
Barmar
  • 741,623
  • 53
  • 500
  • 612
HattrickNZ
  • 4,373
  • 15
  • 54
  • 98
  • 1
    Use the `-t` option to put the destination directory first: `-exec cp -t destination {} +` – Barmar Aug 10 '23 at 00:04
  • 1
    You have to group the conditions, like `find . \( -name "ACMB*" -o ... -o -name "ATRN*" \) -exec cp "{}" ./forShaq1/ \;` because of the way precedence works. See ["Find Command with multiple file extensions"](https://stackoverflow.com/questions/47339376/find-command-with-multiple-file-extensions). – Gordon Davisson Aug 10 '23 at 00:41

0 Answers0