在 Linux 的子目录中搜索以 .txt 结尾的文件



我要做的是在子目录中搜索所有以.txt结尾的文件,例如,我在名为user的目录中,该目录仅包含目录Test1Test2Test1 包含 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结尾(不是子目录的子目录)我应该怎么做才能得到我想要的结果?提前致谢

来自 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.

我相信你想要

 find -mindepth 2 -maxdepth 2 -name "*.txt"
您可以使用

awkbasenamedirname

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

相关内容

最新更新