简体   繁体   English

按照模式在 Linux 中重命名多个文件

[英]Rename multiple files in Linux following pattern

I have a directory with some files that are messed up:我有一个目录,里面有一些乱七八糟的文件:

1243435.pdf
1222424.pdf
643234234.pdf
3423436pdf
45435435pdf
343432.pdf.meta
345232.pdf.meta
3434311pdf.meta
12121234pdf.meta

Files with extension .pdf or .pdf.meta are OK and should stay the same, but how can I rename only the ones that don't have .扩展名为 .pdf 或 .pdf.meta 的文件是可以的,应该保持不变,但我怎样才能只重命名那些没有 .pdf 的文件。 in front of pdf?在pdf前面?

45435435pdf > 45435435.pdf
12121234pdf.meta > 12121234.pdf.meta

Thank you for the help!感谢您的帮助!

You can match all the files with *[^.]pdf , ie anything followed by a non-dot followed by pdf .您可以将所有文件与*[^.]pdf匹配,即任何后跟非点后跟pdf的文件。 To extract the prefix, use parameter expansion:要提取前缀,请使用参数扩展:

#! /bin/bash
for file in *[^.]pdf ; do
    prefix=${file%pdf}
    mv "$file" "$prefix".pdf
done

我认为您可以找到更好的方法。

find . -type f -regex '\.\/[^.]+pdf\.?[meta]*?' -exec bash -c 'mv "$1" "${1/\pdf/\.pdf}"' -- {} \;

First第一的

Save your files in a list eg list.txt将您的文件保存在列表中,例如list.txt

Second第二

Try a dry-run to see it will be correct试一下试运行看看是否正确

perl -lne 's/(?<=\d)(?=pdf)/./g && print' list.txt

Which for your input, has this output你的输入,有这个输出

3423436.pdf
45435435.pdf
3434311.pdf.meta
12121234.pdf.meta

Thrid第三

Run it运行

perl -lne '($old=$_) && s/(?<=\d)(?=pdf)/./g && rename($old,$_)' list.txt

If you have lots of files, this is the fastest way.如果您有很多文件,这是最快的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM