【问题标题】:Rename multiple directories with one domain name to another domain name?将一个域名的多个目录重命名为另一个域名?
【发布时间】:2012-01-31 20:02:24
【问题描述】:

我有许多目录,其名称中有一些短语(域名)。需要将该短语更改为另一个(另一个域名)。如何简单地做到这一点?下面的例子:

之前:

$ 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
...

【问题讨论】:

标签: linux bash shell


【解决方案1】:
$ 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

【讨论】:

  • 非常感谢!效果很好!
  • 你应该引用你的变量扩展,以防他的目录名称中有空格
  • 我没有得到管道到sh,去除回声不是更简单吗?
  • @Sorin:不是真的,向上箭头 + |sh + enter 比删除 echo 更快。这使执行的命令与您在屏幕上看到的内容非常接近。
  • @SiegeX:如果我这样做,你会是第一个指出包含'" 的文件名的问题,不是吗? ;-)
【解决方案2】:

你也可以使用perl自带的rename

$ rename 's/somewhere/elsewhere/' *

【讨论】:

    猜你喜欢
    • 2012-03-14
    • 2013-12-29
    • 2019-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-25
    • 1970-01-01
    • 2012-12-27
    相关资源
    最近更新 更多