Reputation: 53
I am writing a bash shell script (for RH and Solaris) that uses openssl to create hash symlinks to certificates all within the same directory. I have approx 130 certificates and when new certs are added I would like the script to create symlinks for the new certs only. I may have to resort to deleting all symlinks and recreating them but if there is a not-so-difficult way to do this; that is preferable than deleting all and recreating.
I know how to find all files with symlinks:
find . -lname '*.cer'
or
find . -type l -printf '%p -> %l\n'
But I am not sure how to negate or find the inverse of this result in a for or other loop. I want to find all files in the current directory missing a symlink.
Thanks!
Upvotes: 5
Views: 7733
Reputation: 437080
Assuming GNU find
(whose use I infer from your use of nonstandard action -printf
), the following command will output the names of all files in the current directory that aren't the target of symlinks located in the same directory:
comm -23 <(find . -maxdepth 1 -type f -printf '%f\n' | sort) \
<(find . -maxdepth 1 -lname '*' -printf '%l\n' | sort)
Note: This assumes that the symlinks were defined with a mere filename as the target.
Also, GNU find
doesn't sort the output, so explicit sort
commands are needed.
You can process the resulting files in a shell loop to create the desired symlinks.
comm -23 <(find . -maxdepth 1 -type f -printf '%f\n' | sort) \
<(find . -maxdepth 1 -lname '*' -printf '%l\n' | sort) |
while IFS= read -r name; do ln -s "$name" "${name}l"; done
(In the above command, l
is appended to the target filename to form the symlink name, as an example.)
Upvotes: 2
Reputation: 141
$ ls -srlt
example1.png
example.png -> ../example.png
$ find . -type f -print
./example1.png
$ find . ! -type f -print
.
./example.png
Upvotes: 3