Copying files from one directory to another, renaming duplicates.

Below is the script i have but i would like simplified but still do the same job.

I need a script to copy files not directories or sub-directories into a existing or new directory. The files, if have the same name but different extension; for example 01.doc 01.pdf then only copy the .doc file.

If the file names from different directories are the same the file needs to be copied and renamed; for example test/01.doc, pic/01.doc --> pic/01_1.doc

This is what i have...

#! /bin/bash
# echo "this file is for copying directory files"
INP=$1
OUT=$2
# creates the variables

if [ $# -ne 2 ]; then
echo "usage is param1 = input directory name"
echo "usage is param2 = output directory name"
echo "exiting"
exit
fi

if [ ! -d $1 ]; then
echo "$1 is not a directory please reinput"
echo "exiting"
exit
fi

if [ -d "$OUT" ]; then
echo "output directory exists"
else
echo "about to create output directory"
mkdir $OUT
fi
# checks for a valid Input directory, if this does not exist it will exit the script
# checks for a valid output directory if this does not exist it will create a new directory

IFILE="GETFILES";
FINDFILE="FINDFILE";
WORKDIR="WORKDIR";
mkdir $WORKDIR;

#get all the input files into one file
#this file can then be read and each file copied to an output dir
find $INP -name "*.doc" > $FINDFILE
find $INP -name "*.pdf" >> $FINDFILE
find $INP -name "*.PDF" >> $FINDFILE
while read -r line;
do
cp $line $WORKDIR
done < "$FINDFILE"
##searches the input directory and searches the heirarchal directory lookind for.doc files a inserts them into workfile and then repeats for the .pdf .PDF ####

INP=$WORKDIR;
echo " the inout dir should be working as  $INP"

###this changes the name of original input directory to workdir ####


IFILE="$OUT/GETFILES";
ls -R $inp | egrep -i ".doc|.pdf" > $IFILE;
# count how many files are already in the input directory
# now read this output file line at a time
while read -r line;
do
### searches for .doc files and .pdf files and counts the amount in the input file.###

echo " the line that has just been read is ________ $line"
LCOUNT=0
DIFFCOUNT=0
FOUND=0
ARCHIVE=1
BASE="${line%.*}"
EXTENSION="${line##*.}"
echo "base name is $BASE"
echo "extension is $EXTENSION"
COUNT=$ (ls $OUT | grep ${line%%.*} | wc -l);
echo "count number of files in output dir is $Count"
echo "file is $line"
if [ $COUNT -eq 0 ];
then
cp $INP/$line  $OUT;
echo "count means no file in output directory"
echo " so can just copy the file"
### copies and searches the files from the input directory to the output directory if the file has same name it then adds an _1###
###
else
echo "there is already a file in the output so need to compare"
	COMP=$OUT/$line"
	while [ "$FOUND" -eq 0 ] && [ $LCOUNT - lt $COUNT ]; do

	echo "diffcount is $DIFFCOUNT"
### compares the file from the input directory to the file in then output directory ###	
	if [ "$DIFFCOUNT" -eq 0 ];
	then
	echo "file has already been archived no action required"
	FOUND=$[ $FOUND+1 ]
	else
	LCOUNT=$ [ $LCOUNT+1 ]
	COMP="OUT"/"$BASE"_"$LCOUNT"."$EXTENSION"
	echo "line count for next compare is $LCOUNT"
	echo "get the next file to compare"
	echo "the comparison file is now $COMP"
		if [ $LCOUNT -ne $COUNT ]; then
		ARCHIVE=$ [ $ARCHIVE+1 ]
		else
		ARCHIVE=0
		fi
 
### if the file in the inout directory is the same as the file in the output directory it will be called _1.doc, it will repeat this until it has checked the entire directory
		
	fi
	
		if [ $ARCHIVE -eq 0 ];
		then
		echo "now needs to archive with the latest generation number"
		echo "as file does not exist but there are other generations"
		echo "new generation number is $LCOUNT"
		NEWOUT="OUT"/"$BASE"_"$LCOUNT"."$EXTENSION";
		echo "newfile name is $NEWOUT"
		cp $INP/$LINE $NEWOUT
		fi
		done
fi
done < "$IFILE"
rm $IFILE

#now run search for duplicate .pdf files 
OFILE=$OUT/DOCFILES";
ls $OUT | grep ".doc" > $OFILE;
### put all the .doc files into a file to be read" ###
### now read this output file line at a time ###
while read -r line;
do
echo " the line that has just been read is ______   $line"
### this will then compare all the .doc files with .pdf files. ###

BASE="${line%.*}"
EXTENSION=${line##*.}"
NEWEXTENSION=".pdf"
SEARCHFILE=$BASE$NEWEXTENSION"
echo "base name is $BASE"
echo "extension is $EXTENSION"
echo "searchfile is $SEARCHFILE"
echo "if found will delete $SEARCHFILE so no duplicates as there is a .doc file"
find $OUT -name "$SEARCHFILE" -exec /bin/rm -f {} ';'
done < "OFILE"
rm $OFILE
### this will then remove any duplicate files so only individual .doc .pdf files will exist ###

For some the --backup parameter to cp might be a solution.