简体   繁体   English

需要一个快速的bash脚本

[英]Need a quick bash script

I have about 100 directories all in the same parent directory that adhere to the naming convention [sitename].com. 我在同一个父目录中大约有100个目录,它们遵循命名约定[sitename] .com。 I want to rename them all [sitename].subdomain.com. 我想将它们全部重命名为[sitename] .subdomain.com。

Here's what I tried: 这是我尝试过的:

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

But it fails miserably. 但是它失败了。 Any ideas? 有任何想法吗?

Use rename(1) . 使用named(1)

rename .com .subdomain.com *.com

And if you have a perl rename instead of the normal one, this works: 如果您有一个perl重命名而不是普通的重命名 ,则可以使用:

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

Using bash: 使用bash:

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

The ${i%%.com} construct returns the value of i without the '.com' suffix. $ {i %%。com}构造返回不带“ .com”后缀的i的值。

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

What about: 关于什么:

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

See: http://blog.ivandemarino.me/2010/09/30/Rename-Subdirectories-in-a-Tree-the-Bash-way 参见: 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 <ivan.demarino@betfair.com>
#
# 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 <ivan.demarino@betfair.com>"
   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"

Try this: 尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM