正则表达式来批OS X终端重命名文件(Regex to batch rename files in

2019-06-24 14:39发布

我的方式来一批又用正则表达式,即重命名文件

s/123/onetwothree/g

我记得我可以用awk,并用正则表达式的sed但不可能弄清楚如何管他们在一起所需的输出。

Answer 1:

执行重命名操作的有效方式是,构建重命名的命令在一个sed管道并将它们馈送到壳体。

ls |
sed -n 's/\(.*\)\(123\)\(.*\)/mv "\1\2\3" "\1onetwothree\2"/p' |
sh


Answer 2:

您可以安装基于Perl重命名工具:

brew install rename

而不是仅仅使用它像:

rename 's/123/onetwothree/g' *

如果你想测试你的正则表达式不重命名的文件只需添加-n开关



Answer 3:

files = "*"
for f in $files; do
     newname=`echo "$f" | sed 's/123/onetwothree/g'`
     mv "$f" "$newname"
done


Answer 4:

我拿上一个友好的递归正则表达式的文件名重命名其默认只有模仿更换并显示结果的文件名是什么。

使用-w您满意的试运行结果,当实际编写的变化, -s来抑制显示不匹配的文件; -h--help将显示使用说明。

最简单的用法:

# replace all occurences of 'foo' with 'bar'
# "foo-foo.txt" >> "bar-bar.txt"
ren.py . 'foo' 'bar' -s

# only replace 'foo' at the beginning of the filename
# "foo-foo.txt" >> "bar-foo.txt"
ren.py . '^foo' 'bar' -s

匹配的组(如\1\2等)都支持太:

# rename "spam.txt" to "spam-spam-spam.py"
ren.py . '(.+)\.txt' '\1-\1-\1.py' -s 

# rename "12-lovely-spam.txt" to "lovely-spam-12.txt"
# (assuming two digits at the beginning and a 3 character extension 
ren.py . '^(\d{2})-(.+)\.(.{3})' '\2-\1.\3' -s

注意:不要忘了加-w当你测试的结果,并希望实际写的变化。

既与Python 2.x和Python的3.x的工作

#!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import print_function
import argparse
import os
import fnmatch
import sys
import shutil
import re


def rename_files(args):

    pattern_old = re.compile(args.search_for)

    for path, dirs, files in os.walk(os.path.abspath(args.root_folder)):

        for filename in fnmatch.filter(files, "*.*"):

            if pattern_old.findall(filename):
                new_name = pattern_old.sub(args.replace_with, filename)

                filepath_old = os.path.join(path, filename)
                filepath_new = os.path.join(path, new_name)

                if not new_name:
                    print('Replacement regex {} returns empty value! Skipping'.format(args.replace_with))
                    continue

                print(new_name)

                if args.write_changes:
                    shutil.move(filepath_old, filepath_new)
            else:
                if not args.suppress_non_matching:
                    print('Name [{}] does not match search regex [{}]'.format(filename, args.search_for))


if __name__ == '__main__':

    parser = argparse.ArgumentParser(description='Recursive file name renaming with regex support')

    parser.add_argument('root_folder',
                        help='Top folder for the replacement operation',
                        nargs='?',
                        action='store',
                        default='.')
    parser.add_argument('search_for',
                        help='string to search for',
                        action='store')
    parser.add_argument('replace_with',
                        help='string to replace with',
                        action='store')
    parser.add_argument('-w', '--write-changes',
                        action='store_true',
                        help='Write changes to files (otherwise just simulate the operation)',
                        default=False)
    parser.add_argument('-s', '--suppress-non-matching',
                        action='store_true',
                        help='Hide files that do not match',
                        default=False)

    args = parser.parse_args(sys.argv[1:])

    print(args)
    rename_files(args)


Answer 5:

Namechanger是超好听。 它支持搜索和替换正则表达式:



文章来源: Regex to batch rename files in OS X Terminal