I was looking for a linux command line one-liner to rename a bunch of files in one shot.
我正在寻找一个linux命令行one-liner来一次性重命名一堆文件。
pattern1.a pattern1.b pattern1.c ...
Once the command is executed I should get
一旦命令执行,我应该得到
pattern2.a pattern2.b pattern2.c ...
3 个解决方案
#1
12
for i in pattern1.*; do mv -- "$i" "${i/pattern1/pattern2}"; done
Before you run it, stick an echo
in front of the mv
to see what it would do.
在你运行它之前,在mv前面贴一个回声来看看它会做什么。
#2
11
If you happen to be using Linux, you may also have a perl script at /usr/bin/rename which cane rename files based on more complex patterns than shell globbing permits.
如果您正在使用Linux,您可能还有/ usr / bin / rename中的perl脚本,根据比shell globbing许可更复杂的模式重命名文件。
The /usr/bin/rename on one of my systems is documented here. It could be used like this:
这里记录了我的一个系统上的/ usr / bin / rename。它可以像这样使用:
rename "s/pattern1/pattern2/" pattern1.*
A number of other Linux environments seem to have a different rename
that might be used like this:
许多其他Linux环境似乎都有不同的重命名,可能会像这样使用:
rename pattern1 pattern2 pattern1.*
Check man rename
on your system for details.
检查系统上的人员重命名以获取详细信息。
#3
1
Plenty of ways to skin this cat. If you'd prefer your pattern to be a regex rather than a fileglob, and you'd like to do the change recursively you could use something like this:
有很多方法可以给这只猫上皮。如果你更喜欢你的模式是一个正则表达式而不是一个fileglob,并且你想要递归地进行更改,你可以使用这样的东西:
find . -print | sed -ne '/^\.\/pattern1\(\..*\)/s//mv "&" "pattern2\1"/p'
As Kerrek suggested with his answer, this one first shows you what it would do. Pipe the output through a shell (i.e. add | sh
to the end) once you're comfortable with the commands.
正如克雷克在他的回答中提出的那样,这个首先向你展示了它的作用。一旦您对命令感到满意,就将输出通过shell(即添加到最后)进行管道输送。
This works for me:
这对我有用:
[ghoti@pc ~]$ ls -l foo.*
-rw-r--r-- 1 ghoti wheel 0 Mar 26 13:59 foo.php
-rw-r--r-- 1 ghoti wheel 0 Mar 26 13:59 foo.txt
[ghoti@pc ~]$ find . -print | sed -ne '/^\.\/foo\(\..*\)/s//mv "&" "bar\1"/p'
mv "./foo.txt" "bar.txt"
mv "./foo.php" "bar.php"
[ghoti@pc ~]$ find . -print | sed -ne '/^\.\/foo\(\..*\)/s//mv "&" "bar\1"/p' | sh
[ghoti@pc ~]$ ls -l foo.* bar.*
ls: foo.*: No such file or directory
-rw-r--r-- 1 ghoti wheel 0 Mar 26 13:59 bar.php
-rw-r--r-- 1 ghoti wheel 0 Mar 26 13:59 bar.txt
[ghoti@pc ~]$
#1
12
for i in pattern1.*; do mv -- "$i" "${i/pattern1/pattern2}"; done
Before you run it, stick an echo
in front of the mv
to see what it would do.
在你运行它之前,在mv前面贴一个回声来看看它会做什么。
#2
11
If you happen to be using Linux, you may also have a perl script at /usr/bin/rename which cane rename files based on more complex patterns than shell globbing permits.
如果您正在使用Linux,您可能还有/ usr / bin / rename中的perl脚本,根据比shell globbing许可更复杂的模式重命名文件。
The /usr/bin/rename on one of my systems is documented here. It could be used like this:
这里记录了我的一个系统上的/ usr / bin / rename。它可以像这样使用:
rename "s/pattern1/pattern2/" pattern1.*
A number of other Linux environments seem to have a different rename
that might be used like this:
许多其他Linux环境似乎都有不同的重命名,可能会像这样使用:
rename pattern1 pattern2 pattern1.*
Check man rename
on your system for details.
检查系统上的人员重命名以获取详细信息。
#3
1
Plenty of ways to skin this cat. If you'd prefer your pattern to be a regex rather than a fileglob, and you'd like to do the change recursively you could use something like this:
有很多方法可以给这只猫上皮。如果你更喜欢你的模式是一个正则表达式而不是一个fileglob,并且你想要递归地进行更改,你可以使用这样的东西:
find . -print | sed -ne '/^\.\/pattern1\(\..*\)/s//mv "&" "pattern2\1"/p'
As Kerrek suggested with his answer, this one first shows you what it would do. Pipe the output through a shell (i.e. add | sh
to the end) once you're comfortable with the commands.
正如克雷克在他的回答中提出的那样,这个首先向你展示了它的作用。一旦您对命令感到满意,就将输出通过shell(即添加到最后)进行管道输送。
This works for me:
这对我有用:
[ghoti@pc ~]$ ls -l foo.*
-rw-r--r-- 1 ghoti wheel 0 Mar 26 13:59 foo.php
-rw-r--r-- 1 ghoti wheel 0 Mar 26 13:59 foo.txt
[ghoti@pc ~]$ find . -print | sed -ne '/^\.\/foo\(\..*\)/s//mv "&" "bar\1"/p'
mv "./foo.txt" "bar.txt"
mv "./foo.php" "bar.php"
[ghoti@pc ~]$ find . -print | sed -ne '/^\.\/foo\(\..*\)/s//mv "&" "bar\1"/p' | sh
[ghoti@pc ~]$ ls -l foo.* bar.*
ls: foo.*: No such file or directory
-rw-r--r-- 1 ghoti wheel 0 Mar 26 13:59 bar.php
-rw-r--r-- 1 ghoti wheel 0 Mar 26 13:59 bar.txt
[ghoti@pc ~]$