“relative path potentially not safe” error with “find … -delete” on macOS












3















I'm trying to delete all files containing a certain text like this:



$ find ~/Library/MobileDevice/Provisioning Profiles/* -exec grep -l "text to search for" '{}' ; -delete
/Users/build/Library/MobileDevice/Provisioning Profiles/06060826-3fb2-4d71-82c6-7b9d309b08d6.mobileprovision
find: -delete: /Users/build/Library/MobileDevice/Provisioning Profiles/06060826-3fb2-4d71-82c6-7b9d309b08d6.mobileprovision: relative path potentially not safe


However, as you can see, it's throwing a warning and then does not delete the file. How can I resolve this error?



This is on a Mac.










share|improve this question





























    3















    I'm trying to delete all files containing a certain text like this:



    $ find ~/Library/MobileDevice/Provisioning Profiles/* -exec grep -l "text to search for" '{}' ; -delete
    /Users/build/Library/MobileDevice/Provisioning Profiles/06060826-3fb2-4d71-82c6-7b9d309b08d6.mobileprovision
    find: -delete: /Users/build/Library/MobileDevice/Provisioning Profiles/06060826-3fb2-4d71-82c6-7b9d309b08d6.mobileprovision: relative path potentially not safe


    However, as you can see, it's throwing a warning and then does not delete the file. How can I resolve this error?



    This is on a Mac.










    share|improve this question



























      3












      3








      3








      I'm trying to delete all files containing a certain text like this:



      $ find ~/Library/MobileDevice/Provisioning Profiles/* -exec grep -l "text to search for" '{}' ; -delete
      /Users/build/Library/MobileDevice/Provisioning Profiles/06060826-3fb2-4d71-82c6-7b9d309b08d6.mobileprovision
      find: -delete: /Users/build/Library/MobileDevice/Provisioning Profiles/06060826-3fb2-4d71-82c6-7b9d309b08d6.mobileprovision: relative path potentially not safe


      However, as you can see, it's throwing a warning and then does not delete the file. How can I resolve this error?



      This is on a Mac.










      share|improve this question
















      I'm trying to delete all files containing a certain text like this:



      $ find ~/Library/MobileDevice/Provisioning Profiles/* -exec grep -l "text to search for" '{}' ; -delete
      /Users/build/Library/MobileDevice/Provisioning Profiles/06060826-3fb2-4d71-82c6-7b9d309b08d6.mobileprovision
      find: -delete: /Users/build/Library/MobileDevice/Provisioning Profiles/06060826-3fb2-4d71-82c6-7b9d309b08d6.mobileprovision: relative path potentially not safe


      However, as you can see, it's throwing a warning and then does not delete the file. How can I resolve this error?



      This is on a Mac.







      find osx






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited yesterday









      Rui F Ribeiro

      39.8k1479133




      39.8k1479133










      asked yesterday









      Bart van den BurgBart van den Burg

      14115




      14115






















          3 Answers
          3






          active

          oldest

          votes


















          8














          macOS find is based on an older version of FreeBSD find whose -delete would not remove the files that were given as argument.



          When you do:



          find dir/* ... -delete


          Your shell is expanding that dir/* glob into a list of file paths (excluding the hidden ones, while find itself will not exclude the hidden files it finds in any of those dirs), so find receives something like:



          find dir/dir1 dir/dir2 dir/file1 dir/file2... ... -delete


          If dir/file1 matches macOS find's -delete will refuse to delete it. It will happily delete a dir/dir1/.somefile if it matches though.



          That was changed in FreeBSD in 2013, but the change apparently didn't make it to macOS. Here, the work around is easy: use find dir (or find dir/ if you want to allow for dir to be a symlink to a directory and find to descend into it) instead of find dir/*. So, in your case:



          find ~/Library/MobileDevice/Provisioning Profiles/ 
          -exec grep -l "text to search for" '{}' ; -delete


          Or use the more efficient grep -l --null | xargs -0 approach.






          share|improve this answer

































            1














            Try:
            find ~/Library/MobileDevice/Provisioning Profiles/ -type f -name "name to match" -delete



            Edit - first code is not abswer for question, try this:
            find ~/Library/MobileDevice/Provisioning Profiles/ -type f -exec grep -l --null "pattern in file" {} + | xargs -0 rm






            share|improve this answer










            New contributor




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
















            • 1





              This is searching for a filename pattern. OP wants to find files which contain a certain string/text.

              – Michael D.
              yesterday











            • @MichaelD. Oh, you are right, my bad.

              – Matej
              yesterday











            • You can't use xargs in general on the output of find, but especially here, it won't work as the paths of the files contain spaces (all whitespace, quotes backslashes are a problem with find | xargs)

              – Stéphane Chazelas
              yesterday








            • 1





              No, I meant -exec grep -q pattern {} ; -print0 | xargs -0 rm. macOS grep should also support GNU's --null option so -exec grep -l --null pattern {} + | xargs -0 rm should also work (and avoid running one grep per file).

              – Stéphane Chazelas
              yesterday








            • 1





              Yes. It's correct (there are some race condition issues linked to using xargs rm instead of -delete, but probably not worth working around here as we're presumably not removing files from a directory under control of potentially malicious actors).

              – Stéphane Chazelas
              yesterday



















            0














            This is searching for string err in all files in /var/log/ recursive.



            I'm not sure if cut, uniq and xargs is available on Mac and this is not using find.



            grep -r "err" /var/log/ | cut -d: -f1 | uniq | xargs echo


            Replace echo with rm to delete the files.






            share|improve this answer
























            • 'find', cut, uniq and xargs are standard Unix utilities available on any Unix platform.

              – Kusalananda
              yesterday













            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "106"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f497666%2frelative-path-potentially-not-safe-error-with-find-delete-on-macos%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









            8














            macOS find is based on an older version of FreeBSD find whose -delete would not remove the files that were given as argument.



            When you do:



            find dir/* ... -delete


            Your shell is expanding that dir/* glob into a list of file paths (excluding the hidden ones, while find itself will not exclude the hidden files it finds in any of those dirs), so find receives something like:



            find dir/dir1 dir/dir2 dir/file1 dir/file2... ... -delete


            If dir/file1 matches macOS find's -delete will refuse to delete it. It will happily delete a dir/dir1/.somefile if it matches though.



            That was changed in FreeBSD in 2013, but the change apparently didn't make it to macOS. Here, the work around is easy: use find dir (or find dir/ if you want to allow for dir to be a symlink to a directory and find to descend into it) instead of find dir/*. So, in your case:



            find ~/Library/MobileDevice/Provisioning Profiles/ 
            -exec grep -l "text to search for" '{}' ; -delete


            Or use the more efficient grep -l --null | xargs -0 approach.






            share|improve this answer






























              8














              macOS find is based on an older version of FreeBSD find whose -delete would not remove the files that were given as argument.



              When you do:



              find dir/* ... -delete


              Your shell is expanding that dir/* glob into a list of file paths (excluding the hidden ones, while find itself will not exclude the hidden files it finds in any of those dirs), so find receives something like:



              find dir/dir1 dir/dir2 dir/file1 dir/file2... ... -delete


              If dir/file1 matches macOS find's -delete will refuse to delete it. It will happily delete a dir/dir1/.somefile if it matches though.



              That was changed in FreeBSD in 2013, but the change apparently didn't make it to macOS. Here, the work around is easy: use find dir (or find dir/ if you want to allow for dir to be a symlink to a directory and find to descend into it) instead of find dir/*. So, in your case:



              find ~/Library/MobileDevice/Provisioning Profiles/ 
              -exec grep -l "text to search for" '{}' ; -delete


              Or use the more efficient grep -l --null | xargs -0 approach.






              share|improve this answer




























                8












                8








                8







                macOS find is based on an older version of FreeBSD find whose -delete would not remove the files that were given as argument.



                When you do:



                find dir/* ... -delete


                Your shell is expanding that dir/* glob into a list of file paths (excluding the hidden ones, while find itself will not exclude the hidden files it finds in any of those dirs), so find receives something like:



                find dir/dir1 dir/dir2 dir/file1 dir/file2... ... -delete


                If dir/file1 matches macOS find's -delete will refuse to delete it. It will happily delete a dir/dir1/.somefile if it matches though.



                That was changed in FreeBSD in 2013, but the change apparently didn't make it to macOS. Here, the work around is easy: use find dir (or find dir/ if you want to allow for dir to be a symlink to a directory and find to descend into it) instead of find dir/*. So, in your case:



                find ~/Library/MobileDevice/Provisioning Profiles/ 
                -exec grep -l "text to search for" '{}' ; -delete


                Or use the more efficient grep -l --null | xargs -0 approach.






                share|improve this answer















                macOS find is based on an older version of FreeBSD find whose -delete would not remove the files that were given as argument.



                When you do:



                find dir/* ... -delete


                Your shell is expanding that dir/* glob into a list of file paths (excluding the hidden ones, while find itself will not exclude the hidden files it finds in any of those dirs), so find receives something like:



                find dir/dir1 dir/dir2 dir/file1 dir/file2... ... -delete


                If dir/file1 matches macOS find's -delete will refuse to delete it. It will happily delete a dir/dir1/.somefile if it matches though.



                That was changed in FreeBSD in 2013, but the change apparently didn't make it to macOS. Here, the work around is easy: use find dir (or find dir/ if you want to allow for dir to be a symlink to a directory and find to descend into it) instead of find dir/*. So, in your case:



                find ~/Library/MobileDevice/Provisioning Profiles/ 
                -exec grep -l "text to search for" '{}' ; -delete


                Or use the more efficient grep -l --null | xargs -0 approach.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited yesterday

























                answered yesterday









                Stéphane ChazelasStéphane Chazelas

                303k57570926




                303k57570926

























                    1














                    Try:
                    find ~/Library/MobileDevice/Provisioning Profiles/ -type f -name "name to match" -delete



                    Edit - first code is not abswer for question, try this:
                    find ~/Library/MobileDevice/Provisioning Profiles/ -type f -exec grep -l --null "pattern in file" {} + | xargs -0 rm






                    share|improve this answer










                    New contributor




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
















                    • 1





                      This is searching for a filename pattern. OP wants to find files which contain a certain string/text.

                      – Michael D.
                      yesterday











                    • @MichaelD. Oh, you are right, my bad.

                      – Matej
                      yesterday











                    • You can't use xargs in general on the output of find, but especially here, it won't work as the paths of the files contain spaces (all whitespace, quotes backslashes are a problem with find | xargs)

                      – Stéphane Chazelas
                      yesterday








                    • 1





                      No, I meant -exec grep -q pattern {} ; -print0 | xargs -0 rm. macOS grep should also support GNU's --null option so -exec grep -l --null pattern {} + | xargs -0 rm should also work (and avoid running one grep per file).

                      – Stéphane Chazelas
                      yesterday








                    • 1





                      Yes. It's correct (there are some race condition issues linked to using xargs rm instead of -delete, but probably not worth working around here as we're presumably not removing files from a directory under control of potentially malicious actors).

                      – Stéphane Chazelas
                      yesterday
















                    1














                    Try:
                    find ~/Library/MobileDevice/Provisioning Profiles/ -type f -name "name to match" -delete



                    Edit - first code is not abswer for question, try this:
                    find ~/Library/MobileDevice/Provisioning Profiles/ -type f -exec grep -l --null "pattern in file" {} + | xargs -0 rm






                    share|improve this answer










                    New contributor




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
















                    • 1





                      This is searching for a filename pattern. OP wants to find files which contain a certain string/text.

                      – Michael D.
                      yesterday











                    • @MichaelD. Oh, you are right, my bad.

                      – Matej
                      yesterday











                    • You can't use xargs in general on the output of find, but especially here, it won't work as the paths of the files contain spaces (all whitespace, quotes backslashes are a problem with find | xargs)

                      – Stéphane Chazelas
                      yesterday








                    • 1





                      No, I meant -exec grep -q pattern {} ; -print0 | xargs -0 rm. macOS grep should also support GNU's --null option so -exec grep -l --null pattern {} + | xargs -0 rm should also work (and avoid running one grep per file).

                      – Stéphane Chazelas
                      yesterday








                    • 1





                      Yes. It's correct (there are some race condition issues linked to using xargs rm instead of -delete, but probably not worth working around here as we're presumably not removing files from a directory under control of potentially malicious actors).

                      – Stéphane Chazelas
                      yesterday














                    1












                    1








                    1







                    Try:
                    find ~/Library/MobileDevice/Provisioning Profiles/ -type f -name "name to match" -delete



                    Edit - first code is not abswer for question, try this:
                    find ~/Library/MobileDevice/Provisioning Profiles/ -type f -exec grep -l --null "pattern in file" {} + | xargs -0 rm






                    share|improve this answer










                    New contributor




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










                    Try:
                    find ~/Library/MobileDevice/Provisioning Profiles/ -type f -name "name to match" -delete



                    Edit - first code is not abswer for question, try this:
                    find ~/Library/MobileDevice/Provisioning Profiles/ -type f -exec grep -l --null "pattern in file" {} + | xargs -0 rm







                    share|improve this answer










                    New contributor




                    Matej 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 answer



                    share|improve this answer








                    edited yesterday





















                    New contributor




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









                    answered yesterday









                    MatejMatej

                    365




                    365




                    New contributor




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





                    New contributor





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






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








                    • 1





                      This is searching for a filename pattern. OP wants to find files which contain a certain string/text.

                      – Michael D.
                      yesterday











                    • @MichaelD. Oh, you are right, my bad.

                      – Matej
                      yesterday











                    • You can't use xargs in general on the output of find, but especially here, it won't work as the paths of the files contain spaces (all whitespace, quotes backslashes are a problem with find | xargs)

                      – Stéphane Chazelas
                      yesterday








                    • 1





                      No, I meant -exec grep -q pattern {} ; -print0 | xargs -0 rm. macOS grep should also support GNU's --null option so -exec grep -l --null pattern {} + | xargs -0 rm should also work (and avoid running one grep per file).

                      – Stéphane Chazelas
                      yesterday








                    • 1





                      Yes. It's correct (there are some race condition issues linked to using xargs rm instead of -delete, but probably not worth working around here as we're presumably not removing files from a directory under control of potentially malicious actors).

                      – Stéphane Chazelas
                      yesterday














                    • 1





                      This is searching for a filename pattern. OP wants to find files which contain a certain string/text.

                      – Michael D.
                      yesterday











                    • @MichaelD. Oh, you are right, my bad.

                      – Matej
                      yesterday











                    • You can't use xargs in general on the output of find, but especially here, it won't work as the paths of the files contain spaces (all whitespace, quotes backslashes are a problem with find | xargs)

                      – Stéphane Chazelas
                      yesterday








                    • 1





                      No, I meant -exec grep -q pattern {} ; -print0 | xargs -0 rm. macOS grep should also support GNU's --null option so -exec grep -l --null pattern {} + | xargs -0 rm should also work (and avoid running one grep per file).

                      – Stéphane Chazelas
                      yesterday








                    • 1





                      Yes. It's correct (there are some race condition issues linked to using xargs rm instead of -delete, but probably not worth working around here as we're presumably not removing files from a directory under control of potentially malicious actors).

                      – Stéphane Chazelas
                      yesterday








                    1




                    1





                    This is searching for a filename pattern. OP wants to find files which contain a certain string/text.

                    – Michael D.
                    yesterday





                    This is searching for a filename pattern. OP wants to find files which contain a certain string/text.

                    – Michael D.
                    yesterday













                    @MichaelD. Oh, you are right, my bad.

                    – Matej
                    yesterday





                    @MichaelD. Oh, you are right, my bad.

                    – Matej
                    yesterday













                    You can't use xargs in general on the output of find, but especially here, it won't work as the paths of the files contain spaces (all whitespace, quotes backslashes are a problem with find | xargs)

                    – Stéphane Chazelas
                    yesterday







                    You can't use xargs in general on the output of find, but especially here, it won't work as the paths of the files contain spaces (all whitespace, quotes backslashes are a problem with find | xargs)

                    – Stéphane Chazelas
                    yesterday






                    1




                    1





                    No, I meant -exec grep -q pattern {} ; -print0 | xargs -0 rm. macOS grep should also support GNU's --null option so -exec grep -l --null pattern {} + | xargs -0 rm should also work (and avoid running one grep per file).

                    – Stéphane Chazelas
                    yesterday







                    No, I meant -exec grep -q pattern {} ; -print0 | xargs -0 rm. macOS grep should also support GNU's --null option so -exec grep -l --null pattern {} + | xargs -0 rm should also work (and avoid running one grep per file).

                    – Stéphane Chazelas
                    yesterday






                    1




                    1





                    Yes. It's correct (there are some race condition issues linked to using xargs rm instead of -delete, but probably not worth working around here as we're presumably not removing files from a directory under control of potentially malicious actors).

                    – Stéphane Chazelas
                    yesterday





                    Yes. It's correct (there are some race condition issues linked to using xargs rm instead of -delete, but probably not worth working around here as we're presumably not removing files from a directory under control of potentially malicious actors).

                    – Stéphane Chazelas
                    yesterday











                    0














                    This is searching for string err in all files in /var/log/ recursive.



                    I'm not sure if cut, uniq and xargs is available on Mac and this is not using find.



                    grep -r "err" /var/log/ | cut -d: -f1 | uniq | xargs echo


                    Replace echo with rm to delete the files.






                    share|improve this answer
























                    • 'find', cut, uniq and xargs are standard Unix utilities available on any Unix platform.

                      – Kusalananda
                      yesterday


















                    0














                    This is searching for string err in all files in /var/log/ recursive.



                    I'm not sure if cut, uniq and xargs is available on Mac and this is not using find.



                    grep -r "err" /var/log/ | cut -d: -f1 | uniq | xargs echo


                    Replace echo with rm to delete the files.






                    share|improve this answer
























                    • 'find', cut, uniq and xargs are standard Unix utilities available on any Unix platform.

                      – Kusalananda
                      yesterday
















                    0












                    0








                    0







                    This is searching for string err in all files in /var/log/ recursive.



                    I'm not sure if cut, uniq and xargs is available on Mac and this is not using find.



                    grep -r "err" /var/log/ | cut -d: -f1 | uniq | xargs echo


                    Replace echo with rm to delete the files.






                    share|improve this answer













                    This is searching for string err in all files in /var/log/ recursive.



                    I'm not sure if cut, uniq and xargs is available on Mac and this is not using find.



                    grep -r "err" /var/log/ | cut -d: -f1 | uniq | xargs echo


                    Replace echo with rm to delete the files.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered yesterday









                    Michael D.Michael D.

                    1,577816




                    1,577816













                    • 'find', cut, uniq and xargs are standard Unix utilities available on any Unix platform.

                      – Kusalananda
                      yesterday





















                    • 'find', cut, uniq and xargs are standard Unix utilities available on any Unix platform.

                      – Kusalananda
                      yesterday



















                    'find', cut, uniq and xargs are standard Unix utilities available on any Unix platform.

                    – Kusalananda
                    yesterday







                    'find', cut, uniq and xargs are standard Unix utilities available on any Unix platform.

                    – Kusalananda
                    yesterday




















                    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.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f497666%2frelative-path-potentially-not-safe-error-with-find-delete-on-macos%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

                    "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                    Alcedinidae

                    RAC Tourist Trophy