Bash script to move files into folder based on matching ID numbers?

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











up vote
0
down vote

favorite












On ~/Desktop/a/ , I have files and folders with this pattern:



 500 photodir
Photo 500.jpg
1000 photodir
Photo 1000.jpg


I would like to have the .jpgs moved into their folders:



 500 photodir/Photo 500.jpg
1000 photodir/Photo 1000.jpg


This is the code so far:



!#/bin/bash/
for f in ~/Desktop/a/*.jpg
do
base=“$f%Photo*”
mv "$f" "$base/"
mv "$sub/$f"* "$base/$sub/"
done









share|improve this question









New contributor




user10630009 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • Ain't this a duplicate of unix.stackexchange.com/questions/480803/… ?
    – sla3k
    2 hours ago










  • @sla3k , not at all.
    – user10630009
    1 hour ago










  • @sla3k , Differences are that I do not want to make folders in this case - the folders pre-exist, and instead of a whole chunk of the name being similar, I just want to use the numbers for processing
    – user10630009
    1 hour ago















up vote
0
down vote

favorite












On ~/Desktop/a/ , I have files and folders with this pattern:



 500 photodir
Photo 500.jpg
1000 photodir
Photo 1000.jpg


I would like to have the .jpgs moved into their folders:



 500 photodir/Photo 500.jpg
1000 photodir/Photo 1000.jpg


This is the code so far:



!#/bin/bash/
for f in ~/Desktop/a/*.jpg
do
base=“$f%Photo*”
mv "$f" "$base/"
mv "$sub/$f"* "$base/$sub/"
done









share|improve this question









New contributor




user10630009 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • Ain't this a duplicate of unix.stackexchange.com/questions/480803/… ?
    – sla3k
    2 hours ago










  • @sla3k , not at all.
    – user10630009
    1 hour ago










  • @sla3k , Differences are that I do not want to make folders in this case - the folders pre-exist, and instead of a whole chunk of the name being similar, I just want to use the numbers for processing
    – user10630009
    1 hour ago













up vote
0
down vote

favorite









up vote
0
down vote

favorite











On ~/Desktop/a/ , I have files and folders with this pattern:



 500 photodir
Photo 500.jpg
1000 photodir
Photo 1000.jpg


I would like to have the .jpgs moved into their folders:



 500 photodir/Photo 500.jpg
1000 photodir/Photo 1000.jpg


This is the code so far:



!#/bin/bash/
for f in ~/Desktop/a/*.jpg
do
base=“$f%Photo*”
mv "$f" "$base/"
mv "$sub/$f"* "$base/$sub/"
done









share|improve this question









New contributor




user10630009 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











On ~/Desktop/a/ , I have files and folders with this pattern:



 500 photodir
Photo 500.jpg
1000 photodir
Photo 1000.jpg


I would like to have the .jpgs moved into their folders:



 500 photodir/Photo 500.jpg
1000 photodir/Photo 1000.jpg


This is the code so far:



!#/bin/bash/
for f in ~/Desktop/a/*.jpg
do
base=“$f%Photo*”
mv "$f" "$base/"
mv "$sub/$f"* "$base/$sub/"
done






bash






share|improve this question









New contributor




user10630009 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




user10630009 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 59 mins ago









Sparhawk

8,72663789




8,72663789






New contributor




user10630009 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 hours ago









user10630009

32




32




New contributor




user10630009 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





user10630009 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






user10630009 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











  • Ain't this a duplicate of unix.stackexchange.com/questions/480803/… ?
    – sla3k
    2 hours ago










  • @sla3k , not at all.
    – user10630009
    1 hour ago










  • @sla3k , Differences are that I do not want to make folders in this case - the folders pre-exist, and instead of a whole chunk of the name being similar, I just want to use the numbers for processing
    – user10630009
    1 hour ago

















  • Ain't this a duplicate of unix.stackexchange.com/questions/480803/… ?
    – sla3k
    2 hours ago










  • @sla3k , not at all.
    – user10630009
    1 hour ago










  • @sla3k , Differences are that I do not want to make folders in this case - the folders pre-exist, and instead of a whole chunk of the name being similar, I just want to use the numbers for processing
    – user10630009
    1 hour ago
















Ain't this a duplicate of unix.stackexchange.com/questions/480803/… ?
– sla3k
2 hours ago




Ain't this a duplicate of unix.stackexchange.com/questions/480803/… ?
– sla3k
2 hours ago












@sla3k , not at all.
– user10630009
1 hour ago




@sla3k , not at all.
– user10630009
1 hour ago












@sla3k , Differences are that I do not want to make folders in this case - the folders pre-exist, and instead of a whole chunk of the name being similar, I just want to use the numbers for processing
– user10630009
1 hour ago





@sla3k , Differences are that I do not want to make folders in this case - the folders pre-exist, and instead of a whole chunk of the name being similar, I just want to use the numbers for processing
– user10630009
1 hour ago











1 Answer
1






active

oldest

votes

















up vote
0
down vote













#!/bin/bash
cd ~/Desktop/a/
for f in *.jpg; do
target_part="$f%.jpg"
target="$target_part#Photo "
mv "$f" "$target photodir"
done


Explanation



There are two parameter substitutions occurring here. Firstly, target_part="$f%.jpg" removes the trailing .jpg from the filename, then target="$target_part#Photo " removes the Photo (+ space) at the front.



Then, you merely move the file to the number plus photodir, i.e. mv "$f" "$target photodir".



Further information



There were a few things wrong with your script. Firstly, the shebang was wrong. Instead of !#, it should be #!.



Secondly, I'm not sure if just a typo, but the double quote should be with ", not .



I'm also not sure why the * in your parameter substitution of $f%Photo*, nor what the variable $sub referred to (it was never assigned).



Finally, instead of using ~/Desktop/a/*.jpg for the loop, I preferred to cd directly into it, to allow easier parameter substitution.






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: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );






    user10630009 is a new contributor. Be nice, and check out our Code of Conduct.









     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f480899%2fbash-script-to-move-files-into-folder-based-on-matching-id-numbers%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













    #!/bin/bash
    cd ~/Desktop/a/
    for f in *.jpg; do
    target_part="$f%.jpg"
    target="$target_part#Photo "
    mv "$f" "$target photodir"
    done


    Explanation



    There are two parameter substitutions occurring here. Firstly, target_part="$f%.jpg" removes the trailing .jpg from the filename, then target="$target_part#Photo " removes the Photo (+ space) at the front.



    Then, you merely move the file to the number plus photodir, i.e. mv "$f" "$target photodir".



    Further information



    There were a few things wrong with your script. Firstly, the shebang was wrong. Instead of !#, it should be #!.



    Secondly, I'm not sure if just a typo, but the double quote should be with ", not .



    I'm also not sure why the * in your parameter substitution of $f%Photo*, nor what the variable $sub referred to (it was never assigned).



    Finally, instead of using ~/Desktop/a/*.jpg for the loop, I preferred to cd directly into it, to allow easier parameter substitution.






    share|improve this answer


























      up vote
      0
      down vote













      #!/bin/bash
      cd ~/Desktop/a/
      for f in *.jpg; do
      target_part="$f%.jpg"
      target="$target_part#Photo "
      mv "$f" "$target photodir"
      done


      Explanation



      There are two parameter substitutions occurring here. Firstly, target_part="$f%.jpg" removes the trailing .jpg from the filename, then target="$target_part#Photo " removes the Photo (+ space) at the front.



      Then, you merely move the file to the number plus photodir, i.e. mv "$f" "$target photodir".



      Further information



      There were a few things wrong with your script. Firstly, the shebang was wrong. Instead of !#, it should be #!.



      Secondly, I'm not sure if just a typo, but the double quote should be with ", not .



      I'm also not sure why the * in your parameter substitution of $f%Photo*, nor what the variable $sub referred to (it was never assigned).



      Finally, instead of using ~/Desktop/a/*.jpg for the loop, I preferred to cd directly into it, to allow easier parameter substitution.






      share|improve this answer
























        up vote
        0
        down vote










        up vote
        0
        down vote









        #!/bin/bash
        cd ~/Desktop/a/
        for f in *.jpg; do
        target_part="$f%.jpg"
        target="$target_part#Photo "
        mv "$f" "$target photodir"
        done


        Explanation



        There are two parameter substitutions occurring here. Firstly, target_part="$f%.jpg" removes the trailing .jpg from the filename, then target="$target_part#Photo " removes the Photo (+ space) at the front.



        Then, you merely move the file to the number plus photodir, i.e. mv "$f" "$target photodir".



        Further information



        There were a few things wrong with your script. Firstly, the shebang was wrong. Instead of !#, it should be #!.



        Secondly, I'm not sure if just a typo, but the double quote should be with ", not .



        I'm also not sure why the * in your parameter substitution of $f%Photo*, nor what the variable $sub referred to (it was never assigned).



        Finally, instead of using ~/Desktop/a/*.jpg for the loop, I preferred to cd directly into it, to allow easier parameter substitution.






        share|improve this answer














        #!/bin/bash
        cd ~/Desktop/a/
        for f in *.jpg; do
        target_part="$f%.jpg"
        target="$target_part#Photo "
        mv "$f" "$target photodir"
        done


        Explanation



        There are two parameter substitutions occurring here. Firstly, target_part="$f%.jpg" removes the trailing .jpg from the filename, then target="$target_part#Photo " removes the Photo (+ space) at the front.



        Then, you merely move the file to the number plus photodir, i.e. mv "$f" "$target photodir".



        Further information



        There were a few things wrong with your script. Firstly, the shebang was wrong. Instead of !#, it should be #!.



        Secondly, I'm not sure if just a typo, but the double quote should be with ", not .



        I'm also not sure why the * in your parameter substitution of $f%Photo*, nor what the variable $sub referred to (it was never assigned).



        Finally, instead of using ~/Desktop/a/*.jpg for the loop, I preferred to cd directly into it, to allow easier parameter substitution.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 43 mins ago

























        answered 50 mins ago









        Sparhawk

        8,72663789




        8,72663789




















            user10630009 is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            user10630009 is a new contributor. Be nice, and check out our Code of Conduct.












            user10630009 is a new contributor. Be nice, and check out our Code of Conduct.











            user10630009 is a new contributor. Be nice, and check out our Code of Conduct.













             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f480899%2fbash-script-to-move-files-into-folder-based-on-matching-id-numbers%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?