2012-01-31 47 views
4

我有许多目录中有一些短语(域名)的名字。需要将该短语更改为另一个(另一个域名)。如何做到这一点?下面的示例:将带有一个域名的多个目录重命名为另一个域名?

前:

$ ls /var/www 
drwxr-x--- 12 apache apache 4096 Dec 16 10:28 somewhere.com 
drwxr-xr-x 3 apache apache 4096 Jan 28 2011 maven.somewhere.com 
drwxr-x--- 6 apache apache 4096 Feb 24 2010 mini.somewhere.com 
drwxr-x--- 3 apache apache 4096 Jul 16 2010 ml.somewhere.com 
... 

如果输出看起来确定后

$ ls /var/www 
drwxr-x--- 12 apache apache 4096 Dec 16 10:28 elsewhere.com 
drwxr-xr-x 3 apache apache 4096 Jan 28 2011 maven.elsewhere.com 
drwxr-x--- 6 apache apache 4096 Feb 24 2010 mini.elsewhere.com 
drwxr-x--- 3 apache apache 4096 Jul 16 2010 ml.elsewhere.com 
... 
+0

重复[文件夹中的壳/击快捷方式文件重命名散装(Linux)的](的http://stackoverflow.com/questions/8416990/shell-bash-shortcut-for-bulk-renaming- of-files-in-a-folderlinux) – Sorin 2012-01-31 20:02:10

回答

15
$ cd /var/www 
$ for i in *; do echo mv $i ${i/somewhere/elsewhere}; done 

$ for i in *; do echo mv $i ${i/somewhere/elsewhere}; done |sh 
+0

非常感谢!很棒! – marioosh 2012-01-31 10:54:16

+0

你应该引用你的变量扩展,以防他的名字中有空格 – SiegeX 2012-01-31 14:29:10

+0

我没有弄到管道sh,移除回声不是更简单吗? – Sorin 2012-01-31 20:03:07

0

您还可以使用rename自带的perl的。

$ rename 's/somewhere/elsewhere/' *