Linux: how to replace file names from a file containing original patterns and new patterns?












0















if you have 4 files named SampleA.txt, SampleB.txt Samble25.txt and SampleA21.txt.
And that you have a tab-delimited txt file where one column has the original pattern (SampleA, SampleB, Sample25, SampleA21) and another column with corresponding new pattern (Community1, Community2, Community3, Community4),



is there a way to change the files title from the original pattern (first column) to the new patter (second column)?










share|improve this question























  • did you see: askubuntu.com/q/283145? might be useful if you've only got a few patterns…

    – Sam Mason
    Nov 28 '18 at 21:42











  • Hi Sam! Thanks yes i saw it. Unfortunately I would like something that can convert it no matter how many there are objects and how different are the Objects, as long as in one file is listed the original pattern of the file name and its New pattern. But thanks!

    – Vincent La Foote Carrier
    Nov 29 '18 at 8:31
















0















if you have 4 files named SampleA.txt, SampleB.txt Samble25.txt and SampleA21.txt.
And that you have a tab-delimited txt file where one column has the original pattern (SampleA, SampleB, Sample25, SampleA21) and another column with corresponding new pattern (Community1, Community2, Community3, Community4),



is there a way to change the files title from the original pattern (first column) to the new patter (second column)?










share|improve this question























  • did you see: askubuntu.com/q/283145? might be useful if you've only got a few patterns…

    – Sam Mason
    Nov 28 '18 at 21:42











  • Hi Sam! Thanks yes i saw it. Unfortunately I would like something that can convert it no matter how many there are objects and how different are the Objects, as long as in one file is listed the original pattern of the file name and its New pattern. But thanks!

    – Vincent La Foote Carrier
    Nov 29 '18 at 8:31














0












0








0








if you have 4 files named SampleA.txt, SampleB.txt Samble25.txt and SampleA21.txt.
And that you have a tab-delimited txt file where one column has the original pattern (SampleA, SampleB, Sample25, SampleA21) and another column with corresponding new pattern (Community1, Community2, Community3, Community4),



is there a way to change the files title from the original pattern (first column) to the new patter (second column)?










share|improve this question














if you have 4 files named SampleA.txt, SampleB.txt Samble25.txt and SampleA21.txt.
And that you have a tab-delimited txt file where one column has the original pattern (SampleA, SampleB, Sample25, SampleA21) and another column with corresponding new pattern (Community1, Community2, Community3, Community4),



is there a way to change the files title from the original pattern (first column) to the new patter (second column)?







linux filenames






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 28 '18 at 10:23









Vincent La Foote CarrierVincent La Foote Carrier

64110




64110













  • did you see: askubuntu.com/q/283145? might be useful if you've only got a few patterns…

    – Sam Mason
    Nov 28 '18 at 21:42











  • Hi Sam! Thanks yes i saw it. Unfortunately I would like something that can convert it no matter how many there are objects and how different are the Objects, as long as in one file is listed the original pattern of the file name and its New pattern. But thanks!

    – Vincent La Foote Carrier
    Nov 29 '18 at 8:31



















  • did you see: askubuntu.com/q/283145? might be useful if you've only got a few patterns…

    – Sam Mason
    Nov 28 '18 at 21:42











  • Hi Sam! Thanks yes i saw it. Unfortunately I would like something that can convert it no matter how many there are objects and how different are the Objects, as long as in one file is listed the original pattern of the file name and its New pattern. But thanks!

    – Vincent La Foote Carrier
    Nov 29 '18 at 8:31

















did you see: askubuntu.com/q/283145? might be useful if you've only got a few patterns…

– Sam Mason
Nov 28 '18 at 21:42





did you see: askubuntu.com/q/283145? might be useful if you've only got a few patterns…

– Sam Mason
Nov 28 '18 at 21:42













Hi Sam! Thanks yes i saw it. Unfortunately I would like something that can convert it no matter how many there are objects and how different are the Objects, as long as in one file is listed the original pattern of the file name and its New pattern. But thanks!

– Vincent La Foote Carrier
Nov 29 '18 at 8:31





Hi Sam! Thanks yes i saw it. Unfortunately I would like something that can convert it no matter how many there are objects and how different are the Objects, as long as in one file is listed the original pattern of the file name and its New pattern. But thanks!

– Vincent La Foote Carrier
Nov 29 '18 at 8:31












1 Answer
1






active

oldest

votes


















0














just had a quick hack in Python, maybe something like this would be useful:



#!/bin/env python3

from sys import argv
from pathlib import Path
import csv

with open('rename-pats.txt') as fd:
inp = csv.reader(fd, delimiter='t')
patterns =
for src, dst in inp:
patterns.append((src, dst))

for path in argv[1:]:
path = Path(path)
name = path.name
for src, dst in patterns:
name = name.replace(src, dst)
if path.name != name:
path.rename(path.with_name(name))


relies on a file called rename-pats.txt containing something like:



SampleA Community1
SampleB Community2
Sample25 Community3
SampleA21 Community4


which would then be run as:



python3 mmv.py *.txt





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    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',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    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
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53517179%2flinux-how-to-replace-file-names-from-a-file-containing-original-patterns-and-ne%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    just had a quick hack in Python, maybe something like this would be useful:



    #!/bin/env python3

    from sys import argv
    from pathlib import Path
    import csv

    with open('rename-pats.txt') as fd:
    inp = csv.reader(fd, delimiter='t')
    patterns =
    for src, dst in inp:
    patterns.append((src, dst))

    for path in argv[1:]:
    path = Path(path)
    name = path.name
    for src, dst in patterns:
    name = name.replace(src, dst)
    if path.name != name:
    path.rename(path.with_name(name))


    relies on a file called rename-pats.txt containing something like:



    SampleA Community1
    SampleB Community2
    Sample25 Community3
    SampleA21 Community4


    which would then be run as:



    python3 mmv.py *.txt





    share|improve this answer






























      0














      just had a quick hack in Python, maybe something like this would be useful:



      #!/bin/env python3

      from sys import argv
      from pathlib import Path
      import csv

      with open('rename-pats.txt') as fd:
      inp = csv.reader(fd, delimiter='t')
      patterns =
      for src, dst in inp:
      patterns.append((src, dst))

      for path in argv[1:]:
      path = Path(path)
      name = path.name
      for src, dst in patterns:
      name = name.replace(src, dst)
      if path.name != name:
      path.rename(path.with_name(name))


      relies on a file called rename-pats.txt containing something like:



      SampleA Community1
      SampleB Community2
      Sample25 Community3
      SampleA21 Community4


      which would then be run as:



      python3 mmv.py *.txt





      share|improve this answer




























        0












        0








        0







        just had a quick hack in Python, maybe something like this would be useful:



        #!/bin/env python3

        from sys import argv
        from pathlib import Path
        import csv

        with open('rename-pats.txt') as fd:
        inp = csv.reader(fd, delimiter='t')
        patterns =
        for src, dst in inp:
        patterns.append((src, dst))

        for path in argv[1:]:
        path = Path(path)
        name = path.name
        for src, dst in patterns:
        name = name.replace(src, dst)
        if path.name != name:
        path.rename(path.with_name(name))


        relies on a file called rename-pats.txt containing something like:



        SampleA Community1
        SampleB Community2
        Sample25 Community3
        SampleA21 Community4


        which would then be run as:



        python3 mmv.py *.txt





        share|improve this answer















        just had a quick hack in Python, maybe something like this would be useful:



        #!/bin/env python3

        from sys import argv
        from pathlib import Path
        import csv

        with open('rename-pats.txt') as fd:
        inp = csv.reader(fd, delimiter='t')
        patterns =
        for src, dst in inp:
        patterns.append((src, dst))

        for path in argv[1:]:
        path = Path(path)
        name = path.name
        for src, dst in patterns:
        name = name.replace(src, dst)
        if path.name != name:
        path.rename(path.with_name(name))


        relies on a file called rename-pats.txt containing something like:



        SampleA Community1
        SampleB Community2
        Sample25 Community3
        SampleA21 Community4


        which would then be run as:



        python3 mmv.py *.txt






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 29 '18 at 15:03

























        answered Nov 29 '18 at 14:57









        Sam MasonSam Mason

        3,34811331




        3,34811331
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53517179%2flinux-how-to-replace-file-names-from-a-file-containing-original-patterns-and-ne%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Lallio

            Futebolista

            Jornalista