how to replace file extension












0














we want to replace the script file extension



so we did the following:



 new_name=` echo run_fix.bash  |  sed 's/[.].*$//' `
new_file_extension=".in_hold.txt"
new_name=$new_name$new_file_extension

echo $new_name
run_fix.in_hold.txt


but I feel my approach is not so elegant



note - because script extension could be bash or perl or python and also the target extension could be any thing after "." we want global replacement





I am using redhat 7.2










share|improve this question
























  • Don't give scripts file-name-extensions: The caller should not need to know what language a script is written in. Instead, use #!…
    – ctrl-alt-delor
    Dec 17 at 11:54










  • Remove . from your $PATH, before you break something.
    – ctrl-alt-delor
    Dec 17 at 11:54










  • Which OS, are you using?
    – ctrl-alt-delor
    Dec 17 at 11:55










  • redhat 7.2 version
    – yael
    Dec 17 at 15:16
















0














we want to replace the script file extension



so we did the following:



 new_name=` echo run_fix.bash  |  sed 's/[.].*$//' `
new_file_extension=".in_hold.txt"
new_name=$new_name$new_file_extension

echo $new_name
run_fix.in_hold.txt


but I feel my approach is not so elegant



note - because script extension could be bash or perl or python and also the target extension could be any thing after "." we want global replacement





I am using redhat 7.2










share|improve this question
























  • Don't give scripts file-name-extensions: The caller should not need to know what language a script is written in. Instead, use #!…
    – ctrl-alt-delor
    Dec 17 at 11:54










  • Remove . from your $PATH, before you break something.
    – ctrl-alt-delor
    Dec 17 at 11:54










  • Which OS, are you using?
    – ctrl-alt-delor
    Dec 17 at 11:55










  • redhat 7.2 version
    – yael
    Dec 17 at 15:16














0












0








0







we want to replace the script file extension



so we did the following:



 new_name=` echo run_fix.bash  |  sed 's/[.].*$//' `
new_file_extension=".in_hold.txt"
new_name=$new_name$new_file_extension

echo $new_name
run_fix.in_hold.txt


but I feel my approach is not so elegant



note - because script extension could be bash or perl or python and also the target extension could be any thing after "." we want global replacement





I am using redhat 7.2










share|improve this question















we want to replace the script file extension



so we did the following:



 new_name=` echo run_fix.bash  |  sed 's/[.].*$//' `
new_file_extension=".in_hold.txt"
new_name=$new_name$new_file_extension

echo $new_name
run_fix.in_hold.txt


but I feel my approach is not so elegant



note - because script extension could be bash or perl or python and also the target extension could be any thing after "." we want global replacement





I am using redhat 7.2







bash shell-script






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 17 at 15:35









ctrl-alt-delor

10.8k41957




10.8k41957










asked Dec 17 at 11:23









yael

2,42112159




2,42112159












  • Don't give scripts file-name-extensions: The caller should not need to know what language a script is written in. Instead, use #!…
    – ctrl-alt-delor
    Dec 17 at 11:54










  • Remove . from your $PATH, before you break something.
    – ctrl-alt-delor
    Dec 17 at 11:54










  • Which OS, are you using?
    – ctrl-alt-delor
    Dec 17 at 11:55










  • redhat 7.2 version
    – yael
    Dec 17 at 15:16


















  • Don't give scripts file-name-extensions: The caller should not need to know what language a script is written in. Instead, use #!…
    – ctrl-alt-delor
    Dec 17 at 11:54










  • Remove . from your $PATH, before you break something.
    – ctrl-alt-delor
    Dec 17 at 11:54










  • Which OS, are you using?
    – ctrl-alt-delor
    Dec 17 at 11:55










  • redhat 7.2 version
    – yael
    Dec 17 at 15:16
















Don't give scripts file-name-extensions: The caller should not need to know what language a script is written in. Instead, use #!…
– ctrl-alt-delor
Dec 17 at 11:54




Don't give scripts file-name-extensions: The caller should not need to know what language a script is written in. Instead, use #!…
– ctrl-alt-delor
Dec 17 at 11:54












Remove . from your $PATH, before you break something.
– ctrl-alt-delor
Dec 17 at 11:54




Remove . from your $PATH, before you break something.
– ctrl-alt-delor
Dec 17 at 11:54












Which OS, are you using?
– ctrl-alt-delor
Dec 17 at 11:55




Which OS, are you using?
– ctrl-alt-delor
Dec 17 at 11:55












redhat 7.2 version
– yael
Dec 17 at 15:16




redhat 7.2 version
– yael
Dec 17 at 15:16










2 Answers
2






active

oldest

votes


















7














old_name=run_fix.bash
new_name=${old_name%.bash}.in_hold.txt

printf 'New name: %sn' "$new_name"


This would remove the filename suffix .bash from the value of $old_name and add .in_hold.txt to the result of that. The whole thing would be assigned to the variable new_name.



The expansion ${variable%word} to remove the shortest matching suffix string from the value of $variable is a standard parameter expansion.



To replace any filename suffix (i.e. anything after the last dot in the filename):



new_name=${old_name%.*}.new_suffix


The .* pattern would match the last dot and anything after it (this would be removed). Had you used %% instead of %, you would have removed everything after the first dot in the string. If the string does not contain any dots, it remains unaltered.






share|improve this answer































    0














    Try the rename command, by larry wall. Not installed by default on all systems, and some have a different command with this name. It is definitely available for Debian based systems.



    rename is sed for filenames.






    share|improve this answer





















    • It's more like Perl for filenames as as far as I can tell, you could put a whole perl program into a rename operation, given that you use the Perl implementation of rename.
      – Kusalananda
      Dec 17 at 11:55








    • 1




      @Kusalananda yes it is very flexible, but you can also just use it like sed (but for file-names). No need to scare people.
      – ctrl-alt-delor
      Dec 17 at 11:57











    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',
    autoActivateHeartbeat: false,
    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
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f489453%2fhow-to-replace-file-extension%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    7














    old_name=run_fix.bash
    new_name=${old_name%.bash}.in_hold.txt

    printf 'New name: %sn' "$new_name"


    This would remove the filename suffix .bash from the value of $old_name and add .in_hold.txt to the result of that. The whole thing would be assigned to the variable new_name.



    The expansion ${variable%word} to remove the shortest matching suffix string from the value of $variable is a standard parameter expansion.



    To replace any filename suffix (i.e. anything after the last dot in the filename):



    new_name=${old_name%.*}.new_suffix


    The .* pattern would match the last dot and anything after it (this would be removed). Had you used %% instead of %, you would have removed everything after the first dot in the string. If the string does not contain any dots, it remains unaltered.






    share|improve this answer




























      7














      old_name=run_fix.bash
      new_name=${old_name%.bash}.in_hold.txt

      printf 'New name: %sn' "$new_name"


      This would remove the filename suffix .bash from the value of $old_name and add .in_hold.txt to the result of that. The whole thing would be assigned to the variable new_name.



      The expansion ${variable%word} to remove the shortest matching suffix string from the value of $variable is a standard parameter expansion.



      To replace any filename suffix (i.e. anything after the last dot in the filename):



      new_name=${old_name%.*}.new_suffix


      The .* pattern would match the last dot and anything after it (this would be removed). Had you used %% instead of %, you would have removed everything after the first dot in the string. If the string does not contain any dots, it remains unaltered.






      share|improve this answer


























        7












        7








        7






        old_name=run_fix.bash
        new_name=${old_name%.bash}.in_hold.txt

        printf 'New name: %sn' "$new_name"


        This would remove the filename suffix .bash from the value of $old_name and add .in_hold.txt to the result of that. The whole thing would be assigned to the variable new_name.



        The expansion ${variable%word} to remove the shortest matching suffix string from the value of $variable is a standard parameter expansion.



        To replace any filename suffix (i.e. anything after the last dot in the filename):



        new_name=${old_name%.*}.new_suffix


        The .* pattern would match the last dot and anything after it (this would be removed). Had you used %% instead of %, you would have removed everything after the first dot in the string. If the string does not contain any dots, it remains unaltered.






        share|improve this answer














        old_name=run_fix.bash
        new_name=${old_name%.bash}.in_hold.txt

        printf 'New name: %sn' "$new_name"


        This would remove the filename suffix .bash from the value of $old_name and add .in_hold.txt to the result of that. The whole thing would be assigned to the variable new_name.



        The expansion ${variable%word} to remove the shortest matching suffix string from the value of $variable is a standard parameter expansion.



        To replace any filename suffix (i.e. anything after the last dot in the filename):



        new_name=${old_name%.*}.new_suffix


        The .* pattern would match the last dot and anything after it (this would be removed). Had you used %% instead of %, you would have removed everything after the first dot in the string. If the string does not contain any dots, it remains unaltered.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 17 at 12:40

























        answered Dec 17 at 11:57









        Kusalananda

        121k16229372




        121k16229372

























            0














            Try the rename command, by larry wall. Not installed by default on all systems, and some have a different command with this name. It is definitely available for Debian based systems.



            rename is sed for filenames.






            share|improve this answer





















            • It's more like Perl for filenames as as far as I can tell, you could put a whole perl program into a rename operation, given that you use the Perl implementation of rename.
              – Kusalananda
              Dec 17 at 11:55








            • 1




              @Kusalananda yes it is very flexible, but you can also just use it like sed (but for file-names). No need to scare people.
              – ctrl-alt-delor
              Dec 17 at 11:57
















            0














            Try the rename command, by larry wall. Not installed by default on all systems, and some have a different command with this name. It is definitely available for Debian based systems.



            rename is sed for filenames.






            share|improve this answer





















            • It's more like Perl for filenames as as far as I can tell, you could put a whole perl program into a rename operation, given that you use the Perl implementation of rename.
              – Kusalananda
              Dec 17 at 11:55








            • 1




              @Kusalananda yes it is very flexible, but you can also just use it like sed (but for file-names). No need to scare people.
              – ctrl-alt-delor
              Dec 17 at 11:57














            0












            0








            0






            Try the rename command, by larry wall. Not installed by default on all systems, and some have a different command with this name. It is definitely available for Debian based systems.



            rename is sed for filenames.






            share|improve this answer












            Try the rename command, by larry wall. Not installed by default on all systems, and some have a different command with this name. It is definitely available for Debian based systems.



            rename is sed for filenames.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Dec 17 at 11:52









            ctrl-alt-delor

            10.8k41957




            10.8k41957












            • It's more like Perl for filenames as as far as I can tell, you could put a whole perl program into a rename operation, given that you use the Perl implementation of rename.
              – Kusalananda
              Dec 17 at 11:55








            • 1




              @Kusalananda yes it is very flexible, but you can also just use it like sed (but for file-names). No need to scare people.
              – ctrl-alt-delor
              Dec 17 at 11:57


















            • It's more like Perl for filenames as as far as I can tell, you could put a whole perl program into a rename operation, given that you use the Perl implementation of rename.
              – Kusalananda
              Dec 17 at 11:55








            • 1




              @Kusalananda yes it is very flexible, but you can also just use it like sed (but for file-names). No need to scare people.
              – ctrl-alt-delor
              Dec 17 at 11:57
















            It's more like Perl for filenames as as far as I can tell, you could put a whole perl program into a rename operation, given that you use the Perl implementation of rename.
            – Kusalananda
            Dec 17 at 11:55






            It's more like Perl for filenames as as far as I can tell, you could put a whole perl program into a rename operation, given that you use the Perl implementation of rename.
            – Kusalananda
            Dec 17 at 11:55






            1




            1




            @Kusalananda yes it is very flexible, but you can also just use it like sed (but for file-names). No need to scare people.
            – ctrl-alt-delor
            Dec 17 at 11:57




            @Kusalananda yes it is very flexible, but you can also just use it like sed (but for file-names). No need to scare people.
            – ctrl-alt-delor
            Dec 17 at 11:57


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


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





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2funix.stackexchange.com%2fquestions%2f489453%2fhow-to-replace-file-extension%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

            Morgemoulin

            Scott Moir

            Souastre