Welcome to WebmasterWorld Guest from

Forum Moderators: bakedjake

Message Too Old, No Replies

How can I change file extensions recursively?

10:13 am on May 25, 2009 (gmt 0)

Full Member

5+ Year Member

joined:June 1, 2007
votes: 0

I will give a little background on this I have setup a few processes to manage some source code I have written.

Basically I do the php dev in windows and I am wanting to automate some of the tedious tasks I have to do when putting out a new release.
I am no shell script guru and what I have working currently is many hours of hunting the net for solutions.

first of I sync the files and set the perms to 664

find . -type f -name "*.*" -exec chmod 664 {} \;

then I make sure all the newlines are set to unix

find . -type f -name "*.*" -exec dos2unix -o {} \;

At that point I make sure all of the files have the correct charset for UTF-8 excluding any image files and one ttf font that gets distributed

find . -type f \( ! -iname "*.ttf" ! -iname "*.gif" ! -iname "*.jpg" ! -iname "*.jpeg" ! -iname "*.png" \) -exec iconv -f ISO-8859-1 -t UTF-8 -o {}.utf8 {} \;

I could actually use a little help with that one since I think it could be made a little more readable?

then the last thing at this stage before an automated diff patch
is to rename all of the .utf8 files that have been created by dropping the .utf8 extension and using the previous extension for that file so .php.utf8 becomes .php again

this works but does not recurse into the directory structure so the files in the current directory are moved just fine but anything in a directory doesn't get touched.

for x in *.utf8; do mv $x `basename $x .utf8`; done;

Any help is appreciated.

Best Regards,

12:41 am on May 26, 2009 (gmt 0)

Full Member

5+ Year Member

joined:June 1, 2007
votes: 0

I got it worked out took some digging around but here is what I came up with I found out about using mmv.



# move into our working directory
cd /var/www/localhost/htdocs/dist/

# change the execute permissions from windows on our files
find . -type f -name "*.*" -exec chmod 664 {} \;
echo "Files have been modified to remove the execute bit"

# if line endings in our file are windows CR change to unix
find . -type f -name "*.*" -exec dos2unix -q -o {} \;
echo "Dos 2 Unix new line conversion has completed"

# delete any previous utf-8 files so we can write new ones
find . -type f \( -iname "*utf8*" \) -exec rm -f {} \;
echo "Old UTF-8 files have been deleted"

# get all files in our current working directory and make sure they are UTF-8 charset
find . -type f \( ! -iname "*.ttf" ! -iname "*.gif" ! -iname "*.jpg" ! -iname "*.jpeg" ! -iname "*.png" \) -exec iconv -f $CONVERT_FROM -t $CONVERT_TO -s -o {}$
echo "New UTF-8 files have been created"

# delete unconverted files so we can move the new files back later
find . -type f \( ! -iname "*.utf8" ! -iname "*.ttf" ! -iname "*.gif" ! -iname "*.jpg" ! -iname "*.jpeg" ! -iname "*.png" \) -exec rm {} \;
echo "Removal of original unconverted files is complete"

# rename all files converted to utf-8 with utf8 extension
mmv -m ";*.utf8" "#1#2"
# deal with .htaccess
mmv -m ";.*.utf8" "#1.#2"
echo "Dist Prep has completed"

I could still use a little help optimizing it if anyone feels like it.
Best Regards,

2:41 am on May 26, 2009 (gmt 0)

Full Member

5+ Year Member

joined:June 1, 2007
votes: 0

be careful the one line is wrong and will wipe out everything if you aren't careful replace it with this code

find . -type f \( ! -iname "*.ttf" ! -iname "*.gif" ! -iname "*.jpg" ! -iname "*.jpeg" ! -iname "*.png" \) -exec iconv -f $CONVERT_FROM -t $CONVERT_TO -s -o {}.utf8 {} \;

it is missing the final .utf8 {} \;


Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members