我有以下文件找到了最新的文件不同的路径,但具有相同的文件名
./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