使用 find 命令但排除两个目录中的文件 [英] Use find command but exclude files in two directories

查看:17
本文介绍了使用 find 命令但排除两个目录中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想查找以 _peaks.bed 结尾的文件,但排除 tmpscripts 文件夹中的文件.

I want to find files that end with _peaks.bed, but exclude files in the tmp and scripts folders.

我的命令是这样的:

 find . -type f ( -name "*_peaks.bed" ! -name "*tmp*" ! -name "*scripts*" )

但它没有用.tmpscript 文件夹中的文件仍会显示.

But it didn't work. The files in tmp and script folder will still be displayed.

有人对此有想法吗?

推荐答案

以下是您可以使用 find 指定的方法:

Here's how you can specify that with find:

find . -type f -name "*_peaks.bed" ! -path "./tmp/*" ! -path "./scripts/*"

说明:

  • find . - 从当前工作目录开始查找(默认递归)
  • -type f - 指定 find 你只希望结果中的文件
  • -name "*_peaks.bed" - 查找名称以 _peaks.bed
  • 结尾的文件
  • <代码>!-path "./tmp/*" - 排除路径以./tmp/
  • 开头的所有结果
  • <代码>!-path "./scripts/*" - 同时排除路径以 ./scripts/
  • 开头的所有结果
  • find . - Start find from current working directory (recursively by default)
  • -type f - Specify to find that you only want files in the results
  • -name "*_peaks.bed" - Look for files with the name ending in _peaks.bed
  • ! -path "./tmp/*" - Exclude all results whose path starts with ./tmp/
  • ! -path "./scripts/*" - Also exclude all results whose path starts with ./scripts/

测试解决方案:

$ mkdir a b c d e
$ touch a/1 b/2 c/3 d/4 e/5 e/a e/b
$ find . -type f ! -path "./a/*" ! -path "./b/*"

./d/4
./c/3
./e/a
./e/b
./e/5

您非常接近,-name 选项仅考虑基本名称,而 -path 考虑整个路径 =)

You were pretty close, the -name option only considers the basename, where as -path considers the entire path =)

这篇关于使用 find 命令但排除两个目录中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆