[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Symlinks already present

On Tue, Jul 28, 2020 at 4:26 AM Termoregolato <waste at is.invalid> wrote:
> Il 26/07/20 20:39, Dennis Lee Bieber ha scritto:
> > Since symbolic links are essentially just short files containing the
> > path to the eventual target file/directory, with an OS flag that the file
> > is a link
> Yes, I use them massively to give to a lot of directories a kind of
> order, depending on their contents. It's simple to see if link is
> broken, but not if they're duplicate

Ah, I think I get what you're doing.

Do you need an efficient way to see if a single target directory has
multiple symlinks pointing to it, or are you trying to audit the
entire collection all at once? I don't think there's a neat way to do
the former, but the latter isn't too hard. Try something like this:

# Enumerate the target directories (the real/physical ones)
dirs = {dir: None for dir in os.listdir("....")}

# Iterate over the symlinks and see where they point
for link in os.listdir("...."):
    dest = os.readlink(link)
    if dirs[dest]:
        print(dirs[dest], link)
    dirs[dest] = link

You can then also check if any are missing, by seeing if there are any
Nones left in the dirs dict.

Unfortunately there's no real way to shortcut this if you just want to
check one target directory. You'd still have to readlink() every
symlink to try to find them.