I have been using a loop to copy data from an external group to my working directory, appending the folder name to the beginning of the file name. The for loop is based on this previous question. Append part of folder name to all .gz within
Since I asked that question the external group simplified their folder structure, but now has added " (2)" to some of the folder names (I have no ability to influence the way that the group names their files, it's a giant company). The space has broken my loop and I need help fixing it.
The file structure on the external group
Samples/SampleName1/Files/SampleID1_uniqueNumber.gz
Samples/SampleName2/Files/SampleID2_uniqueNumber.gz
Samples/SampleName3/Files/SampleID3_uniqueNumber.gz
Samples/SampleName3 (2)/Files/SampleID3_uniqueNumber.gz
What I want within my destination folder (all samples moved to single destination)
SampleName1.SampleID1_uniqueNumber.gz
SampleName2.SampleID2_uniqueNumber.gz
SampleName3.SampleID3_uniqueNumber.gz
SampleName3.SampleID3_uniqueNumber.gz
My current for loop which correctly copies everything except for the last sample. The uniqueNumber should be unique enough to prevent the 2 SampleName3 from overwriting each other.
for f in ../pathToData/Samples/*/Files/*.gz;
do s=${f##../pathToData/Samples/};
s=${s%%/*};
cp $f "/destinationFolder/"$s"."${f##*Files/};
done
How do I escape out the space so cp sees " (2)" as part of the original file name not "(2)" as the destination folder?