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?

  • Welcome to Ask Different :) What is the name of the source folder and what are the names of the destination folders. What are their locations/path relative to the source folder? – Nimesh Neema Jun 7 at 9:12
  • Name of the source folder is Paras_all and both source and destination folder are located in the a folder called cell_images which is placed in Documents folder – user2293224 Jun 7 at 9:15

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"
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"
        echo cp "$line" ~/"Documents/TARGET3"

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.

| improve this answer | |
  • Great solution by @nohillside — though since many JPEGs have the file extension in lowercase and/or include an E, I'd recommend changing -name '*.JPG' to \( -iname \*.jpg -o -iname \*.jpeg \). – John Leary Jun 7 at 20:06
  • @nohillside Thanks for the solution. However, I dont under the changing of file -name parameter. The images I got have png extension and they have name like C33P1thinF_IMG_20150619_114756a_cell_179, C33P1thinF_IMG_20150619_114756a_cell_180, C33P1thinF_IMG_20150619_114756a_cell_181, something like that. Could you please guide me? – user2293224 Jun 8 at 9:35
  • @user2293224 I've updated the answer to match all files ending in .png/.PNG. You can change the pattern to whatever you need, it works the same as in the shell itself. – nohillside Jun 8 at 9:40
  • Thanks, last thing before I run the script. In the first line where it is written "cd "PATH/TO/Paras_all" does it replace with "cd actual_path_of_Paras_all" or is it should be as it is written in the answer?? – user2293224 Jun 8 at 9:47
  • 1
    @user2293224 You need to replace both the path in the first line and the ones ending in TARGET1/2/3 with the actual names. – nohillside Jun 8 at 9:48

you can first go to Finder and open the folder with all the images. I would select all the files and right-click. One of the options you'll see when you right-click is to Rename 13000 files. If you click on this option you'll get a pane where you can rename the files in sequential order. You can define the first part of the name and it will add the sequential part.

Now create a folder for the first 5000 files. You can just select the first 5000 files and and create a shell script that runs from 1 to 5000 and run it from the new empty folder. The command that sets run 5000 times is, from the new directory, "cp ../old_folder_name/files_name_inum *".This will copy files *_00001 to *_05000 to you first new directory. Then make a 2nd directory and have you script run from *_05001 to *_10000 and finally run a third time to get the files 10001 to 13000.

The actual unix shell script is the following

for i in {0001..5000}
   cp ../old_imager_files/image_files_$i target1/
| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .