bash 在名称以数字开头的目录中查找编号最高的文件名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1587059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 21:17:28  来源:igfitidea点击:

Find highest numbered filename in a directory where names start with digits

bashshellsedls

提问by Jake

I have a directory with files that look like this:

我有一个包含如下文件的目录:

001_something.php  002_something_else.php
004_xyz.php        005_do_good_to_others.php

I ultimately want to create a new, empty PHP file whose name starts with the next number in the series.

我最终想创建一个新的空 PHP 文件,其名称以系列中的下一个数字开头。

LIST=`exec ls $MY_DIR | sed 's/\([0-9]\+\).*//g' | tr '\n' ' '`

The preceding code gives me a string like this:

前面的代码给了我一个这样的字符串:

LIST='001 002 004 005 '

I want to grab that 005, increment by one, and then use that number to generate the new filename. How do I do that in BASH?

我想获取那个 005,加一,然后使用这个数字来生成新的文件名。我如何在 BASH 中做到这一点?

采纳答案by DigitalRoss

$ LIST=($LIST)
$ newfile=`printf %03d-whatever $((10#${LIST[${#LIST}]}+1))`
$ echo $newfile
006-whatever

So, that's bash-specific. Below is a any-posix-shell-including-bashsolution, tho I imagine something simpler is possible.

所以,这是特定于 bash 的。下面是一个any-posix-shell-including-bash解决方案,虽然我想更简单的事情是可能的。

$ cat /tmp/z
f () {
    eval echo ${$#} | sed -e 's/^0*//'
}
LIST='001 002 004 005 '
newname=`printf %03d-whatever $(($(f $LIST) + 1))`
echo $newname
$ sh /tmp/z
006-whatever
$ 

回答by bmb

Do you need the whole LIST?

你需要整个 LIST 吗?

If not

如果不

LAST=`exec ls $MY_DIR | sed 's/\([0-9]\+\).*//g' | sort -n | tail -1`

will give you just the 005 part and

只会给你 005 部分和

printf "%03d" `expr 1 + $LAST`

will print the next number in the sequence.

将打印序列中的下一个数字。

回答by Idelic

Using only standard tools, the following will give you the prefix for the new file (006):

仅使用标准工具,以下内容将为您提供新文件 (006) 的前缀:

ls [0-9]* | sed 's/_/ _/' | sort -rn | awk '{printf "%03d",  + 1; exit}'

回答by Vijay

This seems to be more simple.

这似乎更简单。

ls [0-9]* | sort -rn | awk '{FS="_"; printf "%03d_new_file.php\n",+1;exit}'

回答by Paused until further notice.

Fast, subshell-free and loop-free (also handles filenames with spaces)*:

快速、无子外壳和无循环(也处理带空格的文件名)*:

list=([0-9]*)
last=${list[@]: -1}
nextnum=00$((10#${last%%[^0-9]*} + 1))
nextnum=${nextnum: -3}
touch ${nextnum}_a_new_file.php

Given your example files the output would be:

鉴于您的示例文件,输出将是:

006_a_new_file.php

回答by ghostdog74

  1. touch "newfile_$(printf "%03d" $(echo $(ls 00?_*.php|sed 's/_.*//'|sort -rn|head -1)+1|bc))"
    

    2.

    num=$(ls 00?*.php|sed 's/.*//'|sort -rn|head -1) 
    touch $(printf "newfile_%03d" $((num+1)))

  1. touch "newfile_$(printf "%03d" $(echo $(ls 00?_*.php|sed 's/_.*//'|sort -rn|head -1)+1|bc))"
    

    2.

    num=$(ls 00?*.php|sed 's/.*//'|sort -rn|head -1) 
    touch $(printf "newfile_%03d" $((num+1)))

回答by SebPe

I post this answer to propose a minimal solution to the core question "Find highest numbered filename in a directory where names start with digits":

我发布此答案是为了针对核心问题“在名称以数字开头的目录中查找编号最高的文件名”提出一个最小的解决方案:

ls -1 "$MyDir" | sort -hr | head -n 1

The first statement puts out the files in MyDir as one liners with the -1 option. This is piped to the sort function which sorts in human numeric order with -h and in reverse order (highest number first) with -r. This is piped to the head function which just delivers the -n top most results (1 in the example). The answer considers human numeric sort order i.e. 221 is higher than 67. It is a POSIX shell solution so highly compatible.

第一条语句将 MyDir 中的文件作为带有 -1 选项的单行输出。这是通过管道传送到 sort 函数,该函数使用 -h 以人类数字顺序排序,并使用 -r 以相反顺序(最高数字在前)排序。这通过管道传送到 head 函数,该函数只提供 -n 最高的结果(示例中为 1)。答案考虑了人类的数字排序顺序,即 221 高于 67。它是一种高度兼容的 POSIX shell 解决方案。

回答by ghostdog74

$ for file in *php; do last=${file%%_*} ; done
$ newfilename="test.php"
$ printf "%03d_%s" $((last+1)) $newfilename
006_test.php

i leave it to you to do the creation of new file

我让你来创建新文件

回答by ghostdog74

Here's a solution (you need bc):

这是一个解决方案(您需要 bc):

#!/bin/bash

LIST='001 002 008 005 004 002 '
a=0

for f in $LIST
do
    t=$(echo $f + 1|bc)
    if [ $t -ge $a ]
    then
        a=$t
    fi
done

printf "%03d\n" $a