Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

bingefeller

macrumors 6502a
Original poster
Jun 25, 2007
596
33
Northern Ireland
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!
 

0d085d

macrumors regular
Apr 23, 2015
154
12
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
 
  • Like
Reactions: bingefeller
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.