linux - 如何查找文件名中编号最大的文件

标签 linux bash file

我在 linux 服务器上有一个文件夹,里面有一堆文件,每次修改文件时,它都会得到一个新编号。文件很多,可以修改很多次。这不需要递归,因为文件存储在平面文件系统中

我想要一个命令,给我一个编号最高的文件列表。

file1.rtf
file2.rtf
file3.rtf
file_1.doc
file_2.doc
anotherfile1.txt
anotherfile2.txt
someotherfile1.rtf
someotherfile2.rtf
someotherfile12.rtf

我正在寻找一个文件列表,例如...

file3.rtf
file_2.doc
anotherfile2.txt
someotherfile12.rtf

提前谢谢你

最佳答案

你可以试试 .它使用正则表达式提取扩展名之前的最后一个数字,并使用哈希仅保存具有最大数字的文件:

perl -e '
    for ( @ARGV ) { 
        next unless m/\A(.*?)(\d+)(\.[^.]+)\Z/;
        $key = $1 . $3;
        if ( ! exists $file{ $key } or $file{ $key }->[0] < $2 ) {
            $file{ $key } = [$2, $_];
        }
    }
    for $f ( keys %file ) {
        printf qq|%s\n|, $file{ $f }->[1];
    }
' *

假设当前目录包含您在问题中提供的文件,它会产生:

anotherfile2.txt
file_2.doc
file3.rtf
someotherfile12.rtf

关于linux - 如何查找文件名中编号最大的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23527748/

相关文章:

java - 嵌入式 Apache FTP 服务器的身份验证失败(错误 530)

linux - 是否可以组合更多 tr 命令?

node.js - Bash 101,$HOME/ivy/bin/node,未显示在 Node 环境的 "require.paths"中

bash - 一组提交的 Git 日志

c - 将变量的值传递给C中的文件名

file - Python将文本文件读取为二进制文件?

linux - VSCode 多光标 Lubuntu

c++ - Linux下视频文件+片段着色器

android - 从哪里开始使用 I2C 的 Android 驱动程序

Python-从文本文件或列表中获取特定字符