Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 如何查找文件名中编号最高的文件_Linux_Bash_File_Perl - Fatal编程技术网

Linux 如何查找文件名中编号最高的文件

Linux 如何查找文件名中编号最高的文件,linux,bash,file,perl,Linux,Bash,File,Perl,我在linux服务器上有一个文件夹,里面有一堆文件,每次修改一个文件,它都会得到一个新的编号。有许多文件,可以有许多修订。这不需要是递归的,因为文件存储在平面文件系统中 我想要一个命令,给我的文件列表与最高的数字 file1.rtf file2.rtf file3.rtf file_1.doc file_2.doc anotherfile1.txt anotherfile2.txt someotherfile1.rtf someotherfile2.rtf someotherfile12.rtf

我在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
for f in *; do 
    [[ $f =~ ([^0-9]+)([0-9]+) ]] && echo "${BASH_REMATCH[1]}/${BASH_REMATCH[2]}/$f"
done | 
sort -t/ -k1,1 -k2,2rn | 
awk -F/ '!seen[$1]++ {print $3}'
anotherfile2.txt
file3.rtf
file_2.doc
someotherfile12.rtf