This script copies all the files to all the folders. But I am struggling to construct a condition statement that will specify to which folder each file is copied to. I've tried the if statement but no copy is made at all.
The reason I use parameter expansion is because the file names are like, Long_John_Silver, Miss_Havisham and Master_Pip and the folder names are like Long, Miss and Master. So essentially I'm trying to copy the files to their respective folders e.g. Master_Pip.fna.gz into the folder named Master. And so what I've tried to do is to capture the first word of the file name and somehow use that as a reference.
for fldr in /home/scripts/playground/genomes_2/* ; do
find . -name *fna.gz | while read f ; do
basenm=${fldr##*/} ; f_name=${basenm%%_*} ;
cp -r $f /home/scripts/playground/port/$f_name/ ;
done
done
This is my script with the if statement, but this script copies nothing at all.
for fldr in /home/scripts/playground/genomes_2/* ; do
find . -name *fna.gz | while read f ; do
basenm=${fldr##*/} ; f_name=${basenm%%_*} ;
if [ "$f_name" == /home/scripts/playground/port/"$f_name" ] ; then
cp -r $f /home/scripts/playground/port/"$f_name"/ ;
else
continue
fi
done
done
"$f_name"could possibly equal to itself + another string ??? You might wanna test if the file already exists it the specific folder, fi so, useif -f $myPathAndFile"f_name"is reference point. It extracts from the file name like so - "James_Bond" into "James". I am doing this because all my files have multiple underscores but the beginning of the file is the name of a folder in a distant location i.e there is a folder called "James" somewhere. And there are 100s of these which is why I'm trying to automate this.if [ "$f_name" == /home/scripts/playground/port/"$f_name" ]... i don't get how this can possibly work. It's like sayingx = 10 + x