Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

How to remove first n character of multiple file names in mac finder

I want to rename multiple files to that the first 9 characters are deleted. I couldn't use the replace rename because the first part of each of the file name is unique.


example:


BEFORE

19.49.29 1.jpeg

19.50.17 2.jpeg

19.50.24 3.jpeg

19.50.28 4.jpeg

.

.

AFTER

1.jpeg

2.jpeg

3.jpeg

4.jpeg

.

.


is there a way using the built in app or maybe anything

MacBook Pro 13", macOS 10.15

Posted on May 22, 2020 9:12 AM

Reply
Question marked as Best reply

Posted on May 23, 2020 5:07 AM

From your /Applications folder, double-click Automator (robot icon). It will present a chooser, and you select Desktop as the location. Then click New Document, and that will lead to another panel, where you want to select Application as the type of Automator document, and then click choose.


On the left will be libraries that contain actions. Do the following:

  1. Select library Files and Folders -> drag the Ask for Finder Items action to the large, right workflow window and drop it.
    1. Start at: Desktop
    2. Type: Folders
  2. Select library Files and Folders -> drag Get Folder Contents action, and drop below the previous action. If you have multiple sub-folders within the target folder, then check Repeat for each subfolder found, otherwise leave that unchecked.
  3. Select library Utilities -> drag Run Shell Script and drop below the preceding actions.
    1. Shell: /bin/zsh
      1. Note: The screen capture of the Run Shell Script action below, shows /bin/bash, and it should have shown /bin/zsh for the shell selection.
    2. Pass input: as arguments
    3. Select all of the content in this window and remove it.
    4. Click on the black border near the bottom of the action, and then grab the blue border and drag downward to enlarge the window.
    5. Copy/paste the script that I posted earlier into this Run Shell Script window.
  4. Save the Automator application to your Desktop with a useful name (e.g remove9.app).


It will have a workflow that looks like this:


You then double-click the Automator application on your Desktop. It will accept a single folder choice containing the files you want. The second Automator action will collect all of the files in the selected folder and pass them into the Run Shell Script action, where the for loop processes them individually. If the filename conforms to the regular expression match, then the capture group (remaining filename) is in the match variable and the file is renamed. If no match, then the file is ignored.


The reqular expression that I wrote (e.g. regex) will handle any length of preceding numbers, dots, and a single space prior to the captured filename, so it will handle your 9 character case, longer, or shorter without changing the code.


6 replies
Question marked as Best reply

May 23, 2020 5:07 AM in response to donjonx

From your /Applications folder, double-click Automator (robot icon). It will present a chooser, and you select Desktop as the location. Then click New Document, and that will lead to another panel, where you want to select Application as the type of Automator document, and then click choose.


On the left will be libraries that contain actions. Do the following:

  1. Select library Files and Folders -> drag the Ask for Finder Items action to the large, right workflow window and drop it.
    1. Start at: Desktop
    2. Type: Folders
  2. Select library Files and Folders -> drag Get Folder Contents action, and drop below the previous action. If you have multiple sub-folders within the target folder, then check Repeat for each subfolder found, otherwise leave that unchecked.
  3. Select library Utilities -> drag Run Shell Script and drop below the preceding actions.
    1. Shell: /bin/zsh
      1. Note: The screen capture of the Run Shell Script action below, shows /bin/bash, and it should have shown /bin/zsh for the shell selection.
    2. Pass input: as arguments
    3. Select all of the content in this window and remove it.
    4. Click on the black border near the bottom of the action, and then grab the blue border and drag downward to enlarge the window.
    5. Copy/paste the script that I posted earlier into this Run Shell Script window.
  4. Save the Automator application to your Desktop with a useful name (e.g remove9.app).


It will have a workflow that looks like this:


You then double-click the Automator application on your Desktop. It will accept a single folder choice containing the files you want. The second Automator action will collect all of the files in the selected folder and pass them into the Run Shell Script action, where the for loop processes them individually. If the filename conforms to the regular expression match, then the capture group (remaining filename) is in the match variable and the file is renamed. If no match, then the file is ignored.


The reqular expression that I wrote (e.g. regex) will handle any length of preceding numbers, dots, and a single space prior to the captured filename, so it will handle your 9 character case, longer, or shorter without changing the code.


May 22, 2020 9:40 AM in response to donjonx

Can't be done with the Finder. But you can do it in the Terminal with the following script:


#!/bin/zsh
: <<'COMMENT'

For list of files on input to the script (including *.jpeg),
rename filename without first nine characters. Ignores files
with fewer than nine leading characters.
COMMENT

for f in "$@";
do
    mv "${f}" "${f:9}"
done


Copy/paste that into a plain text file (e.g. rem9.zsh) and in the Terminal make it executable. Let's say you are already in a folder that contains these files:


chmod +x ./rem9.zsh
./rem9.zsh *.jpeg



May 22, 2020 11:18 AM in response to VikingOSX

Ok. Zsh and regex to the rescue. Tested with Zsh v5.3 (Mojave) and v5.8.

#!/bin/zsh
zmodload zsh/regex

: <<'COMMENT'

If a file matches the regex string, use the captured part of the
filename $match in the rename, otherwise ignore failed matches.

Usage: rem9.zsh *.jpeg

VikingOSX, 2020-05-22, Apple Support Communities, No Warranties expressed or implied.
COMMENT

# 19.49.29 110.jpeg would place 110.jpg in the $match variable
regex="^[0-9\.]+ (.*)$"

for f in "$@";
do
    # skip if regex does not match image name
    [[ ${f:a:t} -regex-match $regex ]] || continue
    # print ${match}
    mv "${f:a}" "${f:a:h}/${match}"
done





How to remove first n character of multiple file names in mac finder

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.