2014-01-12 44 views
1

我想要做的是搜索子目錄中以.txt結尾的所有文件,例如我在名爲的用戶中,該目錄僅保存目錄Test1 and Test2Test1包含t1.txt和另一個子目錄Test3Test2包含t2.txt。 Test3的包含t3.txt在linux的子目錄中搜索帶有.txt的文件結尾

我嘗試使用查找和egrep命令

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

,但它給了我

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

我想打印像

Test1/t1.txt 
Test2/t2.txt 

由於我只想讓文件在子目錄中以.txt結尾(不是子目錄)子目錄) 我應該怎麼做才能得到我想要的結果?在此先感謝

+2

爲什麼不'LS */* .txt' – kfsone

+1

或者'echo */*。txt'(或者'如果您有時髦的文件名稱,則使用printf')。 – tripleee

+0

@tripleee這實際上解決了這個問題。我一直在想。非常感謝! – OKC

回答

0

我相信你想

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

感謝您的回答。 :)但我只是想在子目錄中的文件。這也將打印當前目錄中的文件。 – OKC

+0

@OKC沒問題,編輯回答添加「-mindepth 2」;現在它確實如此! –

+0

是的,感謝您的編輯! – OKC

2

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. 
-1

您可以使用awkbasenamedirname

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

-1這似乎不是對這個特定問題的答案。 – tripleee

相關問題