Skip to content

Instantly share code, notes, and snippets.

@c00kiemon5ter
Created December 7, 2011 00:54
Show Gist options
  • Save c00kiemon5ter/1440866 to your computer and use it in GitHub Desktop.
Save c00kiemon5ter/1440866 to your computer and use it in GitHub Desktop.
find no symlinked files
#!/bin/sh
localdir="/tmp/test" # which directory to check
filelist="/tmp/filelist" # where to store regular file names
symlist="/tmp/symlist" # where to store symlinks file names
results="/tmp/results" # where to store file names that have no symlink pointed to them
cd "$localdir"
# seperate files and symlinks
for file in *; do
[ -L "$file" ] && echo "$file" >> "$symlist" \
|| echo "$file" >> "$filelist";
done
# check if each file is referenced by any symlink
while read -r file; do
while read -r smlnk; do
match=0
[ "$(readlink "$smlnk")" = "$file" ] && match=1 && break;
done <"$symlist"
[ "$match" = 0 ] && echo "$file" >> "$results"
done <"$filelist"
# display results
cat "$results"
# cleanup
rm -f "$filelist" "$symlist" "$results"
@c00kiemon5ter
Copy link
Author

a test case:

$ mkdir /tmp/test
$ touch /tmp/test/{a,b,c}
$ ln -s /tmp/test/a /tmp/test/l1
$ ln -s /tmp/test/b /tmp/test/l2
$ sh findnosymlinkedfiles.sh 
> c

one can extend the script to read the localdir as an argument
or even use fds to eliminate the files

have fun o/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment