【问题标题】:Rename files that are beginning with a number to two digits [duplicate]将以数字开头的文件重命名为两位数[重复]
【发布时间】:2019-02-14 06:39:02
【问题描述】:

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

    #!/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

【问题讨论】:

  • 这可能会有所帮助:Sorting strings with numbers in Bash
  • 谢谢,我在 ./*.flac 中更改了 'for f 脚本;做 echo "file '$f'" >> mylist2.txt;完成排序 -V mylist2.txt >> mylist.txt rm mylist2.txt'

标签: bash ffmpeg file-rename


【解决方案1】:

您可以为此使用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

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

【讨论】:

  • 感谢您的帮助。我使用 sort -n 来解决我的问题。
  • 请务必注意,此变体仅适用于不包含空格或换行符的文件名。
  • 不,我使用包含空格的文件名。
  • @Flavorum1:如果它解决了您的问题,您可以接受答案左侧带有 ckeck 标记的答案 :-)
  • @Nic3500:请使用您的文件测试您的 while 读取循环。
猜你喜欢
  • 2015-12-11
  • 2014-06-26
  • 2018-10-19
  • 2012-12-06
  • 2018-06-18
  • 1970-01-01
  • 2020-11-29
  • 1970-01-01
  • 2020-03-19
相关资源
最近更新 更多