I'm trying to create a bash script that will move all files recursively from a source folder to a target folder, and simply rename files if they already exist. Similar to the way M$ Windows does, when a file exists it auto-renames it with "<filemame> (X).<ext>", etc. except for ALL files.
I've create the below, which works fine for almost all scenarios except when a folder has a (.) period in its name and a file within that folder has no extension (no period in its name).
eg a folder-path-file such as: "./oldfolder/this.folder/filenamewithoutextension"
I get (incorrectly):
"./newfolder/this (1).folder/filenamewithoutextension"
if "./newfolder/this.folder/filenamewithoutextension" already exist in the target location (./newfolder),
instead of correctly naming the new file: "./oldfolder/this.folder/filenamewithoutextension (1)"
#!/bin/bash
source=$1 ; target=$2 ;
if [ "$source" != "" ] && [ "$target" != "" ] ; then
        #recursive file search
        find "$source" -type f -exec bash -c '
                #setup variables
                oldfile="$1" ; osource='"${source}"' ; otarget='"${target}"' ;
                #set new target filename with target path
                newfile="${oldfile/${osource}/${otarget}}" ;
                #check if file already exists at target
                [ -f "${newfile}" ] && {
                        #get the filename and fileextension for numbering - ISSUE HERE?
                        filename="${newfile%/}" ; newfileext="${newfile##*.}" ;
                        #compare filename and file extension for missing extension
                        if [ "$filename" == "$newfileext" ] ; then 
                               #filename has no ext - perhaps fix the folder with a period issue here?
                               newfileext="" ; 
                        else 
                               newfileext=".$newfileext" ; 
                        fi
                        #existing files counter
                        cnt=1 ; while [ -f "${newfile%.*} (${cnt})${newfileext}" ] ; do ((cnt+=1)); done
                        #set new filename with counter - New Name created here *** Needs re-work, as folder with a period = fail
                        newfile="${newfile%.*} (${cnt})${newfileext}";
                }
                #show mv command
                echo "mv \"$oldfile\" \"${newfile}\""
        ' _ {} \;
else
        echo "Requires source and target folders";
fi
I suspect the issue is, how to properly identify the filename and extension, found in this line:
filename="${newfile%/}" ; newfileext="${newfile##*.}" which doesn't identify a filename properly (files are always after the last /).
Any suggestion on how to make it work properly?
UPDATED: Just some completion notes - Issues fixes with:
- Initially Splitting each full path filename: path - filename - (optional ext)
- Reconstructing the full path filename: path - filename - counter - (optional ext)
- fixed the file move to ensure directory structure exists with mkdir -p (mv does not create new folders if they do not exist in the target location).
 
    