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

时间:2021-11-30 12:21:21

I want to list all files in a directory that are not referenced by any symlink in the same directory. So if a file is referenced by a symlink in another directory that doesn't matter and will still be listed. I tried it with find, readlink and uniq but it doesn't do what I want

我想列出目录中未被同一目录中的任何符号链接引用的所有文件。因此,如果某个文件被另一个目录中的符号链接引用并且仍然会被列出。我尝试使用find,readlink和uniq,但它没有做我想要的

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

I'm new to Unix/Linux. Thanks in advance

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

1 个解决方案

#1


1  

Try this:

尝试这个:

# 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

The -v option of grep causes it to invert its matching logic. In other words, "give me all the items that don't match the pattern." The -F option tells grep that the pattern consists of a list of fixed strings, as opposed to a regex. You don't want grep trying to interpret any special characters in the filenames as regex symbols. Finally, the -f option to grep tells it to read this list of fixed strings from a file instead of from the command line.

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

#1


1  

Try this:

尝试这个:

# 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

The -v option of grep causes it to invert its matching logic. In other words, "give me all the items that don't match the pattern." The -F option tells grep that the pattern consists of a list of fixed strings, as opposed to a regex. You don't want grep trying to interpret any special characters in the filenames as regex symbols. Finally, the -f option to grep tells it to read this list of fixed strings from a file instead of from the command line.

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