BashScript move files based on matching part of filenames from a list

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











up vote
0
down vote

favorite












I have millions of xml files. The name of the xml file follows this pattern:



ABC_20180912_12345.xml
ABC_20180412_98765.xml
ABC_20180412_45678.xml


From this I want to copy files to a different folder based on the name it has after the underscore. To identify the files, I have a list which I have saved in a csv file which provides me with the required names. An example:



vcfile="/home/mycomp/Documents/wd/vehicles.csv"
vcpvr=`cat $vcfile`


echo $vcpvr provides me with this list:



2894 4249 5464


I am able to loop through the xmlfiles in the folder, open each file and grep to see if the file contains the string and if it is, the move the files to a new location. This is working.



The complete code:



#filesToExtract is the interim folder
fold="/home/mycomp/filesToExtract";
query=$fold/*.xml

vcfile="/home/mycomp/Documents/wd/vehicles.csv"
vcpvr=`cat $vcfile`

#xmlfiles - keep all tar.gz files here
cd ~/xmlfiles/
COUNTER=1
for f in *.tar.gz
do
echo " $COUNTER "
tar zxf "$f" -C ~/filesToExtract
for k in $query
do
file $k | if grep -q "$vcpvr"
then
mv $k ~/xmlToWork/
fi
done
#xmltowork is the final folder
#rm -r ~/filesToExtract/*.xml
COUNTER=$((COUNTER + 1))
done


But since this looks for the string inside the file, instead of filename, it takes longer to process millions of files. Instead, I want to look for the string in the filename and if it is there, move the files. This is what I have tried:



target="/home/mycomp/xmlToWork"

for k in $query
do
if [[ $k =~ "$vcpvr" ]]; then
cp -v $k $target
fi
done


But this gives me an error tarextract.sh: 12: tarextract.sh: [[: not found










share|improve this question

























    up vote
    0
    down vote

    favorite












    I have millions of xml files. The name of the xml file follows this pattern:



    ABC_20180912_12345.xml
    ABC_20180412_98765.xml
    ABC_20180412_45678.xml


    From this I want to copy files to a different folder based on the name it has after the underscore. To identify the files, I have a list which I have saved in a csv file which provides me with the required names. An example:



    vcfile="/home/mycomp/Documents/wd/vehicles.csv"
    vcpvr=`cat $vcfile`


    echo $vcpvr provides me with this list:



    2894 4249 5464


    I am able to loop through the xmlfiles in the folder, open each file and grep to see if the file contains the string and if it is, the move the files to a new location. This is working.



    The complete code:



    #filesToExtract is the interim folder
    fold="/home/mycomp/filesToExtract";
    query=$fold/*.xml

    vcfile="/home/mycomp/Documents/wd/vehicles.csv"
    vcpvr=`cat $vcfile`

    #xmlfiles - keep all tar.gz files here
    cd ~/xmlfiles/
    COUNTER=1
    for f in *.tar.gz
    do
    echo " $COUNTER "
    tar zxf "$f" -C ~/filesToExtract
    for k in $query
    do
    file $k | if grep -q "$vcpvr"
    then
    mv $k ~/xmlToWork/
    fi
    done
    #xmltowork is the final folder
    #rm -r ~/filesToExtract/*.xml
    COUNTER=$((COUNTER + 1))
    done


    But since this looks for the string inside the file, instead of filename, it takes longer to process millions of files. Instead, I want to look for the string in the filename and if it is there, move the files. This is what I have tried:



    target="/home/mycomp/xmlToWork"

    for k in $query
    do
    if [[ $k =~ "$vcpvr" ]]; then
    cp -v $k $target
    fi
    done


    But this gives me an error tarextract.sh: 12: tarextract.sh: [[: not found










    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have millions of xml files. The name of the xml file follows this pattern:



      ABC_20180912_12345.xml
      ABC_20180412_98765.xml
      ABC_20180412_45678.xml


      From this I want to copy files to a different folder based on the name it has after the underscore. To identify the files, I have a list which I have saved in a csv file which provides me with the required names. An example:



      vcfile="/home/mycomp/Documents/wd/vehicles.csv"
      vcpvr=`cat $vcfile`


      echo $vcpvr provides me with this list:



      2894 4249 5464


      I am able to loop through the xmlfiles in the folder, open each file and grep to see if the file contains the string and if it is, the move the files to a new location. This is working.



      The complete code:



      #filesToExtract is the interim folder
      fold="/home/mycomp/filesToExtract";
      query=$fold/*.xml

      vcfile="/home/mycomp/Documents/wd/vehicles.csv"
      vcpvr=`cat $vcfile`

      #xmlfiles - keep all tar.gz files here
      cd ~/xmlfiles/
      COUNTER=1
      for f in *.tar.gz
      do
      echo " $COUNTER "
      tar zxf "$f" -C ~/filesToExtract
      for k in $query
      do
      file $k | if grep -q "$vcpvr"
      then
      mv $k ~/xmlToWork/
      fi
      done
      #xmltowork is the final folder
      #rm -r ~/filesToExtract/*.xml
      COUNTER=$((COUNTER + 1))
      done


      But since this looks for the string inside the file, instead of filename, it takes longer to process millions of files. Instead, I want to look for the string in the filename and if it is there, move the files. This is what I have tried:



      target="/home/mycomp/xmlToWork"

      for k in $query
      do
      if [[ $k =~ "$vcpvr" ]]; then
      cp -v $k $target
      fi
      done


      But this gives me an error tarextract.sh: 12: tarextract.sh: [[: not found










      share|improve this question













      I have millions of xml files. The name of the xml file follows this pattern:



      ABC_20180912_12345.xml
      ABC_20180412_98765.xml
      ABC_20180412_45678.xml


      From this I want to copy files to a different folder based on the name it has after the underscore. To identify the files, I have a list which I have saved in a csv file which provides me with the required names. An example:



      vcfile="/home/mycomp/Documents/wd/vehicles.csv"
      vcpvr=`cat $vcfile`


      echo $vcpvr provides me with this list:



      2894 4249 5464


      I am able to loop through the xmlfiles in the folder, open each file and grep to see if the file contains the string and if it is, the move the files to a new location. This is working.



      The complete code:



      #filesToExtract is the interim folder
      fold="/home/mycomp/filesToExtract";
      query=$fold/*.xml

      vcfile="/home/mycomp/Documents/wd/vehicles.csv"
      vcpvr=`cat $vcfile`

      #xmlfiles - keep all tar.gz files here
      cd ~/xmlfiles/
      COUNTER=1
      for f in *.tar.gz
      do
      echo " $COUNTER "
      tar zxf "$f" -C ~/filesToExtract
      for k in $query
      do
      file $k | if grep -q "$vcpvr"
      then
      mv $k ~/xmlToWork/
      fi
      done
      #xmltowork is the final folder
      #rm -r ~/filesToExtract/*.xml
      COUNTER=$((COUNTER + 1))
      done


      But since this looks for the string inside the file, instead of filename, it takes longer to process millions of files. Instead, I want to look for the string in the filename and if it is there, move the files. This is what I have tried:



      target="/home/mycomp/xmlToWork"

      for k in $query
      do
      if [[ $k =~ "$vcpvr" ]]; then
      cp -v $k $target
      fi
      done


      But this gives me an error tarextract.sh: 12: tarextract.sh: [[: not found







      bash filenames






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Aug 21 at 7:35









      Apricot

      22818




      22818




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          $cvfile is also a list, isn't it? So I would do:



          for k in "$query"
          do
          for l in "$cvfile"
          do
          if [[ "$k" =~ "$l" ]]
          then
          cp -v "$k" "$target"
          fi
          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%2f463793%2fbashscript-move-files-based-on-matching-part-of-filenames-from-a-list%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            $cvfile is also a list, isn't it? So I would do:



            for k in "$query"
            do
            for l in "$cvfile"
            do
            if [[ "$k" =~ "$l" ]]
            then
            cp -v "$k" "$target"
            fi
            done
            done





            share|improve this answer
























              up vote
              0
              down vote













              $cvfile is also a list, isn't it? So I would do:



              for k in "$query"
              do
              for l in "$cvfile"
              do
              if [[ "$k" =~ "$l" ]]
              then
              cp -v "$k" "$target"
              fi
              done
              done





              share|improve this answer






















                up vote
                0
                down vote










                up vote
                0
                down vote









                $cvfile is also a list, isn't it? So I would do:



                for k in "$query"
                do
                for l in "$cvfile"
                do
                if [[ "$k" =~ "$l" ]]
                then
                cp -v "$k" "$target"
                fi
                done
                done





                share|improve this answer












                $cvfile is also a list, isn't it? So I would do:



                for k in "$query"
                do
                for l in "$cvfile"
                do
                if [[ "$k" =~ "$l" ]]
                then
                cp -v "$k" "$target"
                fi
                done
                done






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Aug 21 at 9:29









                spacelander

                1485




                1485



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f463793%2fbashscript-move-files-based-on-matching-part-of-filenames-from-a-list%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?

                    Displaying single band from multi-band raster using QGIS

                    How many registers does an x86_64 CPU actually have?