Update: wrote an undo script that allows you undo the renaming once.
Small script I wrote in Perl for renaming files in the current directory to a random number based on the file extension, useful for randomizing images for slide show purposes.
Simply modify the @extensionArray if you want to add/remove extensions to check against.
#!/usr/local/bin/perl#-----------------------------------------------------# Script to rename files with a random number if # extension matches ## Updated: uses a different comparison: now checks # for exact match ignoring case## Updated: outputs a file for undo feature#-----------------------------------------------------## Define Variable ##@files=<*>;#Array variable to hold extensions you may check@extensionArray=("jpg","zip");#Variable for the largest value of the random #$randNumCeiling=100000;#Simple file counter to keep track of # of files processed$numFilesRenamed=0;#Open a file for writing undo infoopen(UNDOFILE,'>_undo_')ordie"$!";;#-----------------------------------------------------# Loop through all files in the directory#-----------------------------------------------------# If it is a file, get the extension# Checks if the extension is equal to wanted # extension(s)## If match, generate random number for filename # and rename it#-----------------------------------------------------foreach$file(@files){if(-f$file){# find and ignore all characters that is not a dot, then get the restmy$ext=($file=~m/([^.]+)$/)[0];if(extensionMatch($ext)){$numFilesRenamed++;print"($numFilesRenamed)\n";#Old file nameprintUNDOFILE"$file:";print"Old file name: $file\n";my$newName=int(rand($randNumCeiling));rename($file,"$newName.$ext");#New file nameprint"New file name: $newName.$ext\n\n";printUNDOFILE"$newName.$ext\n";}}}closeUNDOFILE;#-----------------------------------------------------# Loop through extention array (defined a the top)# to check for match with the argment passed into# the function (which is the extension of the file# currently being processed#-----------------------------------------------------# Returns a 1 if extensions match# else returns 0#-----------------------------------------------------sub extensionMatch{$passedInExt=@_[0];foreach$extension(@extensionArray){#check for exact string ignoring caseif($extension=~m/^$passedInExt$/i){return1;}}return0;}
Undo
1234567891011121314151617181920212223
#!/usr/local/bin/perl#-----------------------------------------------------# Script to undo the renaming of files using the # random file rename script. Simple rename back to the# original name according to the _undo_ text file#-----------------------------------------------------# Opens the file for readingopen(UNDOFILE,'_undo_')ordie"Cannot open undo file.";while(<UNDOFILE>){#Split up the names@values=split(":",$_);#Get the names$oldname=@values[0];$currentname=@values[1];chomp($currentname);#Rename back to the old name#In this case, old name is the new namerename($currentname,$oldname);}closeUNDOFILE;