如何列出符号链接未引用的所有文件

时间:2014-03-07 21:47:21

标签: linux unix find symlink uniq

我想列出目录中未被同一目录中的任何符号链接引用的所有文件。因此,如果一个文件被另一个目录中的符号链接引用并且仍然会被列出。我尝试使用findreadlinkuniq,但它没有按照我想要的方式进行操作

\(
find -maxdepth 1 -type l -exec readlink {} ';' ;
find -maxdepth 1 -type f 
\) > "output"
uniq -u "output" 

我是Unix / Linux的新手。提前致谢

1 个答案:

答案 0 :(得分: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

-v的{​​{1}}选项使其反转其匹配逻辑。换句话说,“给我所有匹配模式的项目。” grep选项告诉-F该模式由固定字符串列表组成,而不是正则表达式。您不希望grep尝试将文件名中的任何特殊字符解释为正则表达式符号。最后,grep的{​​{1}}选项告诉它从文件而不是从命令行读取此固定字符串列表。