【问题标题】:Batch rename files in sequence按顺序批量重命名文件
【发布时间】:2014-04-22 18:02:23
【问题描述】:

我在 shell 中重命名图像序列时遇到问题。

我有大约 300 个文件遵循模式 myimage_001.jpg,我想将其转换为 myimage.0001.jpg,其中数字随每个文件递增。

这是我尝试但没有成功的方法(-n 标志在实际应用之前显示结果):

rename -n 's/_/./g' *.jpg

【问题讨论】:

  • 你运行这个时发生了什么?
  • 这是什么系统?我的系统没有rename
  • 什么也没发生。我认为我的系统也没有它。我在发布前搜索时在 SO 上找到了该命令。我运行 Linux SL6。

标签: shell rename


【解决方案1】:

试试这个命令:

rename _ . *.jpg

示例:

> touch myimage_001.jpg
-rw-r--r-- 1 oracle oinstall      0 Mar 17 10:55 myimage_001.jpg
> rename  _ . *.jpg
> ll
-rw-r--r-- 1 oracle oinstall      0 Mar 17 10:55 myimage.001.jpg

加上一个 0 :

> touch myimage_001.jpg
-rw-r--r-- 1 oracle oinstall      0 Mar 17 10:55 myimage_001.jpg
> rename  _ .0 *.jpg
> ll
-rw-r--r-- 1 oracle oinstall      0 Mar 17 10:55 myimage.0001.jpg

语法很简单:

rename 'old' 'new' 'data-source'

【讨论】:

  • 这不适用于 OP 使用的 perl 重命名实现。
  • @AdrianFrühwirth - 可以解释原因吗? - 它总是对我有用!
  • 我不知道为什么,但这个有效的事件虽然它没有和 0。这不是问题,我可以使用 myimage.###.jpg 的文件。非常感谢
  • 有添加 0 的 waqy 吗?让最终文件是 myimage.0001.jpg 而不是 myimage.001.jpg ?
  • 不幸的是,我对 shell 还不够熟悉,无法想象 ;) 不过那行得通,谢谢!
【解决方案2】:

适合我吗?但是请注意,这不会像您的问题那样添加额外的前导零,这是一个错字吗?

$ find
.
./myimage_001.jpg
./myimage_007.jpg
./myimage_006.jpg
./myimage_002.jpg
./myimage_004.jpg
./myimage_009.jpg
./myimage_008.jpg
./myimage_003.jpg
./myimage_005.jpg

$ rename -n 's/_/./g' *.jpg
myimage_001.jpg renamed as myimage.001.jpg
myimage_002.jpg renamed as myimage.002.jpg
myimage_003.jpg renamed as myimage.003.jpg
myimage_004.jpg renamed as myimage.004.jpg
myimage_005.jpg renamed as myimage.005.jpg
myimage_006.jpg renamed as myimage.006.jpg
myimage_007.jpg renamed as myimage.007.jpg
myimage_008.jpg renamed as myimage.008.jpg
myimage_009.jpg renamed as myimage.009.jpg

$ rename 's/_/./g' *.jpg
$ find
.
./myimage.008.jpg
./myimage.007.jpg
./myimage.001.jpg
./myimage.003.jpg
./myimage.006.jpg
./myimage.005.jpg
./myimage.002.jpg
./myimage.009.jpg
./myimage.004.jpg

【讨论】:

    【解决方案3】:

    你可以试试这样的:

    for file in *.jpg; do 
        name="${file%_*}" 
        num="${file#*_}"
        num="${num%.*}"
        ext="${file#*.}"
        mv "$file" "$(printf "%s.%04d.%s" $name $num $ext)"
    done
    

    这给出:

    $ ls
    myimage_001.jpg  myimage_002.jpg
    

    $ for file in *.jpg; do 
      name="${file%_*}" 
      num="${file#*_}"
      num="${num%.*}"
      ext="${file#*.}"
      mv "$file" "$(printf "%s.%04d.%s" $name $num $ext)"
    done
    

    $ ls
    myimage.0001.jpg  myimage.0002.jpg
    

    【讨论】:

    • 尽管感谢这篇文章的一位用户,我找到了解决方案,但我还是很想尝试一下。我将如何更改命令以使我的最终文件为:myimage.####.jpg?
    • @titatom 我没有关注。此命令会将 myimage_001.jpg 重命名为 myimage.0001.jpg 您还想要什么吗?
    【解决方案4】:

    另一种选择:

    $ touch a.txt  b.txt  c.txt  d.txt  e.txt  f.txt
    $ ls
    a.txt  b.txt  c.txt  d.txt  e.txt  f.txt
    

    我们可以使用ls 结合sed + xargs 来实现您的目标。

    $ ls | sed -e "p;s/\.txt$/\.sql/"|xargs -n2 mv
    
    $ ls
    a.sql  b.sql  c.sql  d.sql  e.sql  f.sql
    

    有关详细信息,请参阅http://nixtip.wordpress.com/2010/10/20/using-xargs-to-rename-multiple-files/

    【讨论】:

      猜你喜欢
      • 2017-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-10
      • 2020-10-20
      • 2015-01-24
      • 2016-02-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多