3

I have a lot of photos with me that I want to rename. I want them to be 10 random characters. Is there a way to do it?

I am running macOS Sierra.

I didn't try anything yet.

I would prefer a free solution to this problem.

7

The following script assumes that the photos to be renamed are in your Pictures folder. This will rename every file in your Pictures folder to a 10 character random string of letters and numbers while keeping the file extensions intact.

#!/bin/bash

chars=( {a..z} {A..Z} {0..9} )

function rand_string {
    local c=$1 ret=
    while((c--)); do
        ret+=${chars[$((RANDOM%${#chars[@]}))]}
    done
    printf '%s\n' "$ret"
}

for file in ~/Pictures/*
do
    ext=$(echo ${file} | sed 's,^.*\(\.[^\.]*$\),\1,')
    mv "$file" ~/Pictures/"$(rand_string 10)"${ext}
done

If the files to be renamed are not in your Pictures folder then edit ~/Pictures accordingly. The ~ is simply a shortcut for the users home directory. So ~/Pictures = /Users/YourUsername/Pictures. Note that the * tells the script to match any file found in ~/Pictures. Also note that you can change the number of characters generated by changing the number 10 of (rand_string 10)to any number you like.

  • Copy and paste this into a plain text file and name it something like: rename_pics.sh

  • Open your Terminal (use spotlight to find it if you don't know it's location)

  • In your Terminal type chmod 755 rename_pics.sh and press Enter

  • Then type ./rename_pics.sh in your terminal and press Enter to run the script.

  • By the way, is there a way I can rename without changing the extension? I mean right now I have three different scripts to rename the jpg, png and mp4 files. First, I'll place only jpg files on the Desktop then run the jpg script on the desktop and then move the files to another folder. I'll do the same process for png and mp4 files. It would be better if the script renamed all the files without changing it's extension. – user204626 Oct 5 '16 at 7:48
  • 1
    @danielAzuelos Thanks for the sed magic. I have incorporated this into the answer. – I0_ol Oct 5 '16 at 12:17
0

Copy and paste this into a text file:

#!/bin/bash

cd "$1"
for i in ./*; do
    mv "$i" $((RANDOM * 32768 + RANDOM))
done

Run the following command to make the file executable (assuming you've saved the file as randomlyrename):

chmod 755 randomlyrename

And run the file:

./randomlyrename /path/to/folder/containing/things-to-rename

Bear in mind that it'll rename everything in the directory it's provided to a string of random numbers ten digits long.

  • 1
    @perhapsmaybeharry your script removed all the file extensions. Anyway, I've already found my answer from another user and I've accepted it. Thank you so much for taking the time out to help me :) I really appreciate it :) – user204626 Oct 5 '16 at 7:34

You must log in to answer this question.