查找所有 [文件名].mp4 并重命名 [文件名].audio

查找所有 [文件名].mp4 并重命名 [文件名].audio

所以我有一个脚本,使用文件中的音频将两部电影添加在一起$1.audio。我想做的是将目录中的任何文件重命名为:

*.mp4 

到:

*.audio 

保留原始文件名。

答案1

您可以使用该rename命令。它不可移植,但在不同的发行版中以不同的形式存在。

在 CentOS/RHEL 和可能的 Fedora 中:

rename .mp4 .audio *.mp4

应该做。从man renameCentOS 6 开始:

SYNOPSIS
       rename from to file...
       rename -V

DESCRIPTION
       rename  will  rename  the specified files by replacing the first occur-
       rence of from in their name by to.

在 Ubuntu 和可能的任何 Debian 变体中:

rename 's/\.mp4$/.audio/' *.mp4

应该这样做。从man renameUbuntu 14.04 开始:

SYNOPSIS
       rename [ -v ] [ -n ] [ -f ] perlexpr [ files ]

DESCRIPTION
       "rename" renames the filenames supplied according to the rule specified
       as the first argument.  The perlexpr argument is a Perl expression
       which is expected to modify the $_ string in Perl for at least some of
       the filenames specified.  If a given filename is not modified by the
       expression, it will not be renamed.  If no filenames are given on the
       command line, filenames will be read via standard input.

       For example, to rename all files matching "*.bak" to strip the
       extension, you might say

               rename 's/\.bak$//' *.bak

答案2

这是一个快速且可移植的解决方案,仍然可以处理奇怪命名的文件:

find . -name "*.mp4" -exec sh -c 'for i do mv -- "$i" "${i%.mp4}.audio"; done' sh {} +

答案3

使用这个for循环:

for f in *; do
  [ -f "$f" ] && mv -v -- "$f" "${f%.mp3}.audio"
done
  • for i in *循环遍历当前工作目录中的所有文件和目录(点文件除外)并将当前处理的文件存储在$f
    • [ -f "$f" ]检查它是否是常规文件
    • mv -v重命名文件(--文件名不会被错误地解释为参数)
    • ${f%.mp3}.audio删除.mp3扩展名并添加.audio扩展名 (参数扩展

答案4

您可以使用

for file in `ls *.mp4`; { mv $file `echo $file | sed 's/.mp4/.audio/g'`; }

相关内容