id3v2 在命令行中递归使用?

id3v2 在命令行中递归使用?

试图清理我的 id3 标签,并且喜欢在命令行上使用 id3v2 ——但我一直只将它与 *.mp3 通配符一起使用,并且想探索是否有一种方法可以递归使用它,以便我可以批量处理所有我的 MP3。似乎没有递归使用它的选项。

我很确定你们所有很棒的命令行人员都知道一种很好的方法来做到这一点,但我仍在学习它的方法......

这是我为暴力破解而设置的别名命令:

id3v2 --remove-frame "COMM" *.mp3
id3v2 --remove-frame "PRIV" *.mp3
id3v2 -s *.mp3

那么 - 有没有办法递归地执行此操作,以便我可以在音乐文件夹的根目录下运行它?重点是:包括其他音频文件类型并将上述所有三个命令折叠为一个 uber 命令(我可以使用;命令之间的命令来完成此操作......对吗?)

答案1

您应该能够在一行中完成此操作,如下所示:

find . -name '*.mp3' -execdir id3v2 --remove-frame "COMM" '{}' \; -execdir id3v2 --remove-frame "PRIV" '{}' \; -execdir id3v2 -s '{}' \;

{} 将替换当前的文件名匹配项。将它们放在引号 ( '') 中可以保护它们免受 shell 的影响。运行-execdir直到遇到分号,但分号 ( ;) 需要从 shell 中转义,因此使用反斜杠 ( \)。这一切都在find 联机帮助页:

-exec command ;
      Execute  command;  true  if 0 status is returned.  All following
      arguments to find are taken to be arguments to the command until
      an  argument  consisting of `;' is encountered.  The string `{}'
      is replaced by the current file name being processed  everywhere
      it occurs in the arguments to the command, not just in arguments
      where it is alone, as in some versions of find. 
-execdir command {} +
      Like -exec, but the specified command is run from the  subdirec‐
      tory  containing  the  matched  file,  which is not normally the
      directory in which you started find.  This a  much  more  secure
      method  for invoking commands...

因为你听起来像是对此有点陌生,所以需要注意:与往常一样,对于复杂的 shell 命令,请谨慎运行它们,并首先在测试目录中尝试,以确保您了解将要发生的情况。拥有权利的同时也被赋予了重大的责任!

答案2

一个快速的解决方案是循环遍历所有子文件夹,并处理其中的所有文件:

find . -type d | while IFS= read -r d; do
  id3v2 --remove-frame "COMM" "${d}"/*.mp3
  id3v2 --remove-frame "PRIV" "${d}"/*.mp3
  id3v2 -s "${d}"/*.mp3
done

答案3

我没有使用,id3v2所以我不能确定,但​​是您很有可能可以将所有命令合并为一个:

id3v2 --remove-frame "COMM" --remove-frame "PRIV" -s *.mp3

要在子目录中的 MP3 文件中运行此命令,请运行

id3v2 --remove-frame "COMM" --remove-frame "PRIV" -s **/*.mp3

**/*.mp3递归匹配.mp3当前目录及其子目录中的文件。如果您的 shell 是 zsh,则**/可以开箱即用。如果您的 shell 是 bash ≥4,您需要shopt -s globstar先运行(将此行放入您的~/.bashrc)。在ksh中,您需要运行set -o globstar(将其放入~/.kshrc)。如果您有另一个 shell,或者此尝试失败并显示一条消息告诉您命令行太大,则必须使用find下面的方法(使用其他答案中给出的变体)。

递归地作用于目录及其子目录中的文件的更复杂、但更灵活和更便携的方法是命令find

find . -type f -name '*.mp3' -exec id3v2 --remove-frame "COMM" --remove-frame "PRIV" -s {} +

之后的命令-exec执行时,{} +末尾的位被匹配文件的路径替换。如果需要运行多个id3v2命令,请使用多个-exec指令:

find . -type f -name '*.mp3' -exec id3v2 --remove-frame "COMM" {} + -exec id3v2 --remove-frame "PRIV" {} + -exec id3v2 -s {} +

相关内容