Batch rename files randomly

Discussion in 'Mac Basics and Help' started by bingefeller, Aug 4, 2015.

  1. bingefeller macrumors 6502

    Joined:
    Jun 25, 2007
    Location:
    Northern Ireland
    #1
    Hi guys,

    I have a folder with a lot of pdf files and they are all named differently. I would like to rename them all with random numbers but have the same amount of numbers in the file names. For example, I would like each file name to have 5 random numbers in it. Is there anyway I can do this via the command line or with Automator?

    thank you!
     
  2. 0d085d macrumors regular

    Joined:
    Apr 23, 2015
    #2
    This bash script should do what you want:

    Usage: ./script.sh files
    where files is the folder containing your PDF files

    Code:
    LENGTH=5
    EXT=pdf
    
    function makename {
    NEWNAME=
    for i in $(seq 0 $LENGTH); do
      NEWNAME=$NEWNAME"$(($RANDOM % 10))"
    done
    }
    
    for FILE in `ls $1/*.$EXT`; do
    makename
    while [ -f $1/$NEWNAME.$EXT ]; do
      makename
    done
    mv $FILE $1/$NEWNAME.$EXT
    done
    
     
  3. bingefeller thread starter macrumors 6502

    Joined:
    Jun 25, 2007
    Location:
    Northern Ireland

Share This Page