I have these files in a folder:
我将这些文件放在一个文件夹中:
chap11-solutions.pdf
chap12-solutions.pdf
chap13-solutions.pdf
chap14-solutions.pdf
chap15-solutions.pdf
chap16-solutions.pdf
chap17-solutions.pdf
chap21-solutions.pdf
chap22-solutions.pdf
chap23-solutions.pdf
chap24-solutions.pdf
chap25-solutions.pdf
chap26-solutions.pdf
chap2-solutions.pdf
chap3-solutions.pdf
chap4-solutions.pdf
chap5-solutions.pdf
chap6-solutions.pdf
chap7-solutions.pdf
chap8-solutions.pdf
chap9-solutions.pdf
how do I sort them in this way: chap1..., chap...2, ...., chap11..., chap12,... using Ubuntu bash shell? Thanks.
我如何以这种方式对它们进行排序:chap1 ...,chap ... 2,....,chap11 ...,chap12,...使用Ubuntu bash shell?谢谢。
4 个解决方案
#1
23
ls|sort -V
The -V
parameter ensures that chap10
is considered upper that chap9
.
-V参数确保chap10被认为是chap9的上限。
#2
8
GNU ls
has a version sort built-in:
GNU ls内置了一个版本排序:
ls -lv
#3
2
If you have ruby(1.9.1+)
如果你有红宝石(1.9.1+)
ruby -e 'puts Dir["chap*pdf"].sort_by{|x|x[/\d+/].to_i}'
#4
1
Assuming that you want to rename the files so you don't have to keep sorting them later:
假设您要重命名文件,以便以后不必继续对它们进行排序:
for f in chap*-solutions.pdf; do num=`echo $f | grep -o "[0123456789]\+"`; two_num=`printf "%02d" $num`; mv $f chap$two_num-solutions.pdf; done
-
grep -o "[0123456789]+"
outputs the chapter number (one or two digits) -
printf
returns a string that contains the zero-padded number
grep -o“[0123456789] +”输出章节号(一位或两位数)
printf返回包含零填充数字的字符串
#1
23
ls|sort -V
The -V
parameter ensures that chap10
is considered upper that chap9
.
-V参数确保chap10被认为是chap9的上限。
#2
8
GNU ls
has a version sort built-in:
GNU ls内置了一个版本排序:
ls -lv
#3
2
If you have ruby(1.9.1+)
如果你有红宝石(1.9.1+)
ruby -e 'puts Dir["chap*pdf"].sort_by{|x|x[/\d+/].to_i}'
#4
1
Assuming that you want to rename the files so you don't have to keep sorting them later:
假设您要重命名文件,以便以后不必继续对它们进行排序:
for f in chap*-solutions.pdf; do num=`echo $f | grep -o "[0123456789]\+"`; two_num=`printf "%02d" $num`; mv $f chap$two_num-solutions.pdf; done
-
grep -o "[0123456789]+"
outputs the chapter number (one or two digits) -
printf
returns a string that contains the zero-padded number
grep -o“[0123456789] +”输出章节号(一位或两位数)
printf返回包含零填充数字的字符串