Transferring multiple files from one folder to another using Terminal

terminal

I am a new user of macOS, so please accept my apology in advance if my question sounds very trivial or stupid.

I have a folder that contains 13000 images. I want to transfer those images into three different folders in such a way that five thousand images go into two different folders each and remaining 3 thousand go into the third folder.

After searching for a while, I used the following command to transfer first 5000 images but it didn't work. Here are my steps:

  1. I went into the source folder that contains the images

  2. Used this command to copy the images cp head -5000 ../Paras_valid, but it did not work.

Could anyone help me how to copy the multiple files into different folders?

Best Answer

This is more difficult than it may seem. The following assumes that all images to be copied are in the same directory (no sub directories).

cd "PATH/TO/Paras_all"
count=0
find . -maxdepth 1 -iname '*.png' -print | while IFS= read line; do
    if [[ $count -lt 5000 ]]; then
        echo cp "$line" ~/"Documents/TARGET1"
    elif [[ $count -lt 10000 ]]; then
        echo cp "$line" ~/"Documents/TARGET2"
    else
        echo cp "$line" ~/"Documents/TARGET3"
    fi
    ((count++))
done

Replace -name '*.JPG' to mach your images and TARGET1 etc by the actual names. If you run the code like that it will just print the commands. Remove the echo if the commands seem to be correct.

PS: It's probably easiest to put everything in a file with #!/bin/bash in the first line, make the file executable with chmod +x NAMEOFFILE and then run ./NAMEOFFILE.