簡體   English   中英

Linux根據名稱長度查找文件和文件夾,但輸出完整路徑

[英]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

如果在文件上使用basename ,則會丟失有關實際處理的文件的信息。

因此,您必須更改您的正則表達式才能識別最后一個路徑組件的長度。

我能想到的最簡單的方法是:

find . | egrep '[^/]{20,}$' | xargs readlink -f

這利用了以下事實: 文件名不能包含斜杠

結果是包含相對於您當前cwd路徑, readlink 可以用來為您提供完整路徑。

我現在無法測試,但這應該可以完成工作:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM