2011-01-24 83 views
0

我有大约100个目录都在同一个父目录中,遵守命名约定[sitename] .com。我想将它们重命名为[sitename] .subdomain.com。需要一个快速bash脚本

这里是我的尝试:

for FILE in `ls | sed 's/.com//' | xargs`;mv $FILE.com $FILE.subdomain.com; 

但悲惨的失败了。有任何想法吗?

回答

2

使用bash:

for i in * 
do 
    mv $i ${i%%.com}.subdomain.com 
done 

的$ {。我%% COM}构建回报我没有'.com'后缀的价值。

0
find . -name '*.com' -type d -maxdepth 1 \ 
| while read site; do 
    mv "${site}" "${site%.com}.subdomain.com" 
    done 
0

什么:

ls | 
grep -Fv '.subdomain.com' | 
while read FILE; do 
    f=`basename "$FILE" .com` 
    mv $f.com $f.subdomain.com 
done 
7

使用rename(1)

rename .com .subdomain.com *.com 

如果你有一个perl rename,而不是正常的,这个工程:

rename s/\\.com$/.subdomain.com/ *.com 
+0

我觉得有一个在.com.`的`结束了加时赛。 – 2011-01-24 17:49:11

+0

是的,修正了这个问题。 – Tonttu 2011-01-24 17:51:48

+0

我通常会用mmv来解决这个问题。感谢让我意识到另一个工具。 – 2011-01-24 18:09:52

0

参见:http://blog.ivandemarino.me/2010/09/30/Rename-Subdirectories-in-a-Tree-the-Bash-way

#!/bin/bash 
# Simple Bash script to recursively rename Subdirectories in a Tree. 
# Author: Ivan De Marino <[email protected]> 
# 
# Usage: 
# rename_subdirs.sh <starting directory> <new dir name> <old dir name> 

usage() { 
    echo "Simple Bash script to recursively rename Subdirectories in a Tree." 
    echo "Author: Ivan De Marino <[email protected]>" 
    echo 
    echo "Usage:" 
    echo " rename_subdirs.sh <starting directory> <old dir name> <new dir name>" 

    exit 1 
} 

[ "$#" -eq 3 ] || usage 

recursive() 
{ 
    cd "$1" 
    for dir in * 
    do 
     if [ -d "$dir" ]; then 
     echo "Directory found: '$dir'" 
     (recursive "$dir" "$2" "$3") 
     if [ "$dir" == "$2" ]; then 
      echo "Renaming '$2' in '$3'" 
      mv "$2" "$3" 
     fi; 
     fi; 
    done 
} 

recursive "$1" "$2" "$3" 
-1

试试这个:

for FILE in `ls -d *.com`; do 
    FNAME=`echo $FILE | sed 's/\.com//'`; 
    `mv $FILE $FNAME.subdomain.com`; 
done