How to list files of which no symlinks exist

command linefilesfindsymlink

I have a large "myfiles" directory full of miscellaneous documents and do not want to modify its structure.

I therefore created (several) other directories for each class of documents. For example, I have an "images" directory which has symlinks to each .jpg or .cr2 file in the "myfiles" directory plus other descriptive files for each symlink (with the same filename) with description and other metadata. The symlinks in the /images directory might have a different name from the original linked file.

I am trying to find the simplest way to make sure each and every image file in the "myfiles" directory has a symlink into the "images" directory.

See an example of the folder structure

/myfiles/a.doc
/myfiles/b.jpg
/myfiles/c.cr2
/myfiles/d.mov

should result

/images/b_800x600.jpg
/images/b_800x600.desc
/images/c_3820x5640.cr2
/images/c_3820x5640.cr2

Best Answer

If I undersood the question correctly you need files in myfiles which do not have symlinks in images:

#!/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"

There is a caveat with this approach if you have file a.jpg in directory myfiles/1 and you have a symlink to that file in directory images/3 or simply in images/ the file will not be reported with missing symlink.

Related Question