如何列出不存在符号链接的文件?

Rob*_*der 10 command-line find symlink files

我有一个很大的“myfiles”目录,里面装满了各种文件,不想修改它的结构。

因此,我为每一类文档创建了(几个)其他目录。例如,我有一个“images”目录,其中包含指向“myfiles”目录中每个.jpg.cr2文件的符号链接,以及每个符号链接(具有相同文件名)的其他描述性文件以及描述和其他元数据。/images 目录中的符号链接可能与原始链接文件的名称不同。

我试图找到最简单的方法来确保“myfiles”目录中的每个图像文件都有一个指向“images”目录的符号链接。

查看文件夹结构示例

/myfiles/a.doc
/myfiles/b.jpg
/myfiles/c.cr2
/myfiles/d.mov
Run Code Online (Sandbox Code Playgroud)

应该结果

/images/b_800x600.jpg
/images/b_800x600.desc
/images/c_3820x5640.cr2
/images/c_3820x5640.cr2
Run Code Online (Sandbox Code Playgroud)

tal*_*zin 6

如果我正确理解了这个问题,您需要 myfiles 中的文件,这些文件在图像中没有符号链接:

#!/bin/bash

OIFS="$IFS"
IFS=$'\n'

files="$(find myfiles/ -type f -name '*.jpg' -or -name '*.cr2')"
for f in $files; do
    list="$(find -L images/ -xtype l -samefile "$f")"
    if [[ "$list" == "" ]]; then
        echo "$f does not have symlink."
    fi
done

IFS="$OIFS"
Run Code Online (Sandbox Code Playgroud)

如果您在目录 myfiles/1 中有文件 a.jpg 并且您在目录 images/3 或仅在 images/ 中有指向该文件的符号链接,则此方法有一个警告,该文件将不会报告缺少符号链接。