1

I'm trying to write a script that will copy the contents of one directory, send them to ./test/ and append _copy to the file name. Below is my current script. I seem to be unable to append _copy to the file name. Any small suggestions for improvements will help, too.

#!/bin/bash

sourceDir=~sschro15/practice
targetDir=./test/
count=0

if [ -d $sourceDir ] && [ -r  $sourceDir ]
then
   echo "blah" &> /dev/null
else
   echo "Error: $sourceDir is not accessible."
   exit 1
fi

if [ -w  $sourceDir ]

then
   echo "blah" &> /dev/null
else
   echo "Error: $sourceDir is not writeable."
   exit 1
fi

for file in $sourceDir/*
do
   cp -r  $file/* ./test/ "$targetDir/*_copy"
   echo
   echo "Copied $file to $sourceDir/*_copy"
   count=$[ $count + 1 ]
done

echo
echo "$count files have been copied."
tink
  • 1,586
  • 2
  • 11
  • 15
Eikan1126
  • 11
  • 1

1 Answers1

0

Try this (untested), maybe prepend the cp with echo so it prints the commands it would execute:

Replace:

cp -r  $file/* ./test/ "$targetDir/*_copy"

with:

cp -r  "$file" "${targetDir}/${file}_copy"

For testing:

echo cp -r  "$file" "${targetDir}/${file}_copy"
tink
  • 1,586
  • 2
  • 11
  • 15