Search code examples
macosrenamemacos-sierrafile-renamebatch-rename

Remove A String of Numbers or Text From The Beginning of A Filename


On MacOS Sierra (MacBook Pro), how would one remove random strings of numbers from the beginning of .pdf filenames as a batch so that they can be viewed in alphabetical order? For e.g. this is a sample of how these files appear in Finder with the numbers affecting the alphabetical ordering:

22169203 The Greatest Trade Ever by Gregory Zuckerman Excerpt.pdf

22681256 Sample PDF Getting the Money.pdf

24225401 Indian Film Industry.pdf

24309156 Start and Run Your Own Coffee Shop and Lunch Bar.pdf

24375874 5 Steps To Training in Classical Music Riyaz.pdf

24538682 100 Verbal Signals.pdf

24861279 100 Greatest Songs.pdf

24975456 Appointment Book Preview.pdf

25169832 How to Start a Business for Free.pdf

25283738 Building Modern PR Campaigns.pdf

26672829 Biggest Stock Market Plays in the World.pdf

26852793 Free eBook on Secret Practical Guide for Stocks Beginners by Nnadi Jane.pdf

27012881 The Value and Price of Food An excerpt from Terra Madre by Carlo Petrini.pdf

27114721 Social Media Marketing Services.pdf

27881968 Introduction 3.pdf

28097572 Film PDF.pdf


Solution

  • This little shell script will rename the files removing the numbers at the front of the name using the regex defined at the top. Note that this script should be run in the same directory as the PDFs or you will need to change the directory in the for loop.

    #!/bin/bash
    regex="([0-9]*.)(([a-zA-Z0-9]*|.*)*\.pdf)"
    for filename in *.pdf; do
        if [[ $filename =~ $regex ]]
        then
        mv "$filename" "${BASH_REMATCH[2]}"
      fi  
    done
    

    Explanation

    A detailed breakdown of the regular expression used can be found here. It explains things more cleanly than I could in this post.

    The mv command moves the first argument (in this case the PDF with its original name) to the destination specified in the final argument (in this case the second capturing group of the regular expression). BASH_REMATCH is an array which stores the results of the regular expression operation. In this case we used the 2nd index which stores the name of the file minus the prefixed numbers. This article explains bash regular expressions in more detail.