批量重命名与模式不匹配的文件


2

我没有编程经验,大多使用单行或有时更多来完成工作。我有问题批量重命名与特定模式不匹配的文件。

目录中的示例文件名:

Meeting_Packages.pdf
13_textfile0
19_textfile0
23_textfile0
29_textfile0
33_textfile1
45_textfile0
5_textfile3
Membership.pdf
13_textfile1
19_textfile1
23_textfile1
29_textfile1
34_textfile0
46_textfile0
6_textfile0
xyz2009.pdf
13_textfile2
19_textfile2
23_textfile2
29_textfile2
34_textfile1
47_textfile0
6_textfile1
meeting.ics

我想重命名文件,例如 Meeting_Packages.pdfMembership.pdfmeeting.icsxyz2009.pdf 到他们来的文件(输入文件)。实际上它是输出 ripmime 用邮件作为 xx_textfilex 和其他人是附件。我想将附件命名为原始输入文件

我的代码:

#!/bin/bash

FILES=*.mime
for f in $FILES
do
    echo "Processing $f"
    #rip mails into attachments and text files also add a prefix to text files
    ripmime -i $f -d ~/test/ripmime --prefix 
    #Remove white spaces from files 
    rename 's/ /_/g' ~/test/ripmime/*
    #rename attachments as original input files
    rename 's/\[^0-9]/'$f/ ~/test/ripmime/* 
done

我的问题是最后一次重命名行,我尝试过滤除了以外的文件 xx_textfilex 并重命名。我尝试了不同的正则表达式,但不能这样做。我可以通过以下方式选择并重命名文本文件:

rename 's/textfiles/'$f/ ~/test/ripmime/*

但我需要反过来并重命名文件以外的文件文件。

我怎样才能做到这一点?


也许mmv可以做到这一点。 ss64.com/bash/mmv.html
stijn

我可以看到它是如何完成的,但是你需要提供一些样本结果文件名。 怎么样 如果在运行命令后重命名当前示例中的PDF文件?我发现这一部分目前是模棱两可的:如果它们应该像你说的那样重命名为输入文件,这将导致多个文件具有相同的文件名,这是不可能的。
Daniel Andersson

看起来你无论如何都在临时目录中工作;你考虑过移动你想要保留的文件,然后使用类似的东西 rename 重命名剩余的文件,然后将第一组移回原来的位置?
a CVn

Answers:


1

你可以结合起来 lsgrep -v

ls | grep -v ".*textfile.*" | while read filename; do
  # rename $filename to something
done

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.