Bash 将以数字开头的文件重命名为两位数

Bash 将以数字开头的文件重命名为两位数,bash,ffmpeg,file-rename,Bash,Ffmpeg,File Rename,我有几个文件夹是有声读物。文件已编号,我想将它们转换为一个文件。 我使用以下脚本来转换它们: #!/bin/bash if [ ! -d mp3 ]; then mkdir -p mp3; fi; for f in ./*.flac; do echo "file '$f'" >> mylist.txt; done ffmpeg -f concat -safe 0 -i mylist.txt -b:a 320k mp3/title.mp

我有几个文件夹是有声读物。文件已编号,我想将它们转换为一个文件。 我使用以下脚本来转换它们:

    #!/bin/bash 
    if [ ! -d mp3 ]; then
    mkdir -p mp3;
    fi;
    for f in ./*.flac; do echo "file '$f'" >> mylist.txt; done
    ffmpeg -f concat -safe 0 -i mylist.txt -b:a 320k mp3/title.mp3
    [ -e mylist.txt ] && rm mylist.txt
我的问题是,我必须重命名前十个文件,因为它们的顺序不正确。这些文件被命名为1-Title、2-Title、3-Title等等。为了得到正确的顺序,我必须将它们重命名为01-Title,02-Title,…,09-Title。 如何使用bash脚本来实现这一点?此外,如果playlist.m3u文件可以相应地更改,那就太好了

谢谢你的帮助

@Cyrus发布了正确的链接来解决我的问题。 解决的脚本列表:

#!/bin/bash
if [ ! -d mp3 ]; then
mkdir -p mp3;
fi;
for f in ./*.flac; do echo "file '$f'" >> mylist2.txt; done
sort -V mylist2.txt >> mylist.txt
rm mylist2.txt
ffmpeg -f concat -safe 0 -i mylist.txt -b:a 320k mp3/title.mp3
[ -e mylist.txt ] && rm mylist.txt

您可以为此使用
sort-n
。我这样做:

~/SO $ l
total 8
-rw-rw-r-- 1 user user    0 Sep  9 14:49 12 - Title
-rw-rw-r-- 1 user user    0 Sep  9 14:49 1 - Title
-rw-rw-r-- 1 user user    0 Sep  9 14:49 22 - Title
-rw-rw-r-- 1 user user    0 Sep  9 14:49 2 - Title

~/SO $ ls | sort -n
1 - Title
2 - Title
12 - Title
22 - Title
为了向你证明我没有“作弊”,以下是我的别名:

alias l='ls -lp'
alias ls='ls --color=auto'
因此,您可以使用:

ls | sort -n | while read file
do
    echo $file
done

这里的回音只是为了表明处理文件的顺序确实尊重数值。

这可能会有所帮助:谢谢,我更改了脚本“for f in./*.flac;执行echo“file'$f'>>mylist2.txt;完成排序-V mylist2.txt>>mylist.txt rm mylist2.txt'感谢您的帮助。我用sort-n来解决我的问题。需要注意的是,这个变体只适用于不包含空格或换行符的文件名。不,我是用包含空格的文件名来解决的。@Flavorum1:如果它解决了您的问题,您可以接受答案左侧带有ckeck标记的答案:-)@Nic3500:请使用您的文件测试您的while read循环。