I want to move a directory with a bunch of subdirectories and files. But I have the feeling there might be some symlinks to a few of them elsewhere on the file system. (As in the directory contains the targets of symlinks.)

How do I search all files for symlinks pointing to them?

Some combination of find, stat, ls, realpath, readlink and maybe xargs? I can’t quite figure it out.

  • IsoKiero@sopuli.xyz
    link
    fedilink
    English
    arrow-up
    0
    ·
    23 days ago

    I think it’s easier the other way round, find all symlinks and grep the directory you want to move from results.

    Something like ‘find /home/user -type -l -exec ls -l {} ; | grep yourdirectory’ and work from there. I don’t think there’s an easy way to list which symlinks point to any actual file.

    • mina86@lemmy.wtf
      link
      fedilink
      arrow-up
      0
      ·
      edit-2
      23 days ago

      You want readlink -f rather than ls -l. ++OK, actually not exactly. readlink won’t print path to the symlink so it’s not as straightforward.++

      Also, you want + in find ... -exec ... + rather than ;.

      At this point I feel committed to making readlink work. ;) Here’s the script you want:

      #!/bin/sh
      
      want=$1
      shift
      readlink -f -- "$@" | while read got; do
      	if [ "$got" = "$want" ]; then
      		echo "$1"
      	fi
      	shift
      done
      

      and execute it as:

      find ~ -type l -exec /bin/sh /path/to/the/script /path/to/target/dir {} +
      
  • bizdelnick@lemmy.ml
    link
    fedilink
    arrow-up
    0
    ·
    edit-2
    23 days ago

    find / -lname '/path/you/are/looking/for/*'

    Note that the -lname option is a GNU find extension and may not work with other find implementations.

  • Goingdown@sopuli.xyz
    link
    fedilink
    arrow-up
    0
    ·
    23 days ago

    If you just rename the dir, and then find all broken symlinks in your system?

    find . -xtype l

  • catloaf@lemm.ee
    link
    fedilink
    English
    arrow-up
    0
    ·
    23 days ago

    You could just move the dir and leave a symlink in its place. It doesn’t solve the actual problem, but it’s much easier and will keep everything working just fine.