0

I have about 100 directories all in the same parent directory that adhere to the naming convention [sitename].com. I want to rename them all [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?

6 Answers 6

7

Use rename(1).

rename .com .subdomain.com *.com

And if you have a perl rename instead of the normal one, this works:

rename s/\\.com$/.subdomain.com/ *.com
Sign up to request clarification or add additional context in comments.

2 Comments

I think there's an extra period at the end of .com..
I usually reach for mmv to solve that problem. Thanks for making me aware of another tool for it.
2

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

Comments

0

What about:

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

Comments

0

See: 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"

Comments

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

Comments

-1

Try this:

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

1 Comment

-1 for parsing output of ls and executing mv in an unnecessary subshell.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.