Using linux command line, what is the easiest way to delete matching files in all subdirectories of other...

My mom helped me cosign a car and now she wants to take it

Is it beneficial to use a crop sensor camera with a full frame telezoom?

What are the children of two Muggle-borns called?

Does "boire un jus" tend to mean "coffee" or "juice of fruit"?

Why should I allow multiple IP addresses on a website for a single session?

Could you fall off a planet if it was being accelerated by engines?

What is my external HDD doing?

Processes in a session in an interactive shell vs in a script

Why isn't UDP with reliability (implemented at Application layer) a substitute of TCP?

How to count the number of bytes in a file, grouping the same bytes?

Russian equivalents of 能骗就骗 (if you can cheat, then cheat)

Is it theoretically possible to hack printer using scanner tray?

Is it advisable to inform the CEO about his brother accessing his office?

Why do movie directors use brown tint on Mexico cities?

Where to connect the fuse and why?

How do I present a future free of gender stereotypes without being jarring or overpowering the narrative?

How do I tell my girlfriend she's been buying me books by the wrong author for the last nine months?

Is it OK to say "The situation is pregnant with a crisis"?

Finding MacCready

How can this fractal shape perfectly cover a certain platonic solid?

Understanding the as-if rule, "the program was executed as written"

Checkmate in 1 on a Tangled Board

Have any large aeroplanes been landed — safely and without damage — in locations that they could not be flown away from?

iMac 2019: Can I mix the old modules with the new ones when upgrading RAM?



Using linux command line, what is the easiest way to delete matching files in all subdirectories of other extensions?


How to remove all the files in a directory?Delete all but largest file in multiple directoriesHow do you get the first file in each subdirectory matching a list of file extensions?count lines matching string in each subdirectory and their subdirectoriesHow to find out two files and remove the common entries from second fileHow to Delete All the Files in the Current Directory Except the Last TwoHow can I run script for all directories?How to join text files based on a column and removing the first line in linux?how to substitute strings in a set of files with different strings?compare a certain number of lines between columns of two files













0















I've been browsing around and trying to find the answer - bonus points for doing this in the shortest number of characters.



So I have file1.ex1 file2.ex1 file1.ex2 but no file2.ex2



I want to delete all the ex1 files, but only if ex2 version exists first.



So I assume this means I need to get a directory listing of all files ending in the extension I want to keep, piping that through and searching for which ones match the first part of the filename, then piping that into a delete command.



I want to use standard linux fu as I'm doing this on unraid.



I know this is easy for most of you, which is why I added to do it in least number of characters for a challenge! Oh, and this should work in all subdirectories matching to whatever is in that subdirectory only.



Thanks!



Marshalleq










share|improve this question







New contributor



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
























    0















    I've been browsing around and trying to find the answer - bonus points for doing this in the shortest number of characters.



    So I have file1.ex1 file2.ex1 file1.ex2 but no file2.ex2



    I want to delete all the ex1 files, but only if ex2 version exists first.



    So I assume this means I need to get a directory listing of all files ending in the extension I want to keep, piping that through and searching for which ones match the first part of the filename, then piping that into a delete command.



    I want to use standard linux fu as I'm doing this on unraid.



    I know this is easy for most of you, which is why I added to do it in least number of characters for a challenge! Oh, and this should work in all subdirectories matching to whatever is in that subdirectory only.



    Thanks!



    Marshalleq










    share|improve this question







    New contributor



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






















      0












      0








      0








      I've been browsing around and trying to find the answer - bonus points for doing this in the shortest number of characters.



      So I have file1.ex1 file2.ex1 file1.ex2 but no file2.ex2



      I want to delete all the ex1 files, but only if ex2 version exists first.



      So I assume this means I need to get a directory listing of all files ending in the extension I want to keep, piping that through and searching for which ones match the first part of the filename, then piping that into a delete command.



      I want to use standard linux fu as I'm doing this on unraid.



      I know this is easy for most of you, which is why I added to do it in least number of characters for a challenge! Oh, and this should work in all subdirectories matching to whatever is in that subdirectory only.



      Thanks!



      Marshalleq










      share|improve this question







      New contributor



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











      I've been browsing around and trying to find the answer - bonus points for doing this in the shortest number of characters.



      So I have file1.ex1 file2.ex1 file1.ex2 but no file2.ex2



      I want to delete all the ex1 files, but only if ex2 version exists first.



      So I assume this means I need to get a directory listing of all files ending in the extension I want to keep, piping that through and searching for which ones match the first part of the filename, then piping that into a delete command.



      I want to use standard linux fu as I'm doing this on unraid.



      I know this is easy for most of you, which is why I added to do it in least number of characters for a challenge! Oh, and this should work in all subdirectories matching to whatever is in that subdirectory only.



      Thanks!



      Marshalleq







      linux bash shell-script






      share|improve this question







      New contributor



      Marshalleq 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



      Marshalleq 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






      New contributor



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








      asked 1 hour ago









      MarshalleqMarshalleq

      11 bronze badge




      11 bronze badge




      New contributor



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




      New contributor




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
























          1 Answer
          1






          active

          oldest

          votes


















          1














          find . -name "*.ex1" -type f -exec sh -c '[ -f "${1%1}2" ] && echo rm "$1"' sh {} ;


          This finds all regular files with suffix .ex1 recursively in the current directory and executes the shell script inside
          of the single quotes for each file ($1) found. It only prints the rm command it would execute.





          • [ -f "${1%1}2" ] && tests if argument $1 exists with 2 as last character.
            ${1%1} removes the last 1 and we replace it with 2. The [ -f ... ] && tests if the file exists and executes the following command if the condition is true.


          You can remove the echo and run the command again to really delete the files.



          If you want to delete the files only in the current directory and not its subdirectories:



          for i in *.ex1; do [ -f "${i%1}2" ] && echo rm "$i"; done


          Again, you have to remove the echo to execute rm.






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


            }
            });






            Marshalleq 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%2f527688%2fusing-linux-command-line-what-is-the-easiest-way-to-delete-matching-files-in-al%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            find . -name "*.ex1" -type f -exec sh -c '[ -f "${1%1}2" ] && echo rm "$1"' sh {} ;


            This finds all regular files with suffix .ex1 recursively in the current directory and executes the shell script inside
            of the single quotes for each file ($1) found. It only prints the rm command it would execute.





            • [ -f "${1%1}2" ] && tests if argument $1 exists with 2 as last character.
              ${1%1} removes the last 1 and we replace it with 2. The [ -f ... ] && tests if the file exists and executes the following command if the condition is true.


            You can remove the echo and run the command again to really delete the files.



            If you want to delete the files only in the current directory and not its subdirectories:



            for i in *.ex1; do [ -f "${i%1}2" ] && echo rm "$i"; done


            Again, you have to remove the echo to execute rm.






            share|improve this answer




























              1














              find . -name "*.ex1" -type f -exec sh -c '[ -f "${1%1}2" ] && echo rm "$1"' sh {} ;


              This finds all regular files with suffix .ex1 recursively in the current directory and executes the shell script inside
              of the single quotes for each file ($1) found. It only prints the rm command it would execute.





              • [ -f "${1%1}2" ] && tests if argument $1 exists with 2 as last character.
                ${1%1} removes the last 1 and we replace it with 2. The [ -f ... ] && tests if the file exists and executes the following command if the condition is true.


              You can remove the echo and run the command again to really delete the files.



              If you want to delete the files only in the current directory and not its subdirectories:



              for i in *.ex1; do [ -f "${i%1}2" ] && echo rm "$i"; done


              Again, you have to remove the echo to execute rm.






              share|improve this answer


























                1












                1








                1







                find . -name "*.ex1" -type f -exec sh -c '[ -f "${1%1}2" ] && echo rm "$1"' sh {} ;


                This finds all regular files with suffix .ex1 recursively in the current directory and executes the shell script inside
                of the single quotes for each file ($1) found. It only prints the rm command it would execute.





                • [ -f "${1%1}2" ] && tests if argument $1 exists with 2 as last character.
                  ${1%1} removes the last 1 and we replace it with 2. The [ -f ... ] && tests if the file exists and executes the following command if the condition is true.


                You can remove the echo and run the command again to really delete the files.



                If you want to delete the files only in the current directory and not its subdirectories:



                for i in *.ex1; do [ -f "${i%1}2" ] && echo rm "$i"; done


                Again, you have to remove the echo to execute rm.






                share|improve this answer













                find . -name "*.ex1" -type f -exec sh -c '[ -f "${1%1}2" ] && echo rm "$1"' sh {} ;


                This finds all regular files with suffix .ex1 recursively in the current directory and executes the shell script inside
                of the single quotes for each file ($1) found. It only prints the rm command it would execute.





                • [ -f "${1%1}2" ] && tests if argument $1 exists with 2 as last character.
                  ${1%1} removes the last 1 and we replace it with 2. The [ -f ... ] && tests if the file exists and executes the following command if the condition is true.


                You can remove the echo and run the command again to really delete the files.



                If you want to delete the files only in the current directory and not its subdirectories:



                for i in *.ex1; do [ -f "${i%1}2" ] && echo rm "$i"; done


                Again, you have to remove the echo to execute rm.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 20 mins ago









                FreddyFreddy

                5,4511 gold badge6 silver badges23 bronze badges




                5,4511 gold badge6 silver badges23 bronze badges






















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










                    draft saved

                    draft discarded


















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













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












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




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f527688%2fusing-linux-command-line-what-is-the-easiest-way-to-delete-matching-files-in-al%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

                    Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

                    Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

                    Nicolae Petrescu-Găină Cuprins Biografie | Opera | In memoriam | Varia | Controverse, incertitudini...