linux - 我的 find 命令用法有什么问题?

标签 linux bash find

我正在尝试查找其名称与某些 C++ 文件扩展名匹配但排除与此模式匹配的某些目录的所有文件:

find /home/palchan/code -name "*.[CcHh]" -o -name "*.cpp" -o -name "*.hpp" -a ! -name "*pattern*"

这仍然给我输出某些文件,例如:

/home/palchan/code/libFox/pattern/hdr/fox/RedFox.H

里面有图案吗?

这是一个例子:

> ls -R .
.:
libFox

./libFox:
RedFox.C  RedFox.H  pattern

./libFox/pattern:
RedFox.C  RedFox.H

然后我运行:

> find . \( -name "*.[HC]" -a ! -name "*pattern*" \)
./libFox/pattern/RedFox.C
./libFox/pattern/RedFox.H
./libFox/RedFox.C
./libFox/RedFox.H

最佳答案

以下应该有效:

find /home/palchan/code \( -name "*pattern*" \) -prune -o -type f \( -name "*.[CcHh]" -o -name "*.cpp" -o -name "*.hpp" \) -print

来自 man find:

 -name pattern
          Base of file name (the path with the leading directories removed) matches shell pattern pattern.  The metacharacters (`*', `?', and `[]')  match
          a  `.'  at the start of the base name (this is a change in findutils-4.2.2; see section STANDARDS CONFORMANCE below).  To ignore a directory and
          the files under it, use -prune; see an example in the description of -path.  Braces are not recognised as being special, despite the  fact  that
          some  shells  including  Bash  imbue  braces  with  a special meaning in shell patterns.  The filename matching is performed with the use of the
          fnmatch(3) library function.   Don't forget to enclose the pattern in quotes in order to protect it from expansion by the shell.

所以,基本上,您应该使用 -prune排除目录而不是 ! -命名一些东西

关于linux - 我的 find 命令用法有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14719755/

相关文章:

java - 如何在tomcat配置中设置环境变量CA_DATA

Linux 对话框用户输入直接到变量

用于快速创建和填充新测试文件的 BASH shell 脚本

linux - 搜索和替换整个文件

linux - 致命 : git was built without support for git-add--interactive (NO_PERL=1)

python - 在没有输入的情况下使用 GNU 并行生成 n 个工作线程

linux - 用于解析命令输出的 Bash 脚本

macos - GNU 查找所有可执行文件 : Equivalent on OS X?

bash - 查找文件名以指定字符串开头的所有文件?

使用 djvused 程序的 linux shell 脚本不工作