An attempt at copying specific files to specific folders

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












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






share|improve this question






















  • 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 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.
    – Mr Keystrokes
    Mar 6 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 at 13:42










  • Yeah I agree it was a crucial mistake.
    – Mr Keystrokes
    Mar 6 at 15:41














up vote
0
down vote

favorite












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






share|improve this question






















  • 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 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.
    – Mr Keystrokes
    Mar 6 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 at 13:42










  • Yeah I agree it was a crucial mistake.
    – Mr Keystrokes
    Mar 6 at 15:41












up vote
0
down vote

favorite









up vote
0
down vote

favorite











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






share|improve this question














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








share|improve this question













share|improve this question




share|improve this question








edited Mar 15 at 21:44









sourcejedi

18.9k32477




18.9k32477










asked Mar 6 at 12:08









Mr Keystrokes

11




11











  • 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 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.
    – Mr Keystrokes
    Mar 6 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 at 13:42










  • Yeah I agree it was a crucial mistake.
    – Mr Keystrokes
    Mar 6 at 15:41
















  • 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 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.
    – Mr Keystrokes
    Mar 6 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 at 13:42










  • Yeah I agree it was a crucial mistake.
    – Mr Keystrokes
    Mar 6 at 15:41















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 at 12:53





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 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.
– Mr Keystrokes
Mar 6 at 13:32




"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.
– Mr Keystrokes
Mar 6 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 at 13:42




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 at 13:42












Yeah I agree it was a crucial mistake.
– Mr Keystrokes
Mar 6 at 15:41




Yeah I agree it was a crucial mistake.
– Mr Keystrokes
Mar 6 at 15:41










2 Answers
2






active

oldest

votes

















up vote
1
down vote













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 ...





share|improve this answer






















  • 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.
    – Mr Keystrokes
    Mar 6 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.
    – Alexey Dolgopolov
    Mar 6 at 14:05










  • Yeah that's a significant point actually, that I took into account in my solution.
    – Mr Keystrokes
    Mar 6 at 15:42

















up vote
0
down vote



accepted










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





share|improve this answer




















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f428492%2fan-attempt-at-copying-specific-files-to-specific-folders%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    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 ...





    share|improve this answer






















    • 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.
      – Mr Keystrokes
      Mar 6 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.
      – Alexey Dolgopolov
      Mar 6 at 14:05










    • Yeah that's a significant point actually, that I took into account in my solution.
      – Mr Keystrokes
      Mar 6 at 15:42














    up vote
    1
    down vote













    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 ...





    share|improve this answer






















    • 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.
      – Mr Keystrokes
      Mar 6 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.
      – Alexey Dolgopolov
      Mar 6 at 14:05










    • Yeah that's a significant point actually, that I took into account in my solution.
      – Mr Keystrokes
      Mar 6 at 15:42












    up vote
    1
    down vote










    up vote
    1
    down vote









    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 ...





    share|improve this answer














    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 ...






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Mar 6 at 14:29

























    answered Mar 6 at 12:23









    Alexey Dolgopolov

    1113




    1113











    • 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.
      – Mr Keystrokes
      Mar 6 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.
      – Alexey Dolgopolov
      Mar 6 at 14:05










    • Yeah that's a significant point actually, that I took into account in my solution.
      – Mr Keystrokes
      Mar 6 at 15:42
















    • 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.
      – Mr Keystrokes
      Mar 6 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.
      – Alexey Dolgopolov
      Mar 6 at 14:05










    • Yeah that's a significant point actually, that I took into account in my solution.
      – Mr Keystrokes
      Mar 6 at 15:42















    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.
    – Mr Keystrokes
    Mar 6 at 13:22




    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.
    – Mr Keystrokes
    Mar 6 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.
    – Alexey Dolgopolov
    Mar 6 at 14:05




    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.
    – Alexey Dolgopolov
    Mar 6 at 14:05












    Yeah that's a significant point actually, that I took into account in my solution.
    – Mr Keystrokes
    Mar 6 at 15:42




    Yeah that's a significant point actually, that I took into account in my solution.
    – Mr Keystrokes
    Mar 6 at 15:42












    up vote
    0
    down vote



    accepted










    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





    share|improve this answer
























      up vote
      0
      down vote



      accepted










      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





      share|improve this answer






















        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        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





        share|improve this answer












        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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 6 at 15:35









        Mr Keystrokes

        11




        11






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f428492%2fan-attempt-at-copying-specific-files-to-specific-folders%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay