'rename' with expression|replacement with a leading '-' (hyphen|minus)












2














I have many files like xyz_123_foo.ext for which I would like to add -bar to the filenames at the end to result in xyz_123_foo-bar.ext. I tried:



rename . -bar. xyz_*


which resulted in:



rename: invalid option -- 'b'


followed by the usage text. I then tried variations with '-bar' and "-bar" to no avail.



How can I get rename to accept - as part of the replacement string?

Or would another command be more efficient or appropriate?



My shell is bash and I am using the rename from util-linux on SuSe Linux SLE12.










share|improve this question
























  • Which rename are you using? [There are many.]What's with all the renames: prename, rename, file-rename?). Also, what OS are you using?
    – terdon
    Nov 28 '18 at 17:30
















2














I have many files like xyz_123_foo.ext for which I would like to add -bar to the filenames at the end to result in xyz_123_foo-bar.ext. I tried:



rename . -bar. xyz_*


which resulted in:



rename: invalid option -- 'b'


followed by the usage text. I then tried variations with '-bar' and "-bar" to no avail.



How can I get rename to accept - as part of the replacement string?

Or would another command be more efficient or appropriate?



My shell is bash and I am using the rename from util-linux on SuSe Linux SLE12.










share|improve this question
























  • Which rename are you using? [There are many.]What's with all the renames: prename, rename, file-rename?). Also, what OS are you using?
    – terdon
    Nov 28 '18 at 17:30














2












2








2







I have many files like xyz_123_foo.ext for which I would like to add -bar to the filenames at the end to result in xyz_123_foo-bar.ext. I tried:



rename . -bar. xyz_*


which resulted in:



rename: invalid option -- 'b'


followed by the usage text. I then tried variations with '-bar' and "-bar" to no avail.



How can I get rename to accept - as part of the replacement string?

Or would another command be more efficient or appropriate?



My shell is bash and I am using the rename from util-linux on SuSe Linux SLE12.










share|improve this question















I have many files like xyz_123_foo.ext for which I would like to add -bar to the filenames at the end to result in xyz_123_foo-bar.ext. I tried:



rename . -bar. xyz_*


which resulted in:



rename: invalid option -- 'b'


followed by the usage text. I then tried variations with '-bar' and "-bar" to no avail.



How can I get rename to accept - as part of the replacement string?

Or would another command be more efficient or appropriate?



My shell is bash and I am using the rename from util-linux on SuSe Linux SLE12.







rename string arguments options






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 29 '18 at 9:02









terdon

128k31249424




128k31249424










asked Nov 28 '18 at 14:23









astzge2

133




133












  • Which rename are you using? [There are many.]What's with all the renames: prename, rename, file-rename?). Also, what OS are you using?
    – terdon
    Nov 28 '18 at 17:30


















  • Which rename are you using? [There are many.]What's with all the renames: prename, rename, file-rename?). Also, what OS are you using?
    – terdon
    Nov 28 '18 at 17:30
















Which rename are you using? [There are many.]What's with all the renames: prename, rename, file-rename?). Also, what OS are you using?
– terdon
Nov 28 '18 at 17:30




Which rename are you using? [There are many.]What's with all the renames: prename, rename, file-rename?). Also, what OS are you using?
– terdon
Nov 28 '18 at 17:30










3 Answers
3






active

oldest

votes


















4














mmv is nice for tasks like this ex.



mmv -n -- '*.ext' '#1-bar.ext'


or for any dot extension



mmv -n -- '*.*' '#1-bar.#2'


Remove the -n once you are happy that it is doing the right thing.






share|improve this answer























  • This seems really elegant, but I get an error and the usage when I use the -n Option: > mmv -n '*.*' '#1-bar.#2' .../mmv: illegal option -- n .../mmv: [ -b ] [ -f Files ] [ -s Source ] [ -t Target ] Without -n nothing seems to happen
    – astzge2
    Nov 28 '18 at 16:00












  • @astzge2 it seems our mmvs are different unfortunately - what is your OS?
    – steeldriver
    Nov 28 '18 at 16:07










  • I'm on SLE12. However, you've inadvertently answered my question! The -- end-of-options in Linux was unknown to me until I saw your edit. Including that caused rename to stop interpreting -bar. as a -b option. Thanks!
    – astzge2
    Nov 29 '18 at 8:01



















3














Assuming that your rename is the Perl variant of the rename utility:



rename 's/$/-bak/' xyz_*


This uses a Perl expression that simply inserts -bak at the end of all given filenames through a substitution of $ (the "end of line" anchor).



You could also use



rename '$_ .= "-bak"' xyz_*


which uses another Perl expression that appends the string -bak to $_ (the "current thing" in Perl, here it's a filename).



For a shell loop solution:



for name in xyz_*; do
mv -i "$name" "$name-bak"
done





share|improve this answer























  • The Perl rename won't help if they have the util-linux one...
    – ilkkachu
    Nov 28 '18 at 15:41










  • @ilkkachu Added short note about that. That's also why I complemented the answer with a standard shell loop.
    – Kusalananda
    Nov 28 '18 at 16:01



















1














Assuming we're talking about the PERL extension, rename (and not the rename from util-linux)...



rename 's/.ext/-bar.ext/' xyz_123_foo.ext


The result changes the file name to xyz_123_foo-bar.ext.



The portion within single quotes is a PERL regular expression. The s substitutes .ext in the file name with -bar.ext. One might combine this with find, assuming these files are in your home directory (~).



find ~ -type f -name '*.ext' -exec rename 's/.ext/-bar.ext/' {} +





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',
    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%2f484675%2frename-with-expressionreplacement-with-a-leading-hyphenminus%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4














    mmv is nice for tasks like this ex.



    mmv -n -- '*.ext' '#1-bar.ext'


    or for any dot extension



    mmv -n -- '*.*' '#1-bar.#2'


    Remove the -n once you are happy that it is doing the right thing.






    share|improve this answer























    • This seems really elegant, but I get an error and the usage when I use the -n Option: > mmv -n '*.*' '#1-bar.#2' .../mmv: illegal option -- n .../mmv: [ -b ] [ -f Files ] [ -s Source ] [ -t Target ] Without -n nothing seems to happen
      – astzge2
      Nov 28 '18 at 16:00












    • @astzge2 it seems our mmvs are different unfortunately - what is your OS?
      – steeldriver
      Nov 28 '18 at 16:07










    • I'm on SLE12. However, you've inadvertently answered my question! The -- end-of-options in Linux was unknown to me until I saw your edit. Including that caused rename to stop interpreting -bar. as a -b option. Thanks!
      – astzge2
      Nov 29 '18 at 8:01
















    4














    mmv is nice for tasks like this ex.



    mmv -n -- '*.ext' '#1-bar.ext'


    or for any dot extension



    mmv -n -- '*.*' '#1-bar.#2'


    Remove the -n once you are happy that it is doing the right thing.






    share|improve this answer























    • This seems really elegant, but I get an error and the usage when I use the -n Option: > mmv -n '*.*' '#1-bar.#2' .../mmv: illegal option -- n .../mmv: [ -b ] [ -f Files ] [ -s Source ] [ -t Target ] Without -n nothing seems to happen
      – astzge2
      Nov 28 '18 at 16:00












    • @astzge2 it seems our mmvs are different unfortunately - what is your OS?
      – steeldriver
      Nov 28 '18 at 16:07










    • I'm on SLE12. However, you've inadvertently answered my question! The -- end-of-options in Linux was unknown to me until I saw your edit. Including that caused rename to stop interpreting -bar. as a -b option. Thanks!
      – astzge2
      Nov 29 '18 at 8:01














    4












    4








    4






    mmv is nice for tasks like this ex.



    mmv -n -- '*.ext' '#1-bar.ext'


    or for any dot extension



    mmv -n -- '*.*' '#1-bar.#2'


    Remove the -n once you are happy that it is doing the right thing.






    share|improve this answer














    mmv is nice for tasks like this ex.



    mmv -n -- '*.ext' '#1-bar.ext'


    or for any dot extension



    mmv -n -- '*.*' '#1-bar.#2'


    Remove the -n once you are happy that it is doing the right thing.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 28 '18 at 16:04

























    answered Nov 28 '18 at 14:43









    steeldriver

    34.5k35083




    34.5k35083












    • This seems really elegant, but I get an error and the usage when I use the -n Option: > mmv -n '*.*' '#1-bar.#2' .../mmv: illegal option -- n .../mmv: [ -b ] [ -f Files ] [ -s Source ] [ -t Target ] Without -n nothing seems to happen
      – astzge2
      Nov 28 '18 at 16:00












    • @astzge2 it seems our mmvs are different unfortunately - what is your OS?
      – steeldriver
      Nov 28 '18 at 16:07










    • I'm on SLE12. However, you've inadvertently answered my question! The -- end-of-options in Linux was unknown to me until I saw your edit. Including that caused rename to stop interpreting -bar. as a -b option. Thanks!
      – astzge2
      Nov 29 '18 at 8:01


















    • This seems really elegant, but I get an error and the usage when I use the -n Option: > mmv -n '*.*' '#1-bar.#2' .../mmv: illegal option -- n .../mmv: [ -b ] [ -f Files ] [ -s Source ] [ -t Target ] Without -n nothing seems to happen
      – astzge2
      Nov 28 '18 at 16:00












    • @astzge2 it seems our mmvs are different unfortunately - what is your OS?
      – steeldriver
      Nov 28 '18 at 16:07










    • I'm on SLE12. However, you've inadvertently answered my question! The -- end-of-options in Linux was unknown to me until I saw your edit. Including that caused rename to stop interpreting -bar. as a -b option. Thanks!
      – astzge2
      Nov 29 '18 at 8:01
















    This seems really elegant, but I get an error and the usage when I use the -n Option: > mmv -n '*.*' '#1-bar.#2' .../mmv: illegal option -- n .../mmv: [ -b ] [ -f Files ] [ -s Source ] [ -t Target ] Without -n nothing seems to happen
    – astzge2
    Nov 28 '18 at 16:00






    This seems really elegant, but I get an error and the usage when I use the -n Option: > mmv -n '*.*' '#1-bar.#2' .../mmv: illegal option -- n .../mmv: [ -b ] [ -f Files ] [ -s Source ] [ -t Target ] Without -n nothing seems to happen
    – astzge2
    Nov 28 '18 at 16:00














    @astzge2 it seems our mmvs are different unfortunately - what is your OS?
    – steeldriver
    Nov 28 '18 at 16:07




    @astzge2 it seems our mmvs are different unfortunately - what is your OS?
    – steeldriver
    Nov 28 '18 at 16:07












    I'm on SLE12. However, you've inadvertently answered my question! The -- end-of-options in Linux was unknown to me until I saw your edit. Including that caused rename to stop interpreting -bar. as a -b option. Thanks!
    – astzge2
    Nov 29 '18 at 8:01




    I'm on SLE12. However, you've inadvertently answered my question! The -- end-of-options in Linux was unknown to me until I saw your edit. Including that caused rename to stop interpreting -bar. as a -b option. Thanks!
    – astzge2
    Nov 29 '18 at 8:01













    3














    Assuming that your rename is the Perl variant of the rename utility:



    rename 's/$/-bak/' xyz_*


    This uses a Perl expression that simply inserts -bak at the end of all given filenames through a substitution of $ (the "end of line" anchor).



    You could also use



    rename '$_ .= "-bak"' xyz_*


    which uses another Perl expression that appends the string -bak to $_ (the "current thing" in Perl, here it's a filename).



    For a shell loop solution:



    for name in xyz_*; do
    mv -i "$name" "$name-bak"
    done





    share|improve this answer























    • The Perl rename won't help if they have the util-linux one...
      – ilkkachu
      Nov 28 '18 at 15:41










    • @ilkkachu Added short note about that. That's also why I complemented the answer with a standard shell loop.
      – Kusalananda
      Nov 28 '18 at 16:01
















    3














    Assuming that your rename is the Perl variant of the rename utility:



    rename 's/$/-bak/' xyz_*


    This uses a Perl expression that simply inserts -bak at the end of all given filenames through a substitution of $ (the "end of line" anchor).



    You could also use



    rename '$_ .= "-bak"' xyz_*


    which uses another Perl expression that appends the string -bak to $_ (the "current thing" in Perl, here it's a filename).



    For a shell loop solution:



    for name in xyz_*; do
    mv -i "$name" "$name-bak"
    done





    share|improve this answer























    • The Perl rename won't help if they have the util-linux one...
      – ilkkachu
      Nov 28 '18 at 15:41










    • @ilkkachu Added short note about that. That's also why I complemented the answer with a standard shell loop.
      – Kusalananda
      Nov 28 '18 at 16:01














    3












    3








    3






    Assuming that your rename is the Perl variant of the rename utility:



    rename 's/$/-bak/' xyz_*


    This uses a Perl expression that simply inserts -bak at the end of all given filenames through a substitution of $ (the "end of line" anchor).



    You could also use



    rename '$_ .= "-bak"' xyz_*


    which uses another Perl expression that appends the string -bak to $_ (the "current thing" in Perl, here it's a filename).



    For a shell loop solution:



    for name in xyz_*; do
    mv -i "$name" "$name-bak"
    done





    share|improve this answer














    Assuming that your rename is the Perl variant of the rename utility:



    rename 's/$/-bak/' xyz_*


    This uses a Perl expression that simply inserts -bak at the end of all given filenames through a substitution of $ (the "end of line" anchor).



    You could also use



    rename '$_ .= "-bak"' xyz_*


    which uses another Perl expression that appends the string -bak to $_ (the "current thing" in Perl, here it's a filename).



    For a shell loop solution:



    for name in xyz_*; do
    mv -i "$name" "$name-bak"
    done






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 28 '18 at 16:00

























    answered Nov 28 '18 at 14:31









    Kusalananda

    122k16230375




    122k16230375












    • The Perl rename won't help if they have the util-linux one...
      – ilkkachu
      Nov 28 '18 at 15:41










    • @ilkkachu Added short note about that. That's also why I complemented the answer with a standard shell loop.
      – Kusalananda
      Nov 28 '18 at 16:01


















    • The Perl rename won't help if they have the util-linux one...
      – ilkkachu
      Nov 28 '18 at 15:41










    • @ilkkachu Added short note about that. That's also why I complemented the answer with a standard shell loop.
      – Kusalananda
      Nov 28 '18 at 16:01
















    The Perl rename won't help if they have the util-linux one...
    – ilkkachu
    Nov 28 '18 at 15:41




    The Perl rename won't help if they have the util-linux one...
    – ilkkachu
    Nov 28 '18 at 15:41












    @ilkkachu Added short note about that. That's also why I complemented the answer with a standard shell loop.
    – Kusalananda
    Nov 28 '18 at 16:01




    @ilkkachu Added short note about that. That's also why I complemented the answer with a standard shell loop.
    – Kusalananda
    Nov 28 '18 at 16:01











    1














    Assuming we're talking about the PERL extension, rename (and not the rename from util-linux)...



    rename 's/.ext/-bar.ext/' xyz_123_foo.ext


    The result changes the file name to xyz_123_foo-bar.ext.



    The portion within single quotes is a PERL regular expression. The s substitutes .ext in the file name with -bar.ext. One might combine this with find, assuming these files are in your home directory (~).



    find ~ -type f -name '*.ext' -exec rename 's/.ext/-bar.ext/' {} +





    share|improve this answer


























      1














      Assuming we're talking about the PERL extension, rename (and not the rename from util-linux)...



      rename 's/.ext/-bar.ext/' xyz_123_foo.ext


      The result changes the file name to xyz_123_foo-bar.ext.



      The portion within single quotes is a PERL regular expression. The s substitutes .ext in the file name with -bar.ext. One might combine this with find, assuming these files are in your home directory (~).



      find ~ -type f -name '*.ext' -exec rename 's/.ext/-bar.ext/' {} +





      share|improve this answer
























        1












        1








        1






        Assuming we're talking about the PERL extension, rename (and not the rename from util-linux)...



        rename 's/.ext/-bar.ext/' xyz_123_foo.ext


        The result changes the file name to xyz_123_foo-bar.ext.



        The portion within single quotes is a PERL regular expression. The s substitutes .ext in the file name with -bar.ext. One might combine this with find, assuming these files are in your home directory (~).



        find ~ -type f -name '*.ext' -exec rename 's/.ext/-bar.ext/' {} +





        share|improve this answer












        Assuming we're talking about the PERL extension, rename (and not the rename from util-linux)...



        rename 's/.ext/-bar.ext/' xyz_123_foo.ext


        The result changes the file name to xyz_123_foo-bar.ext.



        The portion within single quotes is a PERL regular expression. The s substitutes .ext in the file name with -bar.ext. One might combine this with find, assuming these files are in your home directory (~).



        find ~ -type f -name '*.ext' -exec rename 's/.ext/-bar.ext/' {} +






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 28 '18 at 16:00









        Christopher

        10.2k32947




        10.2k32947






























            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%2f484675%2frename-with-expressionreplacement-with-a-leading-hyphenminus%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

            Bundesstraße 106

            Verónica Boquete

            Ida-Boy-Ed-Garten