我有以下文件找到了最新的文件不同的路徑,但具有相同的文件名
./path/to/stuff1/file1 (x)
./path/to/stuff1/file2
./path/to/stuff1/file3
./path/to/stuff2/file1
./path/to/stuff2/file2 (x)
./path/to/stuff2/file3
./path/to/stuff3/file1 (x)
./path/to/stuff3/file2
./path/to/stuff3/file3
,我標誌着我摸到最後的文件。我想要得到那些標記的文件。換句話說:
- 我想向上最新文件每個目錄。
我構建的bash命令
for line in $(find . -name 'file*' -type f | awk -F/ 'sub($NF,x)' | sort | uniq); do
find $line -name 'file*' -type f -printf '%[email protected] %p\n' | sort -n | tail -1 | cut -f2 -d' '
done
這我能夠在使用system
命令並逃逸$
Perl來使用。是否有可能直接在Perl中執行此操作,或者您認爲我的方法是否正確?
編輯
如果可能的任務應該在Perl來完成,而無需使用外部模塊。
EDIT2
對不起,我發現我的問題不明確。我認爲@TLP的答案可行,但我必須澄清:我想檢查每個文件夾中的最新文件,例如最新的文件在stuff1。說我做
touch ./path/to/stuff1/file1
touch ./path/to/stuff2/file2
touch ./path/to/stuff3/file1
我運行腳本之前。那麼就應該輸出:
./path/to/stuff1/file1
./path/to/stuff2/file2
./path/to/stuff3/file1
文件名可以是不同的東西但只有每個路徑的一個文件應該是輸出相同。
@codnodder腳本執行此操作,但我希望僅搜索文件名而不是完整路徑。所以我想搜索以文件開頭的所有文件,並且腳本應該遞歸搜索。
很抱歉的混亂,但我只注意到你的腳本不輸出期望的文件。我編輯了這個問題...... – EverythingRightPlace
這只是改變目錄名稱的關鍵,而不是我現在所做的。 – TLP
太棒了,thx很多! – EverythingRightPlace