简体   繁体   English

Shell脚本如何按升序列出文件名

[英]Shell script how to list file name in ascending order

I am new to linux, writing a bash script below.我是 linux 的新手,在下面写了一个 bash 脚本。 The files in the current folder are stored as 1.jpg,2.jpg, and so on, I have to process files sequentially according to their names but in the below loop I get file names is some different order.当前文件夹中的文件存储为 1.jpg、2.jpg 等,我必须根据文件名称顺序处理文件,但在下面的循环中,我得到的文件名顺序不同。

for i in ./*.jpg
do
filename=$(basename "$i")
echo "filename is  ./$filename"
done

output I get is like this我得到的output是这样的

filename is  ./10.jpg
filename is  ./11.jpg
filename is  ./12.jpg
filename is  ./13.jpg
filename is  ./14.jpg
filename is  ./15.jpg
filename is  ./16.jpg
filename is  ./17.jpg
filename is  ./18.jpg
filename is  ./19.jpg
filename is  ./1.jpg
filename is  ./20.jpg
filename is  ./21.jpg
filename is  ./22.jpg
filename is  ./27.jpg
filename is  ./28.jpg
filename is  ./29.jpg
filename is  ./2.jpg
filename is  ./3.jpg
filename is  ./4.jpg
filename is  ./6.jpg
filename is  ./7.jpg
filename is  ./8.jpg
filename is  ./9.jpg

Any assistance as to how can I process them in the sequence of names 1.jpg, 2.jpg etc关于如何按名称 1.jpg、2.jpg 等顺序处理它们的任何帮助

Pathname expansion (glob expansion) returns a list of filenames which is alphabetically sorted according to your current locale.路径名扩展(glob 扩展)返回一个文件名列表,该列表根据您当前的语言环境按字母顺序排序。 If you have something simple like UTF-8 or C , your sorting order will be ASCII sorted .如果您有像UTF-8C这样简单的东西,那么您的排序顺序将是ASCII 排序的。 This is visible in the result of the OP.这在 OP 的结果中可见。 The file with name 19.jpg is sorted before 1.jpg because the lt;dot>-character has a higher lexicographical order than the character 9 .名称为19.jpg的文件排在1.jpg之前,因为 lt;dot>-字符的字典顺序高于字符9

If you want to traverse your files in a different sorting order, then a different approach needs to be taken.如果您想以不同的排序顺序遍历文件,则需要采用不同的方法。

Under the bold assumption that the OP requests to traverse the files in a numeric sorted way, ie order the names according to a number at the beginning of the file-name, you can do the following:在OP请求以数字排序方式遍历文件的粗体假设下,即根据文件名开头的数字对名称进行排序,您可以执行以下操作:

while IFS= read  -r -d '' file; do 
   echo "filename: $file"
done < <(find .  -maxdepth 1 -type f -name '*.jpg' -print0 | sort -z -n)

Here we use find to list all files in the current directory ( depth==1 ) we print them with a \0 as a separator, and use sort to ask for the requested sorting, indicating that we have \0 as the field separator.这里我们使用find列出当前目录中的所有文件( depth==1 )我们用\0作为分隔符打印它们,并使用sort请求排序,表明我们有\0作为字段分隔符。 Instead of using a for-loop, we use a while-loop to read the information.我们不使用 for 循环,而是使用 while 循环来读取信息。

See BashPitFall001 For some details有关详细信息,请参阅BashPitFall001

note: sort -z is a GNU extension注意: sort -z是一个 GNU 扩展

not quite sure if this is what you're asking, but you have the echo inside your loop which will cause it to be printed in a different row each time.不太确定这是否是您要问的,但是您的循环中有回声,这将导致它每次都打印在不同的行中。

you can do:你可以做:

list "" 
for i in ./*.jpg
do
filename=$(basename "$i")
list="$list $filename"
done    
echo  "files: $list"

which would output这将是 output

files: 1.jpg 2.jpg文件:1.jpg 2.jpg

Nevertheless, You should clarify your question.不过,您应该澄清您的问题。

From your requirement to "process them in the sequence of names 1.jpg, 2.jpg etc" , this will accomplish that.根据您对“按名称 1.jpg、2.jpg 等的顺序处理它们”的要求,这将实现这一点。 The sort specifies a numeric key obtained by defining the first field as the string before a "."排序指定通过将第一个字段定义为“。”之前的字符串获得的数字键。 delimiter.分隔符。

#!/usr/bin/env bash

shopt -s nullglob
allfiles=(*.jpg);

for f in "${allfiles[@]}"
do
    echo "$f"
done | sort -t"." -k1n

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM