delete files matching pattern











up vote
29
down vote

favorite
9












I need to recursively remove all files in all subdirs where the filename contains a number followed by an 'x' followed by a number, at least two times.



Example:



I'd want to remove these files:



'aaa-12x123-123x12.jpg'
'aaa-12x12-123x12-12x123.jpg'


But I do NOT want to remove these files:



'aaa.jpg'
'aaa-12x12.jpg'
'aaaxaaa-123x123.jpg'
'aaaxaaa-aaaxaaa.jpg'


How can I do that (from the bash shell)










share|improve this question




























    up vote
    29
    down vote

    favorite
    9












    I need to recursively remove all files in all subdirs where the filename contains a number followed by an 'x' followed by a number, at least two times.



    Example:



    I'd want to remove these files:



    'aaa-12x123-123x12.jpg'
    'aaa-12x12-123x12-12x123.jpg'


    But I do NOT want to remove these files:



    'aaa.jpg'
    'aaa-12x12.jpg'
    'aaaxaaa-123x123.jpg'
    'aaaxaaa-aaaxaaa.jpg'


    How can I do that (from the bash shell)










    share|improve this question


























      up vote
      29
      down vote

      favorite
      9









      up vote
      29
      down vote

      favorite
      9






      9





      I need to recursively remove all files in all subdirs where the filename contains a number followed by an 'x' followed by a number, at least two times.



      Example:



      I'd want to remove these files:



      'aaa-12x123-123x12.jpg'
      'aaa-12x12-123x12-12x123.jpg'


      But I do NOT want to remove these files:



      'aaa.jpg'
      'aaa-12x12.jpg'
      'aaaxaaa-123x123.jpg'
      'aaaxaaa-aaaxaaa.jpg'


      How can I do that (from the bash shell)










      share|improve this question















      I need to recursively remove all files in all subdirs where the filename contains a number followed by an 'x' followed by a number, at least two times.



      Example:



      I'd want to remove these files:



      'aaa-12x123-123x12.jpg'
      'aaa-12x12-123x12-12x123.jpg'


      But I do NOT want to remove these files:



      'aaa.jpg'
      'aaa-12x12.jpg'
      'aaaxaaa-123x123.jpg'
      'aaaxaaa-aaaxaaa.jpg'


      How can I do that (from the bash shell)







      bash files find wildcards






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 6 '14 at 17:51









      derobert

      71.5k8152210




      71.5k8152210










      asked Jul 30 '13 at 0:00









      mikkelbreum

      263136




      263136






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          59
          down vote



          accepted










          A string contains “a number followed by an x followed by a number” if and only if it contains a digit followed by an x followed by a digit, i.e. if it contains a substring matching the pattern [0-9]x[0-9]. So you're looking to remove the files whose name matches the pattern *[0-9]x[0-9]*[0-9]x[0-9]*.jpg.



          find /path/to/directory -type f -name '*[0-9]x[0-9]*[0-9]x[0-9]*.jpg' -delete


          If your find doesn't have -delete, call rm to delete the files.



          find /path/to/directory -type f -name '*[0-9]x[0-9]*[0-9]x[0-9]*.jpg' -exec rm {} +





          share|improve this answer

















          • 2




            Thank you! 12.000 files gone i 2 sec. That saved me some manual labour!
            – mikkelbreum
            Jul 30 '13 at 13:45










          • Neither -delete nor -exec rm worked for me in Bash on Windows. But this did: find /path/to/directory -type f -name '*[0-9]x[0-9]*[0-9]x[0-9]*.jpg' | xargs rm
            – Tamlyn
            Aug 13 '15 at 8:30






          • 2




            @Tamlyn Use -print0 and xargs -0, otherwise the command will fail with file names containing spaces or single quotes. But -delete and -exec rm do work on Windows. If something doesn't work, it's not due to their use.
            – Gilles
            Aug 13 '15 at 9:50










          • Will this work with Windows as well?
            – CodyBugstein
            Aug 25 '15 at 4:07










          • It'll work if you have a port of Unix utilities such as Cygwin or GNUWin32. Obviously it won't work out of the box on Windows. Take care that Windows has an unrelated program called find, so make sure the Unix utilities are first in PATH.
            – Gilles
            Aug 25 '15 at 8:33




















          up vote
          0
          down vote













          The right command is:



          find . -type f -iregex '.*[0-9]x[0-9]*.jpg$'



          this will grab only files with names: 'aaa-12x12.jpg', but not 'aaa-12x12red.jpg'






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


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f84852%2fdelete-files-matching-pattern%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
            59
            down vote



            accepted










            A string contains “a number followed by an x followed by a number” if and only if it contains a digit followed by an x followed by a digit, i.e. if it contains a substring matching the pattern [0-9]x[0-9]. So you're looking to remove the files whose name matches the pattern *[0-9]x[0-9]*[0-9]x[0-9]*.jpg.



            find /path/to/directory -type f -name '*[0-9]x[0-9]*[0-9]x[0-9]*.jpg' -delete


            If your find doesn't have -delete, call rm to delete the files.



            find /path/to/directory -type f -name '*[0-9]x[0-9]*[0-9]x[0-9]*.jpg' -exec rm {} +





            share|improve this answer

















            • 2




              Thank you! 12.000 files gone i 2 sec. That saved me some manual labour!
              – mikkelbreum
              Jul 30 '13 at 13:45










            • Neither -delete nor -exec rm worked for me in Bash on Windows. But this did: find /path/to/directory -type f -name '*[0-9]x[0-9]*[0-9]x[0-9]*.jpg' | xargs rm
              – Tamlyn
              Aug 13 '15 at 8:30






            • 2




              @Tamlyn Use -print0 and xargs -0, otherwise the command will fail with file names containing spaces or single quotes. But -delete and -exec rm do work on Windows. If something doesn't work, it's not due to their use.
              – Gilles
              Aug 13 '15 at 9:50










            • Will this work with Windows as well?
              – CodyBugstein
              Aug 25 '15 at 4:07










            • It'll work if you have a port of Unix utilities such as Cygwin or GNUWin32. Obviously it won't work out of the box on Windows. Take care that Windows has an unrelated program called find, so make sure the Unix utilities are first in PATH.
              – Gilles
              Aug 25 '15 at 8:33

















            up vote
            59
            down vote



            accepted










            A string contains “a number followed by an x followed by a number” if and only if it contains a digit followed by an x followed by a digit, i.e. if it contains a substring matching the pattern [0-9]x[0-9]. So you're looking to remove the files whose name matches the pattern *[0-9]x[0-9]*[0-9]x[0-9]*.jpg.



            find /path/to/directory -type f -name '*[0-9]x[0-9]*[0-9]x[0-9]*.jpg' -delete


            If your find doesn't have -delete, call rm to delete the files.



            find /path/to/directory -type f -name '*[0-9]x[0-9]*[0-9]x[0-9]*.jpg' -exec rm {} +





            share|improve this answer

















            • 2




              Thank you! 12.000 files gone i 2 sec. That saved me some manual labour!
              – mikkelbreum
              Jul 30 '13 at 13:45










            • Neither -delete nor -exec rm worked for me in Bash on Windows. But this did: find /path/to/directory -type f -name '*[0-9]x[0-9]*[0-9]x[0-9]*.jpg' | xargs rm
              – Tamlyn
              Aug 13 '15 at 8:30






            • 2




              @Tamlyn Use -print0 and xargs -0, otherwise the command will fail with file names containing spaces or single quotes. But -delete and -exec rm do work on Windows. If something doesn't work, it's not due to their use.
              – Gilles
              Aug 13 '15 at 9:50










            • Will this work with Windows as well?
              – CodyBugstein
              Aug 25 '15 at 4:07










            • It'll work if you have a port of Unix utilities such as Cygwin or GNUWin32. Obviously it won't work out of the box on Windows. Take care that Windows has an unrelated program called find, so make sure the Unix utilities are first in PATH.
              – Gilles
              Aug 25 '15 at 8:33















            up vote
            59
            down vote



            accepted







            up vote
            59
            down vote



            accepted






            A string contains “a number followed by an x followed by a number” if and only if it contains a digit followed by an x followed by a digit, i.e. if it contains a substring matching the pattern [0-9]x[0-9]. So you're looking to remove the files whose name matches the pattern *[0-9]x[0-9]*[0-9]x[0-9]*.jpg.



            find /path/to/directory -type f -name '*[0-9]x[0-9]*[0-9]x[0-9]*.jpg' -delete


            If your find doesn't have -delete, call rm to delete the files.



            find /path/to/directory -type f -name '*[0-9]x[0-9]*[0-9]x[0-9]*.jpg' -exec rm {} +





            share|improve this answer












            A string contains “a number followed by an x followed by a number” if and only if it contains a digit followed by an x followed by a digit, i.e. if it contains a substring matching the pattern [0-9]x[0-9]. So you're looking to remove the files whose name matches the pattern *[0-9]x[0-9]*[0-9]x[0-9]*.jpg.



            find /path/to/directory -type f -name '*[0-9]x[0-9]*[0-9]x[0-9]*.jpg' -delete


            If your find doesn't have -delete, call rm to delete the files.



            find /path/to/directory -type f -name '*[0-9]x[0-9]*[0-9]x[0-9]*.jpg' -exec rm {} +






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jul 30 '13 at 0:07









            Gilles

            525k12710511578




            525k12710511578








            • 2




              Thank you! 12.000 files gone i 2 sec. That saved me some manual labour!
              – mikkelbreum
              Jul 30 '13 at 13:45










            • Neither -delete nor -exec rm worked for me in Bash on Windows. But this did: find /path/to/directory -type f -name '*[0-9]x[0-9]*[0-9]x[0-9]*.jpg' | xargs rm
              – Tamlyn
              Aug 13 '15 at 8:30






            • 2




              @Tamlyn Use -print0 and xargs -0, otherwise the command will fail with file names containing spaces or single quotes. But -delete and -exec rm do work on Windows. If something doesn't work, it's not due to their use.
              – Gilles
              Aug 13 '15 at 9:50










            • Will this work with Windows as well?
              – CodyBugstein
              Aug 25 '15 at 4:07










            • It'll work if you have a port of Unix utilities such as Cygwin or GNUWin32. Obviously it won't work out of the box on Windows. Take care that Windows has an unrelated program called find, so make sure the Unix utilities are first in PATH.
              – Gilles
              Aug 25 '15 at 8:33
















            • 2




              Thank you! 12.000 files gone i 2 sec. That saved me some manual labour!
              – mikkelbreum
              Jul 30 '13 at 13:45










            • Neither -delete nor -exec rm worked for me in Bash on Windows. But this did: find /path/to/directory -type f -name '*[0-9]x[0-9]*[0-9]x[0-9]*.jpg' | xargs rm
              – Tamlyn
              Aug 13 '15 at 8:30






            • 2




              @Tamlyn Use -print0 and xargs -0, otherwise the command will fail with file names containing spaces or single quotes. But -delete and -exec rm do work on Windows. If something doesn't work, it's not due to their use.
              – Gilles
              Aug 13 '15 at 9:50










            • Will this work with Windows as well?
              – CodyBugstein
              Aug 25 '15 at 4:07










            • It'll work if you have a port of Unix utilities such as Cygwin or GNUWin32. Obviously it won't work out of the box on Windows. Take care that Windows has an unrelated program called find, so make sure the Unix utilities are first in PATH.
              – Gilles
              Aug 25 '15 at 8:33










            2




            2




            Thank you! 12.000 files gone i 2 sec. That saved me some manual labour!
            – mikkelbreum
            Jul 30 '13 at 13:45




            Thank you! 12.000 files gone i 2 sec. That saved me some manual labour!
            – mikkelbreum
            Jul 30 '13 at 13:45












            Neither -delete nor -exec rm worked for me in Bash on Windows. But this did: find /path/to/directory -type f -name '*[0-9]x[0-9]*[0-9]x[0-9]*.jpg' | xargs rm
            – Tamlyn
            Aug 13 '15 at 8:30




            Neither -delete nor -exec rm worked for me in Bash on Windows. But this did: find /path/to/directory -type f -name '*[0-9]x[0-9]*[0-9]x[0-9]*.jpg' | xargs rm
            – Tamlyn
            Aug 13 '15 at 8:30




            2




            2




            @Tamlyn Use -print0 and xargs -0, otherwise the command will fail with file names containing spaces or single quotes. But -delete and -exec rm do work on Windows. If something doesn't work, it's not due to their use.
            – Gilles
            Aug 13 '15 at 9:50




            @Tamlyn Use -print0 and xargs -0, otherwise the command will fail with file names containing spaces or single quotes. But -delete and -exec rm do work on Windows. If something doesn't work, it's not due to their use.
            – Gilles
            Aug 13 '15 at 9:50












            Will this work with Windows as well?
            – CodyBugstein
            Aug 25 '15 at 4:07




            Will this work with Windows as well?
            – CodyBugstein
            Aug 25 '15 at 4:07












            It'll work if you have a port of Unix utilities such as Cygwin or GNUWin32. Obviously it won't work out of the box on Windows. Take care that Windows has an unrelated program called find, so make sure the Unix utilities are first in PATH.
            – Gilles
            Aug 25 '15 at 8:33






            It'll work if you have a port of Unix utilities such as Cygwin or GNUWin32. Obviously it won't work out of the box on Windows. Take care that Windows has an unrelated program called find, so make sure the Unix utilities are first in PATH.
            – Gilles
            Aug 25 '15 at 8:33














            up vote
            0
            down vote













            The right command is:



            find . -type f -iregex '.*[0-9]x[0-9]*.jpg$'



            this will grab only files with names: 'aaa-12x12.jpg', but not 'aaa-12x12red.jpg'






            share|improve this answer



























              up vote
              0
              down vote













              The right command is:



              find . -type f -iregex '.*[0-9]x[0-9]*.jpg$'



              this will grab only files with names: 'aaa-12x12.jpg', but not 'aaa-12x12red.jpg'






              share|improve this answer

























                up vote
                0
                down vote










                up vote
                0
                down vote









                The right command is:



                find . -type f -iregex '.*[0-9]x[0-9]*.jpg$'



                this will grab only files with names: 'aaa-12x12.jpg', but not 'aaa-12x12red.jpg'






                share|improve this answer














                The right command is:



                find . -type f -iregex '.*[0-9]x[0-9]*.jpg$'



                this will grab only files with names: 'aaa-12x12.jpg', but not 'aaa-12x12red.jpg'







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Dec 4 at 13:21

























                answered Dec 4 at 13:01









                MasterR

                11




                11






























                    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%2f84852%2fdelete-files-matching-pattern%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

                    List directoties down one level, excluding some named directories and files

                    list processes belonging to a network namespace

                    list systemd RuntimeDirectory mounts