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.
You want++OK, actually not exactly.readlink -f
rather thanls -l
.readlink
won’t print path to the symlink so it’s not as straightforward.++Also, you want
+
infind ... -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 {} +