Rename Files to Same Filename Without Extension with find Command

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











up vote
0
down vote

favorite
1












I'd like to rename a group of files to their same filename but without extension. They are scattered in my home directory and I don't know where they are located precisely, but I do know the extension to be removed.



I need to have one and only one command to perform this task.



Here's the partial command:



$ find ~/ -type f -name "*.hhs" -exec mv <I DO NOT KNOW> ;


Where <I DO NOT KNOW> is to be replaced by your suggestion. For example, I tried to replace it with the following:



`basename .hhs`


It does not work for me.




Example



If files file1.jpg.hhs and file2.jpg.hhs are found, I'd like them to be renamed as follows: file1.jpg and file2.jpg










share|improve this question



















  • 1




    why do you need only one command? I'm curious.
    – Jeff Schaller
    Nov 28 at 14:17










  • Your find example uses cp , which would create a copy yet your title and body of the question imply a rename -- what's the goal?
    – Jeff Schaller
    Nov 28 at 14:18










  • My apologies, I corrected the title.
    – Faxopita
    Nov 28 at 14:28










  • The reason why I want everything under one command: for the beauty. I like to condense. Why creating a multiline script with variables while my task could be solved under one command only.
    – Faxopita
    Nov 28 at 14:31














up vote
0
down vote

favorite
1












I'd like to rename a group of files to their same filename but without extension. They are scattered in my home directory and I don't know where they are located precisely, but I do know the extension to be removed.



I need to have one and only one command to perform this task.



Here's the partial command:



$ find ~/ -type f -name "*.hhs" -exec mv <I DO NOT KNOW> ;


Where <I DO NOT KNOW> is to be replaced by your suggestion. For example, I tried to replace it with the following:



`basename .hhs`


It does not work for me.




Example



If files file1.jpg.hhs and file2.jpg.hhs are found, I'd like them to be renamed as follows: file1.jpg and file2.jpg










share|improve this question



















  • 1




    why do you need only one command? I'm curious.
    – Jeff Schaller
    Nov 28 at 14:17










  • Your find example uses cp , which would create a copy yet your title and body of the question imply a rename -- what's the goal?
    – Jeff Schaller
    Nov 28 at 14:18










  • My apologies, I corrected the title.
    – Faxopita
    Nov 28 at 14:28










  • The reason why I want everything under one command: for the beauty. I like to condense. Why creating a multiline script with variables while my task could be solved under one command only.
    – Faxopita
    Nov 28 at 14:31












up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





I'd like to rename a group of files to their same filename but without extension. They are scattered in my home directory and I don't know where they are located precisely, but I do know the extension to be removed.



I need to have one and only one command to perform this task.



Here's the partial command:



$ find ~/ -type f -name "*.hhs" -exec mv <I DO NOT KNOW> ;


Where <I DO NOT KNOW> is to be replaced by your suggestion. For example, I tried to replace it with the following:



`basename .hhs`


It does not work for me.




Example



If files file1.jpg.hhs and file2.jpg.hhs are found, I'd like them to be renamed as follows: file1.jpg and file2.jpg










share|improve this question















I'd like to rename a group of files to their same filename but without extension. They are scattered in my home directory and I don't know where they are located precisely, but I do know the extension to be removed.



I need to have one and only one command to perform this task.



Here's the partial command:



$ find ~/ -type f -name "*.hhs" -exec mv <I DO NOT KNOW> ;


Where <I DO NOT KNOW> is to be replaced by your suggestion. For example, I tried to replace it with the following:



`basename .hhs`


It does not work for me.




Example



If files file1.jpg.hhs and file2.jpg.hhs are found, I'd like them to be renamed as follows: file1.jpg and file2.jpg







find filenames cp






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 28 at 14:49

























asked Nov 28 at 14:16









Faxopita

327




327







  • 1




    why do you need only one command? I'm curious.
    – Jeff Schaller
    Nov 28 at 14:17










  • Your find example uses cp , which would create a copy yet your title and body of the question imply a rename -- what's the goal?
    – Jeff Schaller
    Nov 28 at 14:18










  • My apologies, I corrected the title.
    – Faxopita
    Nov 28 at 14:28










  • The reason why I want everything under one command: for the beauty. I like to condense. Why creating a multiline script with variables while my task could be solved under one command only.
    – Faxopita
    Nov 28 at 14:31












  • 1




    why do you need only one command? I'm curious.
    – Jeff Schaller
    Nov 28 at 14:17










  • Your find example uses cp , which would create a copy yet your title and body of the question imply a rename -- what's the goal?
    – Jeff Schaller
    Nov 28 at 14:18










  • My apologies, I corrected the title.
    – Faxopita
    Nov 28 at 14:28










  • The reason why I want everything under one command: for the beauty. I like to condense. Why creating a multiline script with variables while my task could be solved under one command only.
    – Faxopita
    Nov 28 at 14:31







1




1




why do you need only one command? I'm curious.
– Jeff Schaller
Nov 28 at 14:17




why do you need only one command? I'm curious.
– Jeff Schaller
Nov 28 at 14:17












Your find example uses cp , which would create a copy yet your title and body of the question imply a rename -- what's the goal?
– Jeff Schaller
Nov 28 at 14:18




Your find example uses cp , which would create a copy yet your title and body of the question imply a rename -- what's the goal?
– Jeff Schaller
Nov 28 at 14:18












My apologies, I corrected the title.
– Faxopita
Nov 28 at 14:28




My apologies, I corrected the title.
– Faxopita
Nov 28 at 14:28












The reason why I want everything under one command: for the beauty. I like to condense. Why creating a multiline script with variables while my task could be solved under one command only.
– Faxopita
Nov 28 at 14:31




The reason why I want everything under one command: for the beauty. I like to condense. Why creating a multiline script with variables while my task could be solved under one command only.
– Faxopita
Nov 28 at 14:31










2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










If you have or can install the PERL extension, rename (apt-get install rename on a Debian-based distribution)...



find ~ -type f -name "*.hhs" -exec rename -d ".hhs" +


The option, -d or --delete deletes the specified string.






share|improve this answer



























    up vote
    1
    down vote













    You require a tool that can delete the .hhs filename suffix from the filename. find can not do this for you, so you will have to call another utility through -exec:



    find "$HOME" -type f -name '*.hhs' -exec sh -c '
    for pathname do
    cp -i "$pathname" "$pathname%.hhs"
    done' sh +


    I'm not using basename here as that would also remove the directory path from the found pathnames (which could be inserted again with dirname, but it would yield messy code).



    Instead, I call an in-line shell script with as many found pathnames as possible, and let the script loop over these, copying the individual files. The new name is constructed using a standard parameter substitution that deletes the string .hhs from the end of the value of $pathname.



    Related:



    • Understanding the -exec option of `find`





    share|improve this answer






















    • @JeffSchaller Noted.
      – Kusalananda
      Nov 28 at 14:40










    • +1 Great method, @Kusalananda :-) Please explain how the shellscript works in this case: how is pathname given its value? What is the difference compared to find . -name "*.pdf" -exec bash -c 'echo cp -p "$1" "$1%%.pdf"' bash ; which works for me (I tested locally and for pdf files).
      – sudodus
      Nov 28 at 15:14










    • @sudodus I hoped to avoid that explanation by linking to a more expressive general answer regarding the various uses of -exec with find. But in general, find passes the found pathnames to the utility that you call from -exec, replacing with the pathname that it has found. With -exec ... +, find will replace with not one pathname, but with as many as possible. This means that the script that I have is called with a number of pathnames, and the loop in the script loops over these.
      – Kusalananda
      Nov 28 at 15:44











    • @sudodus In your example with bash, the bash shell will be invoked once for each file. This is slow if there are many files.
      – Kusalananda
      Nov 28 at 15:45










    • Thanks. But one more question. I have learned that a shellscript will use $1 etc as parameters. How come you can use pathname for this purpose? Will the first available variable be used in these cases?
      – sudodus
      Nov 28 at 15:52











    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
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f484674%2frename-files-to-same-filename-without-extension-with-find-command%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








    up vote
    2
    down vote



    accepted










    If you have or can install the PERL extension, rename (apt-get install rename on a Debian-based distribution)...



    find ~ -type f -name "*.hhs" -exec rename -d ".hhs" +


    The option, -d or --delete deletes the specified string.






    share|improve this answer
























      up vote
      2
      down vote



      accepted










      If you have or can install the PERL extension, rename (apt-get install rename on a Debian-based distribution)...



      find ~ -type f -name "*.hhs" -exec rename -d ".hhs" +


      The option, -d or --delete deletes the specified string.






      share|improve this answer






















        up vote
        2
        down vote



        accepted







        up vote
        2
        down vote



        accepted






        If you have or can install the PERL extension, rename (apt-get install rename on a Debian-based distribution)...



        find ~ -type f -name "*.hhs" -exec rename -d ".hhs" +


        The option, -d or --delete deletes the specified string.






        share|improve this answer












        If you have or can install the PERL extension, rename (apt-get install rename on a Debian-based distribution)...



        find ~ -type f -name "*.hhs" -exec rename -d ".hhs" +


        The option, -d or --delete deletes the specified string.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 28 at 14:36









        Christopher

        9,50032846




        9,50032846






















            up vote
            1
            down vote













            You require a tool that can delete the .hhs filename suffix from the filename. find can not do this for you, so you will have to call another utility through -exec:



            find "$HOME" -type f -name '*.hhs' -exec sh -c '
            for pathname do
            cp -i "$pathname" "$pathname%.hhs"
            done' sh +


            I'm not using basename here as that would also remove the directory path from the found pathnames (which could be inserted again with dirname, but it would yield messy code).



            Instead, I call an in-line shell script with as many found pathnames as possible, and let the script loop over these, copying the individual files. The new name is constructed using a standard parameter substitution that deletes the string .hhs from the end of the value of $pathname.



            Related:



            • Understanding the -exec option of `find`





            share|improve this answer






















            • @JeffSchaller Noted.
              – Kusalananda
              Nov 28 at 14:40










            • +1 Great method, @Kusalananda :-) Please explain how the shellscript works in this case: how is pathname given its value? What is the difference compared to find . -name "*.pdf" -exec bash -c 'echo cp -p "$1" "$1%%.pdf"' bash ; which works for me (I tested locally and for pdf files).
              – sudodus
              Nov 28 at 15:14










            • @sudodus I hoped to avoid that explanation by linking to a more expressive general answer regarding the various uses of -exec with find. But in general, find passes the found pathnames to the utility that you call from -exec, replacing with the pathname that it has found. With -exec ... +, find will replace with not one pathname, but with as many as possible. This means that the script that I have is called with a number of pathnames, and the loop in the script loops over these.
              – Kusalananda
              Nov 28 at 15:44











            • @sudodus In your example with bash, the bash shell will be invoked once for each file. This is slow if there are many files.
              – Kusalananda
              Nov 28 at 15:45










            • Thanks. But one more question. I have learned that a shellscript will use $1 etc as parameters. How come you can use pathname for this purpose? Will the first available variable be used in these cases?
              – sudodus
              Nov 28 at 15:52















            up vote
            1
            down vote













            You require a tool that can delete the .hhs filename suffix from the filename. find can not do this for you, so you will have to call another utility through -exec:



            find "$HOME" -type f -name '*.hhs' -exec sh -c '
            for pathname do
            cp -i "$pathname" "$pathname%.hhs"
            done' sh +


            I'm not using basename here as that would also remove the directory path from the found pathnames (which could be inserted again with dirname, but it would yield messy code).



            Instead, I call an in-line shell script with as many found pathnames as possible, and let the script loop over these, copying the individual files. The new name is constructed using a standard parameter substitution that deletes the string .hhs from the end of the value of $pathname.



            Related:



            • Understanding the -exec option of `find`





            share|improve this answer






















            • @JeffSchaller Noted.
              – Kusalananda
              Nov 28 at 14:40










            • +1 Great method, @Kusalananda :-) Please explain how the shellscript works in this case: how is pathname given its value? What is the difference compared to find . -name "*.pdf" -exec bash -c 'echo cp -p "$1" "$1%%.pdf"' bash ; which works for me (I tested locally and for pdf files).
              – sudodus
              Nov 28 at 15:14










            • @sudodus I hoped to avoid that explanation by linking to a more expressive general answer regarding the various uses of -exec with find. But in general, find passes the found pathnames to the utility that you call from -exec, replacing with the pathname that it has found. With -exec ... +, find will replace with not one pathname, but with as many as possible. This means that the script that I have is called with a number of pathnames, and the loop in the script loops over these.
              – Kusalananda
              Nov 28 at 15:44











            • @sudodus In your example with bash, the bash shell will be invoked once for each file. This is slow if there are many files.
              – Kusalananda
              Nov 28 at 15:45










            • Thanks. But one more question. I have learned that a shellscript will use $1 etc as parameters. How come you can use pathname for this purpose? Will the first available variable be used in these cases?
              – sudodus
              Nov 28 at 15:52













            up vote
            1
            down vote










            up vote
            1
            down vote









            You require a tool that can delete the .hhs filename suffix from the filename. find can not do this for you, so you will have to call another utility through -exec:



            find "$HOME" -type f -name '*.hhs' -exec sh -c '
            for pathname do
            cp -i "$pathname" "$pathname%.hhs"
            done' sh +


            I'm not using basename here as that would also remove the directory path from the found pathnames (which could be inserted again with dirname, but it would yield messy code).



            Instead, I call an in-line shell script with as many found pathnames as possible, and let the script loop over these, copying the individual files. The new name is constructed using a standard parameter substitution that deletes the string .hhs from the end of the value of $pathname.



            Related:



            • Understanding the -exec option of `find`





            share|improve this answer














            You require a tool that can delete the .hhs filename suffix from the filename. find can not do this for you, so you will have to call another utility through -exec:



            find "$HOME" -type f -name '*.hhs' -exec sh -c '
            for pathname do
            cp -i "$pathname" "$pathname%.hhs"
            done' sh +


            I'm not using basename here as that would also remove the directory path from the found pathnames (which could be inserted again with dirname, but it would yield messy code).



            Instead, I call an in-line shell script with as many found pathnames as possible, and let the script loop over these, copying the individual files. The new name is constructed using a standard parameter substitution that deletes the string .hhs from the end of the value of $pathname.



            Related:



            • Understanding the -exec option of `find`






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 28 at 14:39

























            answered Nov 28 at 14:23









            Kusalananda

            118k16223364




            118k16223364











            • @JeffSchaller Noted.
              – Kusalananda
              Nov 28 at 14:40










            • +1 Great method, @Kusalananda :-) Please explain how the shellscript works in this case: how is pathname given its value? What is the difference compared to find . -name "*.pdf" -exec bash -c 'echo cp -p "$1" "$1%%.pdf"' bash ; which works for me (I tested locally and for pdf files).
              – sudodus
              Nov 28 at 15:14










            • @sudodus I hoped to avoid that explanation by linking to a more expressive general answer regarding the various uses of -exec with find. But in general, find passes the found pathnames to the utility that you call from -exec, replacing with the pathname that it has found. With -exec ... +, find will replace with not one pathname, but with as many as possible. This means that the script that I have is called with a number of pathnames, and the loop in the script loops over these.
              – Kusalananda
              Nov 28 at 15:44











            • @sudodus In your example with bash, the bash shell will be invoked once for each file. This is slow if there are many files.
              – Kusalananda
              Nov 28 at 15:45










            • Thanks. But one more question. I have learned that a shellscript will use $1 etc as parameters. How come you can use pathname for this purpose? Will the first available variable be used in these cases?
              – sudodus
              Nov 28 at 15:52

















            • @JeffSchaller Noted.
              – Kusalananda
              Nov 28 at 14:40










            • +1 Great method, @Kusalananda :-) Please explain how the shellscript works in this case: how is pathname given its value? What is the difference compared to find . -name "*.pdf" -exec bash -c 'echo cp -p "$1" "$1%%.pdf"' bash ; which works for me (I tested locally and for pdf files).
              – sudodus
              Nov 28 at 15:14










            • @sudodus I hoped to avoid that explanation by linking to a more expressive general answer regarding the various uses of -exec with find. But in general, find passes the found pathnames to the utility that you call from -exec, replacing with the pathname that it has found. With -exec ... +, find will replace with not one pathname, but with as many as possible. This means that the script that I have is called with a number of pathnames, and the loop in the script loops over these.
              – Kusalananda
              Nov 28 at 15:44











            • @sudodus In your example with bash, the bash shell will be invoked once for each file. This is slow if there are many files.
              – Kusalananda
              Nov 28 at 15:45










            • Thanks. But one more question. I have learned that a shellscript will use $1 etc as parameters. How come you can use pathname for this purpose? Will the first available variable be used in these cases?
              – sudodus
              Nov 28 at 15:52
















            @JeffSchaller Noted.
            – Kusalananda
            Nov 28 at 14:40




            @JeffSchaller Noted.
            – Kusalananda
            Nov 28 at 14:40












            +1 Great method, @Kusalananda :-) Please explain how the shellscript works in this case: how is pathname given its value? What is the difference compared to find . -name "*.pdf" -exec bash -c 'echo cp -p "$1" "$1%%.pdf"' bash ; which works for me (I tested locally and for pdf files).
            – sudodus
            Nov 28 at 15:14




            +1 Great method, @Kusalananda :-) Please explain how the shellscript works in this case: how is pathname given its value? What is the difference compared to find . -name "*.pdf" -exec bash -c 'echo cp -p "$1" "$1%%.pdf"' bash ; which works for me (I tested locally and for pdf files).
            – sudodus
            Nov 28 at 15:14












            @sudodus I hoped to avoid that explanation by linking to a more expressive general answer regarding the various uses of -exec with find. But in general, find passes the found pathnames to the utility that you call from -exec, replacing with the pathname that it has found. With -exec ... +, find will replace with not one pathname, but with as many as possible. This means that the script that I have is called with a number of pathnames, and the loop in the script loops over these.
            – Kusalananda
            Nov 28 at 15:44





            @sudodus I hoped to avoid that explanation by linking to a more expressive general answer regarding the various uses of -exec with find. But in general, find passes the found pathnames to the utility that you call from -exec, replacing with the pathname that it has found. With -exec ... +, find will replace with not one pathname, but with as many as possible. This means that the script that I have is called with a number of pathnames, and the loop in the script loops over these.
            – Kusalananda
            Nov 28 at 15:44













            @sudodus In your example with bash, the bash shell will be invoked once for each file. This is slow if there are many files.
            – Kusalananda
            Nov 28 at 15:45




            @sudodus In your example with bash, the bash shell will be invoked once for each file. This is slow if there are many files.
            – Kusalananda
            Nov 28 at 15:45












            Thanks. But one more question. I have learned that a shellscript will use $1 etc as parameters. How come you can use pathname for this purpose? Will the first available variable be used in these cases?
            – sudodus
            Nov 28 at 15:52





            Thanks. But one more question. I have learned that a shellscript will use $1 etc as parameters. How come you can use pathname for this purpose? Will the first available variable be used in these cases?
            – sudodus
            Nov 28 at 15:52


















            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%2f484674%2frename-files-to-same-filename-without-extension-with-find-command%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

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay