[英]Linux find files and folders based on name length but output full path
我具有以下文件夾結構:
├── longdirectorywithsillylengththatyouwouldntnormallyhave
│ ├── asdasdads9ads9asd9asd89asdh9asd9asdh9asd
│ └── sinlf
└── shrtdir
├── nowthisisalongfile0000000000000000000000000
└── sfile
我需要查找名稱長度大於x個字符的文件和文件夾。 我已經能夠做到這一點:
find . -exec basename '{}' ';' | egrep '^.{20,}$'
longdirectorywithsillylengththatyouwouldntnormallyhave
asdasdads9ads9asd9asd89asdh9asd9asdh9asd
nowthisisalongfile0000000000000000000000000
但是,這僅輸出相關文件或文件夾的名稱。 我如何輸出結果匹配的完整路徑,如下所示:
/home/user/Desktop/longdirectorywithsillylengththatyouwouldntnormallyhave
/home/user/Desktop/longdirectorywithsillylengththatyouwouldntnormallyhave/asdasdads9ads9asd9asd89asdh9asd9asdh9asd
/home/user/Desktop/shrtdir/nowthisisalongfile0000000000000000000000000
我現在無法測試,但這應該可以完成工作:
find $(pwd) -exec basename '{}' ';' | egrep '^.{20,}$'
find -name "????????????????????*" -printf "$PWD/%P\n"
find的-printf選項非常強大。 %P:
%P File's name with the name of the starting-point under which it was found removed. (%p starts with ./).
因此,我們在前面添加了$ PWD /。
/home/stefan/proj/mini/forum/tmp/Mo/shrtdir/nowthisisalongfile0000000000000000000000000
/home/stefan/proj/mini/forum/tmp/Mo/longdirectorywithsillylengththatyouwouldntnormallyhave
/home/stefan/proj/mini/forum/tmp/Mo/longdirectorywithsillylengththatyouwouldntnormallyhave/asdasdads9ads9asd9asd89asdh9asd9asdh9asd
為了防止我們手動計算問號,我們使用:
for i in {1..20}; do echo -n "?" ; done; echo
????????????????????
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.