0

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
4
  • how can "$f_name" could possibly equal to itself + another string ??? You might wanna test if the file already exists it the specific folder, fi so, use if -f $myPathAndFile
    – Carpette
    Mar 6, 2018 at 12:53
  • "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. Mar 6, 2018 at 13:32
  • I understand this but ... if [ "$f_name" == /home/scripts/playground/port/"$f_name" ] ... i don't get how this can possibly work. It's like saying x = 10 + x
    – Carpette
    Mar 6, 2018 at 13:42
  • Yeah I agree it was a crucial mistake. Mar 6, 2018 at 15:41

2 Answers 2

1

1. Change this code

basenm=${fldr##*/};

to this one

basenm=${f##*/} ;

and remove -r switch from copy command

cp $f /home/scripts/playground/port/"$f_name"

Maybe you have some jumbled source paths like this /home/scripts/playground/genomes_2/Long_John_Silver/Master_Pip.fna.gz. So, when you're extracting f_name from folder name fldr you get 'Long'. Then you copy Master_Pip.fna.gz to the ..../Long folder. 2.

"$f_name" == /home/scripts/playground/port/"$f_name"

they are not equal, and they won't.

Basically, your first script is good. Condition is redundant because you do it implicitly by parameter expansion.

I would only add directory creation command before copying. So you'll always have a directory to copy to in case new person appears in source directory, Shorty_Pete for example.

mkdir -p /home/scripts/playground/port/"$f_name"
cp ...
3
  • I have already created folders which don't exist in a previous step of the same script, I've only posted this part of the script. Even still, if I remove the if statement, which I have tested, it copies all files into all folders and so the implicit specificity by parameter expansion actually doesn't seem to work. Mar 6, 2018 at 13:22
  • OK. What are the names of subdirectories in the /home/scripts/playground/genomes_2 ? The only discrepancy I see that you extract f_name not from filename $f but from $fldr name. Mar 6, 2018 at 14:05
  • Yeah that's a significant point actually, that I took into account in my solution. Mar 6, 2018 at 15:42
0

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .