[英]How to check if file has a symlink
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. 我正在编写一个bash shell脚本(用于RH和Solaris),它使用openssl为同一目录中的所有证书创建哈希符号链接。 I have approx 130 certificates and when new certs are added I would like the script to create symlinks for the new certs only.
我有大约130个证书,当添加新证书时,我希望脚本只为新证书创建符号链接。 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. 但我不确定如何在for或其他循环中否定或找到此结果的反转。 I want to find all files in the current directory missing a symlink.
我想找到当前目录中缺少符号链接的所有文件。
Thanks! 谢谢!
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 : 假设GNU
find
(我使用非标准动作-printf
推断其使用),以下命令将输出当前目录中不是位于同一目录中的符号链接目标的所有文件的名称 :
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. 此外, GNU
find
不对输出进行排序,因此需要显式sort
命令。
You can process the resulting files in a shell loop to create the desired symlinks. 您可以在shell循环中处理生成的文件以创建所需的符号链接。
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.) (在上面的命令中,
l
作为示例附加到目标文件名以形成符号链接名称。)
$ ls -srlt
example1.png
example.png -> ../example.png
$ find . -type f -print
./example1.png
$ find . ! -type f -print
.
./example.png
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.