Linux 中更改多个文件的名称

发布于 2024-11-28 15:29:33 字数 201 浏览 4 评论 0原文

我在 ubuntu 机器上有许多名为 a1.txt、b1.txt、c1、txt...的文件。

有没有快速的方法将所有文件名更改为a2.txt、b2.txt、c2.txt...?

特别是,我想替换部分名称字符串。例如,每个文件名都包含一个名为“apple”的字符串,我想将所有文件名中的“apple”替换为“pear”。

有什么命令或脚本吗?

I have a number of files with names a1.txt, b1.txt, c1,txt...on ubuntu machine.

Is there any quick way to change all file names to a2.txt, b2.txt, c2.txt...?

In particular, I'd like to replace part of the name string. For instance, every file name contains a string called "apple" and I want to replace "apple" with "pear" in all file names.

Any command or script?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

硬不硬你别怂 2024-12-05 15:29:33

无需任何额外的软件,您就可以:

for FILE in *1.txt; do mv "$FILE" $(echo "$FILE" | sed 's/1/2/'); done

without any extra software you can:

for FILE in *1.txt; do mv "$FILE" $(echo "$FILE" | sed 's/1/2/'); done
弃爱 2024-12-05 15:29:33
for f in {a..c}1.txt; do echo "$f" "${f/1/2}"; done

如果输出看起来正确,请将“echo”替换为“mv”。

我想用“linux”替换“apple”

for f in *apple*; do mv "$f" "${f/apple/linux}"; done

第一行中的大括号至少应该适用于 bash。

for f in {a..c}1.txt; do echo "$f" "${f/1/2}"; done

replace 'echo' with 'mv' if the output looks correct.

and I want to replace "apple" with "linux"

for f in *apple*; do mv "$f" "${f/apple/linux}"; done

The curly brackets in line 1 should work with bash at least.

瀞厅☆埖开 2024-12-05 15:29:33

以下命令将通过用 2 替换名称中第一次出现的 1 来重命名指定文件:

rename 1 2 *1.txt

The following command will rename the specified files by replacing the first occurrence of 1 in their name by 2:

rename 1 2 *1.txt
白日梦 2024-12-05 15:29:33
ls *1.txt | perl -ne 'chomp; $x = $_; $x =~ s/1/2/; rename $_, $x;'
ls *1.txt | perl -ne 'chomp; $x = $_; $x =~ s/1/2/; rename $_, $x;'
雄赳赳气昂昂 2024-12-05 15:29:33

这是另一个对我有用的选项(按照上面的示例),适用于不同子目录中的文件

for FILE in $(find . -name *1.txt); do mv "$FILE" "${FILE/1/2}"; done;

Here's another option that worked for me (following the examples above) for files in different subdirectories

for FILE in $(find . -name *1.txt); do mv "$FILE" "${FILE/1/2}"; done;
最美不过初阳 2024-12-05 15:29:33

像这样的事情应该有效:

for i in *1.txt; do
    name=$(echo $i | cut -b1)
    mv $i ${name}2.txt
done

修改以满足您的需求。

Something like this should work:

for i in *1.txt; do
    name=$(echo $i | cut -b1)
    mv $i ${name}2.txt
done

Modify to suit your needs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文