ディレクトリ内のディレクトリのみを検索し、リンクされたディレクトリとそのリンクを除外する必要があります。

ディレクトリ内のディレクトリのみを検索し、リンクされたディレクトリとそのリンクを除外する必要があります。

私はルートディレクトリにあり、そこにいくつかのフォルダがあります。

0.1
0.2
0.3
0.4
0.5
0.6
shortcut -> 0.6

0.6フォルダだけでなく、ショートカットなしで上記のディレクトリを一覧表示する必要があります。この場所の上やフォルダ内では検索しません。ここでもいくつかのファイルがあるかもしれませんが、無視する必要があります。同じ命名規則を持つ新しいフォルダが時々このディレクトリに追加されるため、この検索はbashスクリプトに含まれ、新しいフォルダを追加してスクリプトを実行すると他の結果が生成されます。

頑張ってたけどfind -P . -maxdepth 1 -type d -ls運がなかった。

ベストアンサー1

シンボリックリンクを見つけて追跡する以外に、シンボリックリンクのターゲット名が何であるかを知る方法はありません。

したがって、次のようにすることができます(bashバージョン4.0以降を想定)。

#!/bin/bash

# Our blacklist and whitelist associative arrays (whitelist is a misnomer, I know)
# blacklist: keyed on confirmed targets of symbolic links
# whitelist: keyed on filenames that are not symbolic links
#            nor (yet) confirmed targets of symbolic links

declare -A blacklist whitelist

for name in *; do
    if [ -L "$name" ]; then

        # this is a symbolic link, get its target, add it to blacklist
        target=$(readlink "$name")
        blacklist[$target]=1

        # flag target of link in whitelist if it's there
        whitelist[$target]=0

    elif [ -z "${blacklist[$name]}" ]; then
        # This is not a symbolic link, and it's not in the blacklist,
        # add it to the whitelist.
        whitelist[$name]=1
    fi
done

# whitelist now has keys that are filenames that are not symbolic
# links. If a value is zero, it's on the blacklist as a target of a
# symbolic link.  Print the keys that are associated with non-zeros.
for name in "${!whitelist[@]}"; do
    if [ "${whitelist[$name]}" -ne 0 ]; then
        printf '%s\n' "$name"
    fi
done

スクリプトは、ユーザーのディレクトリを現在の作業ディレクトリとして使用して実行する必要があり、そのディレクトリの名前を想定してはいけません。

おすすめ記事