【问题标题】:Ignore directory path in find (in shell) [duplicate]忽略查找中的目录路径(在shell中)[重复]
【发布时间】:2015-08-06 09:08:24
【问题描述】:

我正在尝试在某个目录中查找文件并忽略某些路径。

cd /some/dir && find . -type f ! -path ./media 不起作用。哪里出错了?

不应忽略子目录中的任何同名目录。例如/some/media/.

附:同样掩码./media/* 也不起作用。

P.P.S 是的,我知道我可以像 grep -vE "^(\./dir1|\./dir2)" 一样使用 grep。有机会使用 find only 吗?

【问题讨论】:

  • ! -path ./media 仅对于确切的目录(或文件)./media 是错误的,对于该目录中的任何内容(例如 ./media/somefile)仍然是正确的。你想要find . -type f ! -path ./media ! -path './media/*'find . -path ./media -prune -o -type f -print(这实际上会停止遍历目录而不是仅仅不打印它,尽管两者都应该产生相同的输出)

标签: shell find


【解决方案1】:

您可以使用-regex 选项:

find -type f ! -regex '.*/media/.*'

【讨论】:

  • .*/media/.* 应该只是 ./media/.* (第一个 . 可能也需要转义)以满足 Any directory with the same name in subdirs shouldn't be ignored
  • 是的,同意。我在开头使用点。
猜你喜欢
  • 1970-01-01
  • 2019-06-06
  • 2013-02-05
  • 2012-12-13
  • 2019-05-26
  • 1970-01-01
  • 1970-01-01
  • 2014-01-31
  • 2010-11-21
相关资源
最近更新 更多