I have a script that creates a new directory based on User Input, and well as which directory contents it will copy over. Now here is where I am having issues, I have a find command that will replace files that have a specific name in front and replace it with the new name. It works, but I want to use the variables that I've created instead of a changing the string I have set, I keep getting an error saying it can't do it.

#Creates Directory
echo "Name of New Directory"
read userInput
if [[ -n "$newdir" ]]
  mkdir $newdir
echo $newdir Directory Created
echo "Directory you wish to Copy?"
read copydir
if [[ -n "$copydir" ]]
#Copies contents of Specified Directory
 cp -R $copydir/!(*.UNC) $newdir;

#Searches through directory  
find $newdir/ -name "$copydir*" -exec bash -c 'f="$1"; mv "$f" "${f/sppark/work}"' - {} \;


So instead of sppark and work, I want to use my variables newdir and copydir, in the Find command.

  • 1
    Changed the Title, I require assistance with my Find command, I want to use my variables $newdir and $copydir, instead of "${f/sppark/work}", where newdir is work and copydir is sppark – Josh Adams Oct 18 '18 at 13:59
  • you could use rename instead of mv.. – pLumo Oct 18 '18 at 13:59
  • That was my first guess, but rename doesn't exist in my environment, and I can't just download it, so mv is next best – Josh Adams Oct 18 '18 at 14:02
  • Why ksh and exec bash ? Perhaps you can try to export your variables before calling find. – ctac_ Oct 18 '18 at 14:36

I figured it out, I used a for loop and a mv instead to do what I want.

    #Searches through directory  
for file in $newdir/$copydir*; do
#Changes Part of File Name
mv -- "$file" "$(echo ${file}|sed 's/'$copydir'/'$newdir'/')"
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.