基于查找批量重命名文件

bcr*_*awl 6 python bash perl

我有一个充满图像文件的文件夹,如

  • 1500000704_full.jpg
  • 1500000705_full.jpg
  • 1500000711_full.jpg
  • 1500000712_full.jpg
  • 1500000714_full.jpg
  • 1500000744_full.jpg
  • 1500000745_full.jpg
  • 1500000802_full.jpg
  • 1500000803_full.jpg

我需要根据文本文件中的查找重命名文件,文件文件包含如下条目:

  • SH103239 1500000704
  • SH103240 1500000705
  • SH103241 1500000711
  • SH103242 1500000712
  • SH103243 1500000714
  • SH103244 1500000744
  • SH103245 1500000745
  • SH103252 1500000802
  • SH103253 1500000803
  • SH103254 1500000804

所以,我希望重命名图像文件,

  • SH103239_full.jpg
  • SH103240_full.jpg
  • SH103241_full.jpg
  • SH103242_full.jpg
  • SH103243_full.jpg
  • SH103244_full.jpg
  • SH103245_full.jpg
  • SH103252_full.jpg
  • SH103253_full.jpg
  • SH103254_full.jpg

我怎样才能最轻松地完成这项工作?任何人都可以给我写一个快速命令或脚本,可以为我做这个吗?我有很多这些图像文件和手动更改是不可行的.

我在ubuntu上,但根据工具,如果需要,我可以切换到Windows.理想情况下,我希望在bash脚本中使用它,以便我可以学习更多或简单的perl或python.

谢谢

编辑:必须更改文件名

Wes*_*ley 7

这是一个简单的Python 2脚本来进行重命名.

#!/usr/bin/env python

import os

# A dict with keys being the old filenames and values being the new filenames
mapping = {}

# Read through the mapping file line-by-line and populate 'mapping'
with open('mapping.txt') as mapping_file:
    for line in mapping_file:
        # Split the line along whitespace
        # Note: this fails if your filenames have whitespace
        new_name, old_name = line.split()
        mapping[old_name] = new_name

suffix = '_full'

# List the files in the current directory
for filename in os.listdir('.'):
    root, extension = os.path.splitext(filename)
    if not root.endswith(suffix):
        # File doesn't end with this suffix; ignore it
        continue
    # Strip off the number of characters that make up suffix
    stripped_root = root[:-len(suffix)]
    if stripped_root in mapping:
        os.rename(filename, ''.join(mapping[stripped_root] + suffix + extension))
Run Code Online (Sandbox Code Playgroud)

脚本的各个部分都是硬编码的,实际上不应该这样.这些包括映射文件(mapping.txt)的名称和文件名后缀(_full).这些可能可以作为参数传递并使用解释sys.argv.

  • 不,**漂亮而简单**是`perl -lane'重命名("$ F [1] .jpg","$ F [0] .jpg")'mapping.txt`.啧! (3认同)

tch*_*ist 5

这将适用于您的问题:

#!/usr/bin/perl
while (<DATA>) {
    my($new, $old) = split;
    rename("$old.jpg", "$new.jpg")
        || die "can't rename "$old.jpg", "$new.jpg": $!";
}
__END__
SH103239 1500000704
SH103240 1500000705
SH103241 1500000711
SH103242 1500000712
SH103243 1500000714
SH103244 1500000744
SH103245 1500000745
SH103252 1500000802
SH103253 1500000803
SH103254 1500000804
Run Code Online (Sandbox Code Playgroud)

切换到ARGVDATA特定输入文件中读取行.

通常对于大规模重命名操作,我使用更像这样的东西:

#!/usr/bin/perl
# rename script by Larry Wall
#
# eg:
#      rename 's/\.orig$//'  *.orig
#      rename 'y/A-Z/a-z/ unless /^Make/'  *
#      rename '$_ .= ".bad"'  *.f
#      rename 'print "$_: "; s/foo/bar/ if <STDIN> =~ /^y/i'  *
#      find /tmp -name '*~' -print | rename 's/^(.+)~$/.#$1/'

($op = shift) || die "Usage: rename expr [files]\n";

chomp(@ARGV = <STDIN>) unless @ARGV;

for (@ARGV) {
    $was = $_;
    eval $op;
    die if $@;  # means eval `failed'
    rename($was,$_) unless $was eq $_;
}
Run Code Online (Sandbox Code Playgroud)

我有一个功能更全面的版本,但这应该足够了.