How to delete broken symlinks in one go?












3















I have created symlinks to a large amount of logfiles. The syntax of the logfiles is yyyymmdd.log.gz.



To simplify things I use a simple sequence without parsing it with date:



for dd in $(seq -w 20150101 20151231) ; do 
ln -s $origin/$dd.log.gz $target/$dd.log.gz
done


How do I get rid of all the broken symlinks I just created in a single fell swoop?










share|improve this question





























    3















    I have created symlinks to a large amount of logfiles. The syntax of the logfiles is yyyymmdd.log.gz.



    To simplify things I use a simple sequence without parsing it with date:



    for dd in $(seq -w 20150101 20151231) ; do 
    ln -s $origin/$dd.log.gz $target/$dd.log.gz
    done


    How do I get rid of all the broken symlinks I just created in a single fell swoop?










    share|improve this question



























      3












      3








      3








      I have created symlinks to a large amount of logfiles. The syntax of the logfiles is yyyymmdd.log.gz.



      To simplify things I use a simple sequence without parsing it with date:



      for dd in $(seq -w 20150101 20151231) ; do 
      ln -s $origin/$dd.log.gz $target/$dd.log.gz
      done


      How do I get rid of all the broken symlinks I just created in a single fell swoop?










      share|improve this question
















      I have created symlinks to a large amount of logfiles. The syntax of the logfiles is yyyymmdd.log.gz.



      To simplify things I use a simple sequence without parsing it with date:



      for dd in $(seq -w 20150101 20151231) ; do 
      ln -s $origin/$dd.log.gz $target/$dd.log.gz
      done


      How do I get rid of all the broken symlinks I just created in a single fell swoop?







      bash find symlink






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 11 '16 at 23:52









      Gilles

      538k12810881606




      538k12810881606










      asked Oct 7 '16 at 13:24









      runlevel0runlevel0

      268210




      268210






















          2 Answers
          2






          active

          oldest

          votes


















          10














          This simple one liner does the job quite fast, requires GNU find:



          find . -xtype l -delete


          A bit of explanation:



          -xtype l tests for links that are broken (it is the opposite of -type)



          -delete deletes the files directly, no need for further bothering with xargs or -exec



          NOTE: -xtype l means -xtype low case L (as in link) ;)



          GNU Findutils: Find






          share|improve this answer





















          • 1





            Took me a second to notice it's a lower case L and not 1, on -xtype l.

            – Telmo Marques
            Feb 2 at 18:40








          • 1





            thx, I wrote a small note.

            – runlevel0
            Feb 8 at 10:35



















          0














          With zsh (you're actually using zsh syntax in your code; with bash, you'd need to quote those variables):



          rm -- $target/*(-@)


          Or:



          rm -- $target/<20150101-20151231>.log.gz(-@)


          *(@) matches the files of type symlink. *(-@) are the ones that are still of type symlink after symlink resolution (that is, those for which the target of the symlink can't be resolved). That's equivalent to GNU find's -xtype l.



          In zsh and with GNU ln, you'd rather write your loop as:



          ln -srt $target -- $origin/<20150101-20151231>.log.gz


          Which would also work even if $origin contains a relative path (and creates relative symlinks which reduces the risk of symlinks to be broken if some path components of the origin (the ones that are common with that of the target) are renamed in the future).






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


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f314974%2fhow-to-delete-broken-symlinks-in-one-go%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









            10














            This simple one liner does the job quite fast, requires GNU find:



            find . -xtype l -delete


            A bit of explanation:



            -xtype l tests for links that are broken (it is the opposite of -type)



            -delete deletes the files directly, no need for further bothering with xargs or -exec



            NOTE: -xtype l means -xtype low case L (as in link) ;)



            GNU Findutils: Find






            share|improve this answer





















            • 1





              Took me a second to notice it's a lower case L and not 1, on -xtype l.

              – Telmo Marques
              Feb 2 at 18:40








            • 1





              thx, I wrote a small note.

              – runlevel0
              Feb 8 at 10:35
















            10














            This simple one liner does the job quite fast, requires GNU find:



            find . -xtype l -delete


            A bit of explanation:



            -xtype l tests for links that are broken (it is the opposite of -type)



            -delete deletes the files directly, no need for further bothering with xargs or -exec



            NOTE: -xtype l means -xtype low case L (as in link) ;)



            GNU Findutils: Find






            share|improve this answer





















            • 1





              Took me a second to notice it's a lower case L and not 1, on -xtype l.

              – Telmo Marques
              Feb 2 at 18:40








            • 1





              thx, I wrote a small note.

              – runlevel0
              Feb 8 at 10:35














            10












            10








            10







            This simple one liner does the job quite fast, requires GNU find:



            find . -xtype l -delete


            A bit of explanation:



            -xtype l tests for links that are broken (it is the opposite of -type)



            -delete deletes the files directly, no need for further bothering with xargs or -exec



            NOTE: -xtype l means -xtype low case L (as in link) ;)



            GNU Findutils: Find






            share|improve this answer















            This simple one liner does the job quite fast, requires GNU find:



            find . -xtype l -delete


            A bit of explanation:



            -xtype l tests for links that are broken (it is the opposite of -type)



            -delete deletes the files directly, no need for further bothering with xargs or -exec



            NOTE: -xtype l means -xtype low case L (as in link) ;)



            GNU Findutils: Find







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Feb 8 at 10:35

























            answered Oct 7 '16 at 13:24









            runlevel0runlevel0

            268210




            268210








            • 1





              Took me a second to notice it's a lower case L and not 1, on -xtype l.

              – Telmo Marques
              Feb 2 at 18:40








            • 1





              thx, I wrote a small note.

              – runlevel0
              Feb 8 at 10:35














            • 1





              Took me a second to notice it's a lower case L and not 1, on -xtype l.

              – Telmo Marques
              Feb 2 at 18:40








            • 1





              thx, I wrote a small note.

              – runlevel0
              Feb 8 at 10:35








            1




            1





            Took me a second to notice it's a lower case L and not 1, on -xtype l.

            – Telmo Marques
            Feb 2 at 18:40







            Took me a second to notice it's a lower case L and not 1, on -xtype l.

            – Telmo Marques
            Feb 2 at 18:40






            1




            1





            thx, I wrote a small note.

            – runlevel0
            Feb 8 at 10:35





            thx, I wrote a small note.

            – runlevel0
            Feb 8 at 10:35













            0














            With zsh (you're actually using zsh syntax in your code; with bash, you'd need to quote those variables):



            rm -- $target/*(-@)


            Or:



            rm -- $target/<20150101-20151231>.log.gz(-@)


            *(@) matches the files of type symlink. *(-@) are the ones that are still of type symlink after symlink resolution (that is, those for which the target of the symlink can't be resolved). That's equivalent to GNU find's -xtype l.



            In zsh and with GNU ln, you'd rather write your loop as:



            ln -srt $target -- $origin/<20150101-20151231>.log.gz


            Which would also work even if $origin contains a relative path (and creates relative symlinks which reduces the risk of symlinks to be broken if some path components of the origin (the ones that are common with that of the target) are renamed in the future).






            share|improve this answer






























              0














              With zsh (you're actually using zsh syntax in your code; with bash, you'd need to quote those variables):



              rm -- $target/*(-@)


              Or:



              rm -- $target/<20150101-20151231>.log.gz(-@)


              *(@) matches the files of type symlink. *(-@) are the ones that are still of type symlink after symlink resolution (that is, those for which the target of the symlink can't be resolved). That's equivalent to GNU find's -xtype l.



              In zsh and with GNU ln, you'd rather write your loop as:



              ln -srt $target -- $origin/<20150101-20151231>.log.gz


              Which would also work even if $origin contains a relative path (and creates relative symlinks which reduces the risk of symlinks to be broken if some path components of the origin (the ones that are common with that of the target) are renamed in the future).






              share|improve this answer




























                0












                0








                0







                With zsh (you're actually using zsh syntax in your code; with bash, you'd need to quote those variables):



                rm -- $target/*(-@)


                Or:



                rm -- $target/<20150101-20151231>.log.gz(-@)


                *(@) matches the files of type symlink. *(-@) are the ones that are still of type symlink after symlink resolution (that is, those for which the target of the symlink can't be resolved). That's equivalent to GNU find's -xtype l.



                In zsh and with GNU ln, you'd rather write your loop as:



                ln -srt $target -- $origin/<20150101-20151231>.log.gz


                Which would also work even if $origin contains a relative path (and creates relative symlinks which reduces the risk of symlinks to be broken if some path components of the origin (the ones that are common with that of the target) are renamed in the future).






                share|improve this answer















                With zsh (you're actually using zsh syntax in your code; with bash, you'd need to quote those variables):



                rm -- $target/*(-@)


                Or:



                rm -- $target/<20150101-20151231>.log.gz(-@)


                *(@) matches the files of type symlink. *(-@) are the ones that are still of type symlink after symlink resolution (that is, those for which the target of the symlink can't be resolved). That's equivalent to GNU find's -xtype l.



                In zsh and with GNU ln, you'd rather write your loop as:



                ln -srt $target -- $origin/<20150101-20151231>.log.gz


                Which would also work even if $origin contains a relative path (and creates relative symlinks which reduces the risk of symlinks to be broken if some path components of the origin (the ones that are common with that of the target) are renamed in the future).







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Feb 8 at 11:09

























                answered Feb 8 at 11:03









                Stéphane ChazelasStéphane Chazelas

                306k57581935




                306k57581935






























                    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%2f314974%2fhow-to-delete-broken-symlinks-in-one-go%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 make a Squid Proxy server?

                    Is this a new Fibonacci Identity?

                    19世紀