Reputation: 33
I am writing a bash script to find image files and to copy them to a dir. My problem is when I use find it finds all files but when I add
-exec cp -n {} $2 \;
I know that I have set do not clobber but my test case takes this into account I also have a renaming mechanism in place later in the script that is tried and tested.
When I enter straight in terminal I get same problem
find Desktop -iname '*.JPG' -o -iname '*.jpeg' -o -iname '*.jf[il1]f' -o -iname '*.exif' -o -iname '*.tiff' -o -iname '*.gif' -o -iname '*.bmp' -o -iname '*.png'
I get (my test case 300 images in different dir with duplicates and same named file but for this problem simplified to these files on my desktop)
Desktop/IMG_0316.PNG
Desktop/IMG_0191.JPG
Desktop/IMG_0292.JPG
Desktop/IMG_0269.PNG
Desktop/IMG_0318.PNG
Desktop/IMG_0172.JPG
when I add cp command
find Desktop/ -iname '*.JPG' -o -iname '*.jpeg' -o -iname '*.jf[il1]f' -o -iname '*.exif' -o -iname '*.tiff' -o -iname '*.gif' -o -iname '*.bmp' -o -iname '*.png' -exec cp -n {} test \;
The only files I get in test are
Desktop/IMG_0316.PNG
Desktop/IMG_0269.PNG
Desktop/IMG_0318.PNG
I have tried removing -n from cp but made no diffrence Thanks any help is appreciated
If I add echo I get
cp -n Desktop/IMG_0316.PNG test
cp -n Desktop/IMG_0269.PNG test
cp -n Desktop/IMG_0318.PNG test
Upvotes: 3
Views: 1214
Reputation: 17455
Use parens
find Desktop/ \( -iname '*.JPG' -o -iname '*.jpeg' -o -iname '*.jf[il1]f' -o -iname '*.exif' -o -iname '*.tiff' -o -iname '*.gif' -o -iname '*.bmp' -o -iname '*.png' \) -exec cp -n {} test \;
Actually, -exec
is an action while -iname
is a predicate (a test in terms of man find
). They aren't interchangeable at all. Default action is -print
. So your expression means:
find Desktop -iname '*.JPG' -print \
-o -iname '*.jpeg' -print -o ... -o -iname -iname '*.png' -exec cp...
By using parens your group tests and the provide a single action for the entire group.
Upvotes: 3