linux - 使用 find 命令但排除两个目录中的文件

标签 linux shell unix find

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

我的命令是这样的:

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

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

有人对此有想法吗?

最佳答案

您可以通过以下方式指定 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/
  • 开头的所有结果

测试解决方案:

$ 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 考虑整个路径 =)

关于linux - 使用 find 命令但排除两个目录中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14132210/

相关文章:

python - 如何使用python从服务器读取本地PC中的文件

c - linux中的ls命令使用哪个linux系统调用来显示文件夹/文件名?

bash - 从命令行覆盖 Bash 脚本中的变量

shell - 避免 shell 脚本中的交互模式

shell - 如何将以下 shell 脚本转换为更多数量的命令行输入参数。

linux - Flask中使用curl上传文件

linux - 使用其他服务器中 createrepo 创建的本地 yum 存储库

unix - SSH portforwarding UNIX

unix - ar 在现有的 .a 文件上?

bash - 如何将脚本的输出作为参数传递给 ssh 命令?