Demo entry 6718096

mass rename

   

Submitted by anonymous on Mar 01, 2018 at 21:27
Language: Bash. Code size: 865 Bytes.

#!/system/bin/sh
if [ $# -le 1 ]; then
	echo "Usage: rename [match pattern] [replace pattern | rand]"
	echo
	echo "	Bulk rename files."
	echo "	If 'rand' specified as replace pattern, would treat it as a random digit sequence (eg. 2937325505)\n"

	exit
fi	 

n=0
OIFS=IFS
IFS=;
for file in *$1*; do
	if [ "$file" == "*$1*" ]; then
		echo "No files matching pattern.\n"
		exit
	fi
	
	_2=$2
	if [ $2 == "rand" ]; then
		_2=$RANDOM$RANDOM
	fi

	eval FILE_$n=\"\$file\"
	eval REPL_$n=\"${file/$1/$_2}\"

	echo "$(eval echo \$FILE_$n) --> $(eval echo \$REPL_$n)"
	n=$(( n + 1 ))
done
IFS=OIFS

echo -n "\nContinue? (y/n) "
read -r -s -n 1

if [[ $REPLY = [Yy] ]]; then
	echo
	while [ n=$(( n - 1 )) -ge 0 ]; do
		echo "renaming $(eval echo \$FILE_$n) as $(eval echo \$REPL_$n)"
		mv -i "$(eval echo \$FILE_$n)" "$(eval echo \$REPL_$n)"
	done
else
	echo "\nAbort."
fi

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).