I am trying to write a script that cycles through a folder containing many folders and when inside each one it's supposed to copy all the .fna.gz files to a folder elsewhere if the file and the respective folder have the same name.
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
This script so far doesn't actually copy anything over at all.
is supposed to be the name, so it's used as a reference point because these files are something like "Long_John_Silver" but the folders into which they're supposed to be placed are like "Long".
Also note, that this if statement will never be true:
if [ "$f_name" == /home/playground/port/"$f_name" ]; then
Perhaps you meant:
if [ "$f" == /home/playground/port/"$f_name" ]; then
Remember I showed you this construct: basenm=${fldr##*/} followed by f_name=${basenm%%_*} in this thread for extracting the filename proceeding _ from a full path.
This trick should server you well in this script to replace the echo + double cut command substitution.
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 is 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