0

I have a ton of files to rename where we only need the last 9 characters of the file.

The files are currently named as this: FirstName_LastName_000000000.pdf. Essentially, I want to rename the files so that they are named as such: 000000000.pdf

If somebody can assist with providing a way of doing so that will be awesome.

3
  • Are all the files in the same folder? Are the last 9 characters unique or is there the possibility that the last 9 characters could be duplicated? Aug 11, 2020 at 19:29
  • The files are in the same folder. The last 9 characters are all unique and not duplicated. Aug 11, 2020 at 19:50
  • 1
    Jonah Coloma, RE: "The files are in the same folder. The last 9 characters are all unique and not duplicated" -- If this is a one time thing, see my "Alternate answer per Comment:" at the end of my answer Aug 11, 2020 at 21:22

3 Answers 3

1

Yes, one of the ways it can be done in Automator is by using a Run Shell Script action with the example bash script code shown further below.

You can create an Automator Workflow, or Service/Quick Action, workflow.

If you do just a Workflow, then you will need to add a Files & Folders action, e.g. Get Selected Finder Items or Get Specified Finder Items, and then add a Run Shell Script action.

If you do a Service/Quick Action, then you only need a Run Shell Script action.

If you do choose a Service/Quick Action then set it to use files and folders in Finder.

For the Run Shell Script action, have its settings configured as:

  • Shell: /bin/bash
  • Pass input: as arguments
  • Replace the default code of the Run Shell Script action with the example bash script code.

The example bash script code uses shell parameter expansion to slice and dice the fully qualified pathname of the file(s) passed to it into the necessary pieces and then renames the file(s) using the mv command with the -n option, which will not overwrite an existing file, based on the values of the sliced and diced pieces of the fully qualified pathname.

As coded, it uses a regex to only act on files that have the following pattern e.g., FirstName_LastName_000000000 with any extension added, e.g.: .pdf

Example bash script code:

        # f = fully qualified pathname
        # d = directory pathname
        # fn = filename with extension
        # n = filename without extension
        # e = filename extension
        # s = filename suffix (last 9 characters of filename without extension)


    for f in "$@"
    do
        [ -f "${f}" ] || continue
        d="${f%/*}"
        fn="${f##*/}"
        [[ ${fn} =~ ^.*_.*_[0-9]{9}\..* ]] || continue
        n="${fn%.*}"
        e="${fn##*.}"
        s="${n: -9}"
        [ -n "${s}" ] || continue
        mv -n "${f}" "${d}/${s}.${e}"
    done

The output of which would be, e.g.: 000000000.pdf

If you want it to act only on .pdf files, then change:

[[ ${fn} =~ ^.*_.*_[0-9]{9}\..* ]] || continue

To:

[[ ${fn} =~ ^.*_.*_[0-9]{9}\.[pP][dD][fF] ]] || continue

Alternate answer per Comment:

The files are in the same folder. The last 9 characters are all unique and not duplicated

If this is a one time thing and you just want to open Terminal to that folder, you can use the following compound command:

for f in *.[pP][dD][fF]; do mv -nv "${f}" "$(n="${f%.*}"; printf ${n: -9}).${f##*.}"; done 

It will act only on PDF files and rename to the last 9 characters. Note however, no error handling other then not overwriting an existing file.


Note: The example bash script code is just that and does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted.

0

If the files are in the same folder, go to Finder and select all the files you want to rename. Then right-click on them and one of the options is to rename xxx items where xxx is the number of files selected. When you select this option it opens a panel where you can enter how you want the files renamed. Then you can select rename and all the files will be renamed.

Or from the command line use ls *.ext where ext is the extension of your file names, in your case pdf. If the ls command listed just the files you want to change the name of use cp *.ext newFileName.ext to copy the files to a new file name or use mv *.pdf NewFileName.pdfto move the files.

5
  • What the OP wants is not doable per your answer! Aug 11, 2020 at 19:42
  • why not? He want to rename a list of files.
    – Natsfan
    Aug 11, 2020 at 19:43
  • What the op said is doable by my method. if he truly wants all file names to be 0000000.pdf, he can't do that any way except by putting one file one folder. I don't think he wants that.
    – Natsfan
    Aug 11, 2020 at 19:51
  • The last 9 characters are unique per file if that needs to be clarified. Aug 11, 2020 at 19:53
  • can extra character be added before those nine like a number? like 01.000000000.pdf
    – Natsfan
    Aug 11, 2020 at 19:59
-1

Open Terminal and copy/paste the following code:

for f in *; do mv "$f" "${f:19}"; done

This will truncate the first 19 characters from the file names.

6
  • This does not work properly! Aug 11, 2020 at 21:04
  • worked for me without any problems in 10.15.4
    – agarza
    Aug 11, 2020 at 21:07
  • You cannot just chop 19 characters from the file names as "FirstName_LastName_000000000.pdf" is just an example. The length of "FirstName" and "LastName" is unknown as it's just a placeholder! Aug 11, 2020 at 21:07
  • I will admit I did overlook that issue, but the OP didn't specify that the "FirstName" and "LastName" would be different.
    – agarza
    Aug 11, 2020 at 21:10
  • It doesn't matter whether or not the first and last names are the same or different, only that he wants the last 9 numeric characters! If the actual filename was e.g. Jonah_Coloma_000000000.pdf then your command renames it to 000.pdf not 000000000.pdf! Aug 11, 2020 at 21:21

You must log in to answer this question.

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