【问题标题】:Search for file end with .txt in subdirectories in linux在linux的子目录中搜索以.txt结尾的文件
【发布时间】:2014-01-31 14:09:30
【问题描述】:

我要做的是在子目录中搜索所有以.txt结尾的文件,例如,我在名为user的目录中,该目录仅包含目录Test1 em> 和 Test2Test1 包含 t1.txt 和另一个子目录 Test3Test2 包含 t2.txt。 Test3 包含 t3.txt

我尝试使用 find 和 egrep

find */ -name "*.txt" | egrep "*/[^/]*(.txt)$"

但它给了我

Test1/Test3/t3.txt
Test1/t1.txt
Test2/t2.txt

我想打印类似的东西

Test1/t1.txt
Test2/t2.txt

因为我只想在子目录(不是子目录的子目录)中以 .txt 结尾的文件 我应该怎么做才能得到我想要的结果?提前致谢

【问题讨论】:

  • 为什么不只是ls */*.txt
  • 或者更确切地说是echo */*.txt(或者如果你有时髦的文件名,可能是printf)。
  • @tripleee 这实际上解决了问题。我想多了。非常感谢!

标签: linux command-line glob


【解决方案1】:

我相信你想要

 find -mindepth 2 -maxdepth 2 -name "*.txt"

【讨论】:

  • 感谢您的回答。 :) 但我只想要子目录中的文件。这也将打印当前目录中的文件。
  • @OKC 没问题,编辑答案添加“-mindepth 2”;现在它确实做到了!
【解决方案2】:

您可以使用awkbasenamedirname

  awk -F/ '{print $(NF-1)}'

【讨论】:

  • -1 这似乎不是这个特定问题的答案。
【解决方案3】:

来自man find

   -maxdepth levels
      Descend at most levels (a non-negative integer) levels of direc-
      tories below the command line arguments.   '-maxdepth  0'  means
      only  apply the tests and actions to the command line arguments.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-15
    • 1970-01-01
    • 2018-08-26
    • 1970-01-01
    • 2012-11-25
    • 1970-01-01
    • 2021-10-29
    • 1970-01-01
    相关资源
    最近更新 更多