すべてのシンボリックリンクを探し、どれがファイルで、どれがディレクトリであるかを教えてくれます。

すべてのシンボリックリンクを探し、どれがファイルで、どれがディレクトリであるかを教えてくれます。

私は、ディレクトリ構造内のすべてのシンボリックリンクを見つけ、どちらがファイルリンクであり、どちらがディレクトリリンクであるかを区別できるようにしたいと思います。

このコマンドは、どのリンクがディレクトリであり、どのリンクがファイルであるかを知らせるだけでなく、必要なすべての操作を実行します。

find . -type l -ls

ベストアンサー1

どのファイルを探したいかを推測します一般ファイルそしてどれ目次

したがって、次のように使用できます。

ソリューション1

find -type l -exec stat -L --printf '%n is a %F\n' {} +

名前付きパイプまたは他の種類のファイルへのシンボリックリンクがある場合、上記のコマンドはそのリンクも印刷します。

壊れたリンクを無視する:

find -type l ! -xtype l -exec stat -L --printf '%n is a %F\n' {} +
#or
find -type l -not -xtype l -exec stat -L --printf '%n is a %F\n' {} +
#Or
find -type l -readable  -exec stat -L --printf '%n is a %F\n' {} +

stat -Lはリンクをたどるために使用され、%nおよび%Fはそれぞれファイル名とファイル形式を取得するために使用されます。

たとえば、次のような構造を使用します。

lrwxrwxrwx 1 edgar edgar   65 Nov 29 17:51 dirOne2Link -> /home/edgar/Documents/Gitlab/Linux_programming/testing/forums/one
lrwxrwxrwx 1 edgar edgar   65 Nov 29 17:54 fileTwo2Link -> /home/edgar/Documents/Gitlab/Linux_programming/testing/forums/two
drwxr-xr-x 2 edgar edgar 4096 Nov 29 17:50 one
-rwxr--r-- 1 edgar edgar  201 Nov 25 21:13 script
-rw-r--r-- 1 edgar edgar    0 Nov 29 17:50 two

find上記のコマンドを使用すると、次のようになります。

./fileTwo2Link is a regular empty file
./dirOne2Link is a directory

ソリューション2

find次のことなく行うことができます -exec stat ...

find -type l  -printf "%p is a %Y\n"

上記のコマンドを使用すると、次のようになります。

./pipelink is a p
./fileTwo2Link is a f
./onemorebroken is a N
./dirOne2Link is a d
./broken is a N

ここではp、名前付きパイプ、fは通常のファイル、Nは存在しないファイル(たとえば、壊れたリンク)、およびdはディレクトリです。

おすすめ記事