You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用find命令查找.hs文件并排除sub目录及含test的文件

How to Find .hs Files Excluding a Specific Subdirectory and Test-Named Files

Got it, let's fix this for you—you're right, this shouldn't be as complicated as it feels when you're first figuring out find's quirks.

First, let's break down why your previous attempts didn't work:

  • Your first command find . -name "*.hs" -not -name '*sub*' -not -name "*test*" only excludes files whose names contain "sub", not the entire sub directory and everything inside it. So files inside sub/ that end with .hs would still show up, as long as their names don't have "sub" in them.
  • Your second command find . -name "*.hs" -not -path '/sub' -not -name "*test*" uses an absolute path /sub which doesn't match the relative path from your current directory. Even if you fixed it to ./sub, it would only exclude the directory itself, not the files inside it.

The Simple, Efficient Solution Using -prune

The cleanest way to skip an entire directory (instead of filtering its files after finding them) is with -prune. Here's the command you need:

find . -type d -name "sub" -prune -o -name "*.hs" -not -name "*test*" -print

Let's break this down piece by piece so you understand what's happening:

  • .: Start searching from the current directory
  • -type d -name "sub" -prune: Look for directories named sub, and when we find them, skip (prune) them entirely—we won't even look inside them for files
  • -o: Short for "or"—if the previous condition (finding the sub directory) isn't met, run the next part
  • -name "*.hs": Match any file ending with .hs
  • -not -name "*test*": Exclude any file whose name contains "test"
  • -print: Print the matching files (some find versions do this by default, but adding it makes the behavior explicit and consistent across systems)

Alternative: Using -path to Filter Results

If you prefer a more straightforward (though slightly less efficient) approach, you can use -path to exclude any file inside the sub directory:

find . -name "*.hs" -not -name "*test*" -not -path "./sub/*"

This works by finding all .hs files, then filtering out any that are inside sub/ and any with "test" in their name. The downside here is that find still has to traverse the entire sub directory first before discarding the results—so if sub is large, -prune is better because it avoids that work entirely.

Either way, both commands will give you exactly what you want: all .hs files in your current directory and its subdirectories (except sub/), with no files that have "test" in their name.

内容的提问来源于stack exchange,提问作者user855443

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:18:05