【问题标题】:How to list all files that are not referenced by symlinks如何列出符号链接未引用的所有文件
【发布时间】:2014-03-08 09:03:14
【问题描述】:
我想列出一个目录中没有被同一目录中的任何符号链接引用的所有文件。因此,如果文件被另一个目录中的符号链接引用,则无关紧要并且仍会列出。我用find、readlink 和uniq 尝试过,但它没有达到我想要的效果
\(
find -maxdepth 1 -type l -exec readlink {} ';' ;
find -maxdepth 1 -type f
\) > "output"
uniq -u "output"
我是 Unix/Linux 新手。提前致谢
【问题讨论】:
标签:
linux
unix
find
symlink
uniq
【解决方案1】:
试试这个:
# Create a temp file containing the names of all the symlinks
tmp=$(mktemp)
find -maxdepth 1 -type l > $tmp
# List all the regular files, and remove (grep -vF) the symlinks
find -maxdepth 1 -type f | grep -vF -f $tmp
# Clean up
rm -f $tmp
grep 的-v 选项会导致它反转其匹配逻辑。换句话说,“给我所有不与模式匹配的项目。” -F 选项告诉grep 该模式由固定字符串列表组成,而不是正则表达式。您不希望 grep 尝试将文件名中的任何特殊字符解释为正则表达式符号。最后,grep 的 -f 选项告诉它从文件而不是从命令行读取这个固定字符串列表。