I am trying to copy specific files to specific folders, but 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.txt 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, however, it is at this point that I falter.
for fldr in /home/playground/genomes* ; do
find . -name *fna.gz | while read f ; do
f_name=$( echo $fldr | cut -d '/' -f 7 | cut -d '_' -f 1) ;
#echo $f_name ;
if [ "$f_name" == /home/playground/port/"$f_name" ]; then
cp -r "$f" /home/playground/port/"$f_name"/ ;
else
continue
fi
done
done
edit-----------------------------------------------
for fldr in /home/p995824/scripts/playground/genomes_2/* ; do
find . -name *fna.gz | while read f ; do
basenm=${fldr##*/} ; f_name=${basenm%%_*} ;
cp -r $f /home/p995824/scripts/playground/port/$f_name/ ;
done
done
This script copies all the files to all the folders. But I am struggling to construct a condition statement that will specific to which folder each file is copied to. I've tried the if statement as you can see, but I must be constructing it wrongly. Any help is much appreciated.
I realised that I was copying all the files into all the folders based on the type of file i.e. .fna.gz, so I specified what type of fna.gz file I want to be read and then copied. There was no need for an if statement since the specificity is implied through the parameter expansion. It now works perfectly.
for fldr in /home/scripts/playground/genomes_2/* ; do
basenm=${fldr##*/} ; f_name=${basenm%%_*} ;
find . -name $f_name*fna.gz | while read f ; do
cp -r $f /home/scripts/playground/port/$f_name/ ;
done
done