Summary
This article introduces how to batch copy photos with specified file names using terminal commands on macOS. In response to the need to select some numbered files from more than two thousand photos after wedding shooting, two solutions are provided: one is to copy directly using the file name array, and the other is to dynamically read the comma-separated numbered files and convert them into file name format. Both solutions involve creating a target folder, setting 777 permissions, and using the find and cp commands to recursively search and copy the files. This method is suitable for scenarios where you need to accurately select and transfer specified files from a large number of files. It is planned to be integrated into command line tools or software in the future.
After I took a wedding last time, I had more than 2,000 photos. I picked some today and just remembered the numbers in the names of the photos. I thought that Mac’s file search could be separated by English commas for quick search, but who knew it couldn’t be done? So now it’s time to think about it.
Not tested on Windows
Be sure to use the command
under sudo authority Preparation 1. :
- *File path*
/Volumes/婚礼/DCIM: - My photos are stored in
/Volumes/婚礼/DCIM/matched_photos。 - (contains subfolders). The
/Volumes/婚礼/DCIM/numbers.txtgoal is to copy matching files to
0182,0178,0168,0165,0163,...
- .
IMG_编号.CR2*Number list*IMG_0182.CR2)。
- : - I have a comma separated list of numbers saved in a text file :
- with the following content:
These numbers need to be converted into the file name format:
(e.g.
).
- Environment
- :
0182,0178,0168,0165,0163,...
- macOS system (I use zsh shell, macOS default shell).
/Volumes/婚礼/DCIM/numbers.txtSolution 1: Directly use the file name list
1. Prepare numbering file
Make sure your numbered list has been saved as a text file. If not, you can create it manually: IMG_XXXX.CR2Open "TextEdit". IMG_ Enter the number, for example: .CR2 is saved as
- (make sure to save in plain text format, avoid rich text).
,2. Convert the number to the file name format.CR2,IMG_。 - My photo file name format is
IMG_, so I need to add the prefix.CR2。 - and the suffix
IMG_0182.CR2,IMG_0178.CR2,IMG_0168.CR2,...
to the number. You can use the text editor's "Find and Replace" function:
looks for
and replaces it with
- .
Command + 空格Manually add
at the beginning and
at the end.
mkdir -p /Volumes/婚礼/DCIM/matched_photos && chmod 777 /Volumes/婚礼/DCIM/matched_photos && numbers=(IMG_0182.CR2 IMG_0178.CR2 IMG_0168.CR2 IMG_0165.CR2 IMG_0163.CR2) && for number in $numbers; do find /Volumes/婚礼/DCIM -type f -name "$number" -exec cp {} /Volumes/婚礼/DCIM/matched_photos/ \; ; done && chmod 777 /Volumes/婚礼/DCIM/matched_photos/*
Example results (partial):
- 3. Search and copy the file
- using Terminal commands
- Directly write the file name list to the command and set permissions:
- Open terminal "$number"Press
- , enter "Terminal" and press Enter.
- Enter the command
Copy the following command to the terminal and run (only some of the file names are shown here, the complete list contains 151):
Command description:
mkdir -p /Volumes/wedding/DCIM/matched_photos: Create the target folder.
chmod 777 /Volumes/wedding/DCIM/matched_photos: Set 777 permissions for the destination folder.
0182,0178,0168,0165,0163,...
numbers=(...): Define the file name array and use the converted file name directly.
find/Volumes/wedding/DCIM -type f -name "$number": Searches /Volumes/wedding/DCIM and its subfolders for exact file name matches.
-exec cp {} /Volumes/wedding/DCIM/matched_photos/ ;: Copy the found files to a new folder.
- chmod 777 /Volumes/wedding/DCIM/matched_photos/*: Sets 777 permissions for all copied files.
Solution 2: Dynamically read numbers.txt file
If your number list may change, or you want to manage files more flexibly, you can use numbers.txt to dynamically read numbers and generate file names.
mkdir -p /Volumes/婚礼/DCIM/matched_photos && chmod 777 /Volumes/婚礼/DCIM/matched_photos && numbers=($(cat /Volumes/婚礼/DCIM/numbers.txt | tr ',' ' ')) && for number in $numbers; do find /Volumes/婚礼/DCIM -type f -name "IMG_${number}.CR2" -exec cp {} /Volumes/婚礼/DCIM/matched_photos/ \; ; done && chmod 777 /Volumes/婚礼/DCIM/matched_photos/*
1. Prepare numbering file
- Make sure /Volumes/wedding/DCIM/numbers.txt exists, and the content is comma separated numbers:
- 2. Use terminal commands to read and process
- The following command will read numbers.txt, convert the numbers into file name format and perform a search, while setting permissions:
- 3. Open the terminal
- Press Command + Space, enter "Terminal", and press Enter.
- 4. Enter the command
- -name "IMG_${number}.CR2" Copy the following command to the terminal and run:
- Command description:
- mkdir -p /Volumes/wedding/DCIM/matched_photos: Create the target folder.
chmod 777 /Volumes/wedding/DCIM/matched_photos: Set 777 permissions for the destination folder.
- numbers=($(cat /Volumes/wedding/DCIM/numbers.txt | tr ',' ' '))
- cat reads numbers.txt.
tr ',' ' ' Replace commas with spaces to generate a zsh array.
for number in $numbers; do ...: iterate through each number (for example, 0182).
- -name "IMG_${number}.CR2": Dynamically generate the file name, such as IMG_0182.CR2.
ls /Volumes/婚礼/DCIM
-exec cp {} /Volumes/wedding/DCIM/matched_photos/ ;: Copy the found files to a new folder.
- chmod777 /Volumes/wedding/DCIM/matched_photos/*: Set 777 permissions for all copied files.
find /Volumes/婚礼/DCIM -type f | head -n 10
Advantages:
- If you need to change the number, just edit numbers.txt without modifying the command.
find /Volumes/婚礼/DCIM -type f -name "IMG_0182.CR2"
is more flexible and suitable for dynamic needs.
- FAQ
cat /Volumes/婚礼/DCIM/numbers.txt
1. Path error
- Check whether the disk is mounted:
ls -l /Volumes/婚礼/DCIM/matched_photos
. File name does not match
View the actual file name:
3. Subfolder does not contain











