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











up vote
2
down vote

favorite












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









New contributor




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




















  • 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 at 17:30















up vote
2
down vote

favorite












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









New contributor




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




















  • 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 at 17:30













up vote
2
down vote

favorite









up vote
2
down vote

favorite











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









New contributor




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











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









New contributor




astzge2 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




astzge2 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 Nov 29 at 9:02









terdon

127k31244421




127k31244421






New contributor




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









asked Nov 28 at 14:23









astzge2

133




133




New contributor




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





New contributor





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






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












  • 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 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 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 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 at 17:30










3 Answers
3






active

oldest

votes

















up vote
4
down vote



accepted










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 at 16:00












  • @astzge2 it seems our mmvs are different unfortunately - what is your OS?
    – steeldriver
    Nov 28 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 at 8:01


















up vote
3
down vote













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 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 at 16:01


















up vote
1
down vote













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


    }
    });






    astzge2 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%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








    up vote
    4
    down vote



    accepted










    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 at 16:00












    • @astzge2 it seems our mmvs are different unfortunately - what is your OS?
      – steeldriver
      Nov 28 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 at 8:01















    up vote
    4
    down vote



    accepted










    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 at 16:00












    • @astzge2 it seems our mmvs are different unfortunately - what is your OS?
      – steeldriver
      Nov 28 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 at 8:01













    up vote
    4
    down vote



    accepted







    up vote
    4
    down vote



    accepted






    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 at 16:04

























    answered Nov 28 at 14:43









    steeldriver

    33.7k34983




    33.7k34983












    • 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 at 16:00












    • @astzge2 it seems our mmvs are different unfortunately - what is your OS?
      – steeldriver
      Nov 28 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 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 at 16:00












    • @astzge2 it seems our mmvs are different unfortunately - what is your OS?
      – steeldriver
      Nov 28 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 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 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 at 16:00














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




    @astzge2 it seems our mmvs are different unfortunately - what is your OS?
    – steeldriver
    Nov 28 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 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 at 8:01












    up vote
    3
    down vote













    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 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 at 16:01















    up vote
    3
    down vote













    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 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 at 16:01













    up vote
    3
    down vote










    up vote
    3
    down vote









    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 at 16:00

























    answered Nov 28 at 14:31









    Kusalananda

    118k16223361




    118k16223361












    • The Perl rename won't help if they have the util-linux one...
      – ilkkachu
      Nov 28 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 at 16:01


















    • The Perl rename won't help if they have the util-linux one...
      – ilkkachu
      Nov 28 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 at 16:01
















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




    The Perl rename won't help if they have the util-linux one...
    – ilkkachu
    Nov 28 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 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 at 16:01










    up vote
    1
    down vote













    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

























      up vote
      1
      down vote













      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























        up vote
        1
        down vote










        up vote
        1
        down vote









        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 at 16:00









        Christopher

        9,40032846




        9,40032846






















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










            draft saved

            draft discarded


















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













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












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
















            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

            AnyDesk - Fatal Program Failure

            How to calibrate 16:9 built-in touch-screen to a 4:3 resolution?

            QoS: MAC-Priority for clients behind a repeater