bash使用序列号批量重命名文件夹和子文件夹中的文件 [英] bash mass rename files in folders and subfolders with sequential number

查看:102
本文介绍了bash使用序列号批量重命名文件夹和子文件夹中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个执行以下操作的 bash 脚本:对于文件夹及其子文件夹中存在的每个特定类型的文件,它会在前面加上一个序列号(4 位数字),后跟一个分隔符 (-)

I need a bash script which does the following: for every file of a certain type present in a folder and in its sub folders it prepends a sequential number (4 digits) followed by a separator (-)

例如我有:

  /Queen/(1986) A Kind of Magic/01.One vision.mp3
  /Queen/(1986) A Kind of Magic/02.A kind of magic.mp3
  /Queen/(1986) A Kind of Magic/cover.jpg
  /Queen/(1991) Innuendo/01.Innuendo.mp3
  /Queen/(1991) Innuendo/02.Headlong.mp3
  /Queen/(1991) Innuendo/cover.jpg
  /Queen/(1991) Innuendo/booklet.pdf

我希望在脚本中具有以下内容:

I want that at the of the script the following:

  /Queen/(1986) A Kind of Magic/0001-01.One vision.mp3
  /Queen/(1986) A Kind of Magic/0002-02.A kind of magic.mp3
  /Queen/(1986) A Kind of Magic/cover.jpg
  /Queen/(1991) Innuendo/0003-01.Innuendo.mp3
  /Queen/(1991) Innuendo/0004-02.Headlong.mp3
  /Queen/(1991) Innuendo/cover.jpg
  /Queen/(1991) Innuendo/booklet.pdf

请注意,文件名中有空格,文件夹名中有括号!我怎样才能做到这一点?我尝试使用 mac automator 但没有成功!:(

Please note I have spaces in the filenames and parenthesis in the folder names! How can I do that? I tried with mac automator but no success! :(

谢谢

推荐答案

创建以下 bash 脚本并在您的音乐收藏的基本目录中执行它.它将处理包含空格、括号、单引号和双引号、其他奇怪字符的文件和目录名称,但 处理包含换行符或真的 奇怪字符的名称.您可以自定义它,为 start_dir 提供不同的值,或更改格式规范 %s/%04u-%s,目前包含 %s>(目录名)%04u(编号索引,格式为用零填充的四位整数)和 %s(文件名),由/-.

Create the following bash script and execute it at the base directory of your music collection. It will work with file and directory names containing spaces, parentheses, single and double quotes, other weird characters, but not with names containing newlines or really weird characters. You can customize it giving a different value for start_dir, or changing the format specification %s/%04u-%s, which presently contains %s (the directory name) %04u (the numbering index, formatted as a zero-padded four-digit integer), and %s (the file name), separated by / and -.

#!/bin/bash
start_dir="."
find "$start_dir" -name '*.mp3' -type f \
  |sort \
  |while read name; do 
     ((i++))
     mv -i "$name" \
       "$(printf '%s/%04u-%s' "$(dirname "$name")" $((i)) "$(basename "$name")")"
   done

备注:

Socowi 的回答与我的非常相似.不同之处在于(1)他需要bash 4.0,Mac上默认没有安装.我不.(2) 他的脚本将使用包含换行符的文件名(非常罕见的情况).我的不会.(3)如果所有 mp3 文件(包括子目录)的名称总长度超过 262144 字节,他的脚本将失败;我会说如果您有大约 6000 个以上的文件,就会发生这种情况.我的可以处理任意数量的文件.

Socowi’s answer is very similar to mine. The differences are (1) He requires bash 4.0, which is not installed on Macs by default. I don’t. (2) His script will work with filenames containing newlines (a very rare occasion). Mine won’t. (3) His script will fail if the total length of the names of all your mp3 files (including subdirectories) is more than 262144 bytes; I’d say this would happen if you have more than approximately 6000 files. Mine will work with any number of files.

(给热爱 mapfile 的 bash 从业者注意:我知道使用 mapfile 可能是一种无需显式创建 $i,但是 (i) 不需要 mapfile 会创建的巨大数组,并且 (ii) 在解析传递给 mv 回调.)

(Note for mapfile-loving bash practitioners: I know that using mapfile might be a smart way to generate the indexes without explicitly creating $i, but (i) there is no need for the huge array mapfile would create and (ii) I would need a variable anyway when parsing the command line passed to the mv callback.)

这篇关于bash使用序列号批量重命名文件夹和子文件夹中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆