cp RAW files if JPEG file present

hi guys and girls,

i have a folder containing RAW and JPG images. eg...

001.jpg
003.jpg
005.jpg
001.raw
002.raw
003.raw
004.raw
005.raw

I want to copy only RAW files that have a corresponding JPG file in to a new folder. the jpg files do not need to be copied.

in this example i need to copy

001.raw
003.raw
005.raw

in to a subfolder.

i have thousands RAW and JPG images and i do not have patience to do this manually :frowning:

please help.

Run it in the directory containing image files:

ls *.jpg | perl -lne 's/\..*//;system "cp $_.raw /copy/where"'

Change the red part to the path that you want your files to be copied to.

1 Like
ls *.jpg *.raw |
awk -F'.'  ' arr[$1]++ 
             END {for (i in arr) { if(arr==2) {print i ".raw ./subdirectory" } ' > filecpy
chmod +x filecpy
# review what is in the filecpy file before you do this next step
./filecpy

thank you for the prompt reply, will try that when i get home and let you know how it went :slight_smile:

---------- Post updated at 02:09 PM ---------- Previous update was at 02:08 PM ----------

thank you jim, i will try your suggestion too, much appreciated.

Run the below command from the dir containing jpg and raw files

find . -maxdepth 1 -name "*.jpg" -o -name "*.raw" | sort |awk -F"[/.]" '/jpg/{j[$3]++;next} j[$3]{print "cp ",$0," ./subdir/"}' | while read line; do $line; done
for f in *.raw; do
        [ -f "${f%raw}"jpg ] && cp "$f" subdir
done

Regards,
Alister

bartus11 that worked a treat, thank you.

thanks to all the other guys who contributed too, unfortunately the otherscripts did not work. i think i may have made a mistake with the other scripts.