Find the answer to your Linux question:
Results 1 to 5 of 5
Like Tree2Likes
  • 1 Post By Cabhan
  • 1 Post By Cabhan
Searching for it in google I've found: # find /usr/bin -type l ! -xtype f ! -xtype d -ok rm -f {} \; (for gentoo, if it makes some difference) ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie
    Join Date
    Apr 2007
    Posts
    214

    Removing/moving multiple dead symlinks by command line


    Searching for it in google I've found:

    # find /usr/bin -type l ! -xtype f ! -xtype d -ok rm -f {} \;

    (for gentoo, if it makes some difference)

    But that's quite slow, as it asks for confirmation for each file...

    Isn't there some way to change this a little to move the dead symlinks without asking to some folder ("simulating" a trash folder, or could even be the trashcan itself, if anyone wants it, of course) without asking (or even removing without asking, if such "find" parameters are sure to not delete anything that wouldn't be a dead symlink or an empty file).

    I tried a few "tweaks" on this expression myself but without any success...

  2. #2
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    Your problem here is the use of "-ok". This asks for confirmation on each file.

    Here is what I might suggest:
    Code:
    find -L -type l -delete
    How does this work? Well, the "-L" option means to always follow symbolic links. We then look for any symbolic links. "But wait!", I hear you cry. "We will never find a symbolic link if we're always following them!". That's true, except that "-type l" will return true when "-L" is in effect only when the symbolic link is broken. We then use "-delete" to delete the file.

    Now, this will have the property of going into directories if a symbolic link points there. If we want to only look in the given directory, I may suggest:
    Code:
    find -L -maxdepth 1 -type l -delete
    where it will only delve into the commandline arguments (and no further).
    the dsc likes this.

  3. #3
    Linux Newbie
    Join Date
    Apr 2007
    Posts
    214
    Many thanks! Funny how it looks almost nothing like the other expression I saw first. I wonder how you guys came to know this sort of thing, I don't know, seems more complicated than DOS equivalents (when it exists). I think you got to know some (recent) programming stuff. If I recall, the whole syntax of "rename" is in python or something. (but perhaps just some time spent on "man find"? )


    maxdepth 2 will do the job for me, as I have a folder with many subfolders, but these subfolders don't have any subfolder themselves.



    I didn't quite get what it would do without the maxdepth argument. Would it go to the dir where the first link it finds points (if the location still exists) and then keep following links to anywhere they point without doing anything else, is that it?

  4. #4
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    The reason for the maxdepth is simple: we are telling find to follow links. Suppose we had a structure like this:
    Code:
    /home/user/dir
      /home/user/dir/a
      /home/user/dir/b --> /home/user/dir
    (so /home/user/dir/b is a link)

    Without a maxdepth, find will follow the b link back to its parent directory, creating an infinite loop. By specifying "-maxdepth 1", find will follow any commandline arguments (so it will go into /home/user/dir), but it won't follow anything that it encounters (so if "a" is a directory, it won't go into it, but it also won't follow "b"). This is, I think, the safest way.

    I personally know a lot about find because I think it's a very powerful utility, and I have helped many people use it correctly. But yeah, it's a lot of reading man pages and experimentation. I don't think that you need to know how to program, though of course some understanding of Linux will always be helpful.

    Also, I don't know anything about Batch scripting (DOS scripting), but some Linux utilities can be very complicated because the idea is that you have ultimate control over the process. If you look at the find man page, there are tons of tests, because you can really try to classify files in almost any way.

    It's a big world, and the best way to learn it is by playing with it. It's a fun process.
    the dsc likes this.

  5. #5
    Just Joined!
    Join Date
    Apr 2013
    Posts
    1
    Amazing stuff!

    Quote Originally Posted by Cabhan View Post
    Your problem here is the use of "-ok". This asks for confirmation on each file.

    Here is what I might suggest:
    Code:
    find -L -type l -delete
    How does this work? Well, the "-L" option means to always follow symbolic links. We then look for any symbolic links. "But wait!", I hear you cry. "We will never find a symbolic link if we're always following them to the real file!". That's true, "-type l" will never return true when "-L" is in effect, unless the symbolic link is broken. We then use "-delete" to delete the file.

    Now, this will have the property of going into directories if a symbolic link points there. If we want to only look in the given directory, I may suggest:
    Code:
    find -L -maxdepth 1 -type l -delete
    where it will only delve into the commandline arguments (and no further).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •