在Linux或Windows中更改目录中所有文件名和扩展名。

5

我有数千个文件,文件扩展名如下所示:

3_bedroom_villas_in_chennai.html__201308050010_
3_bedroom_villas_in_chennai.html__201308080012_
3_bedroom_villas_in_chennai.html__201308100012_
3_bedroom_villas_in_chennai.html__201308110034_ and so on.....

在一个目录中。我想将所有这些更改为以下内容:

3_bedroom_villas_in_chennai__201308050010_.html
3_bedroom_villas_in_chennai__201308080012_.html
3_bedroom_villas_in_chennai__201308100012_.html
3_bedroom_villas_in_chennai__201308110034_.html

当我在Windows尝试使用以下命令时

ren *.* *.html

我得到以下结果。
A duplicate file name exists, or the file
cannot be found.
A duplicate file name exists, or the file
cannot be found.
A duplicate file name exists, or the file
cannot be found.
A duplicate file name exists, or the file
cannot be found. and so on...

因为我知道它会试图将所有内容更改为单个文件名,例如:
3_bedroom_villas_in_chennai.html and so on... 

有没有在Windows或Linux上执行此操作的任何方法?
2个回答

8

Linux中:

ls | xargs -I % mv % %.html

ls 命令输出被管道传输到 xargs,并且 xargs 会用 ls 的输入替换所有 %(在 mv 之后)。

另外,如果您想递归地遍历所有子目录,可以使用:

find . -type f | xargs -I % mv % %.html

Windows 中:

for /r %x in (*) do ren "%x" *.txt

它起作用了..谢谢:) 我可以知道如何在Windows中做同样的事吗? - Venkateshwaran Selvaraj
1
@Venky 我添加了一个Windows解决方案。(在Windows 7中测试过) - julumme
@julumme,您能否解释一下ls | xargs -I % mv % %.html的每个部分的作用?我的意思是为什么要使用-I - Ranveer
另外,如果我想要重命名特定的扩展名怎么办? - Ranveer
@Ranveer,-I 定义了要替换的字符串。在这个例子中,我使用了 % 符号。它不一定是单个字符,但通常更容易理解正在发生的事情。因此,它意味着 xargs 将替换来自 ls(或 find)命令的所有后续出现的 % 符号的输入。例如,echo 1 | xargs -I % echo %%% 将在控制台上打印 111 - julumme

1
使用 renamer
$ renamer --regex --find '(.*).html(.*)' --replace '$1$2.html' *

适用于 Windows、Mac 和 Linux。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接