简体   繁体   中英

Bash Script - For loop using find -exec cp produces error: cp: omitting directory

Background

I have two folders of images... FOLDER ONE is the main folder with thousands of colored images. FOLDER TWO is a filtered folder of things that I've been looking for throughout FOLDER ONE, but the images in FOLDER TWO are in black and white.

Special Note – the file names of the images in FOLDER TWO are the same as in FOLDER ONE + some other info afterwards (but before the .jpeg extension).

My goal

Search FOLDER ONE for the beginning part of the name of the files in FOLDER TWO (so that I can get the colored copy of the images I've filtered), and then copy those images to another folder called PASS FOLDER.

My code

note – FOLDER ONE, TWO, PASS FOLDER = F1, F2, PASSED (respectively)

for doc in F2/*
do
    name=`basename $doc`;
    name=${name%-*};
    find /home/user/F1/ -name "$name*" -exec cp {} /home/user/PASSED/ \;;
done

My problem

I keep getting the error

cp: omitting directory '/home/user/F1/'

Please help! I think this should be very simple to do, it's just consistently having problems. I've also tested each individual part of the for loop on its own, and they work. I think the for loop is messing something up, but I don't see why.

The problem is that find is handing cp directories to copy and you only want to copy files. Thus, replace:

find /home/user/F1/ -name "$name*" -exec cp {} /home/user/PASSED/ \;

With:

find /home/user/F1/ -type f -name "$name*" -exec cp {} /home/user/PASSED/ \;

Thw predicate -type f tells find to match only for regular files, not directories.

just add -r parameter. after cp

find /home/user/F1/ -name "$name*" -exec cp -r {} /home/user/PASSED/ \;;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM