Solved

Changing extension and content for ALL files in a folder

Posted on 2006-10-26
1
181 Views
Last Modified: 2010-04-20
Hello,

I use the following to change ALL extensions from .cgi to .pl.

It changes the filenames, AND it also changes the text inside of the files to reflect the changes.

It works awesome, but it ONLY adjusts the content of the .cgi files.  Other files, like .html files and .js files are NOT changed to reflect the filename changes.

So, style.css and main.htm still point to the OLD extension, not the new one.

How can I change the extension name then change the content of ALL files to reflect the new extensions?

Thanks!






        # Thanks, ravenpl
        # http://www.experts-exchange.com/Operating_Systems/Linux/Q_22025115.html
        cd destination/dir
        find -type f -name '*.cgi' | while read file; do
         BASE=$(basename "$file" .cgi)
         DIR=$(dirname "$file")
         DEST="$DIR/$BASE.pl"
         [ -e "$DEST" ] && { echo "$DEST already exists, terminating"; exit 1; }
         cat "$file" | sed "s/\.cgi/\.pl/g" > "$DIR/$BASE.pl"
        done

        #if verified all done fine
        find -type f -name '*.cgi' -exec rm -fv {} \;

0
Comment
Question by:hankknight
1 Comment
 
LVL 45

Accepted Solution

by:
sunnycoder earned 500 total points
ID: 17810761
Hi hankknight,

#change extension
find /top/dir -type f -name "*.cgi" | while read fname
do
       filen=`echo $fname | sed 's/\(.*\)\.cgi/\1/'`
       mv -f $fname $fname.pl
done

#change file contents
find /top/dir -type f | while read fname
do
        sed 's/\.cgi/\.pl/g' $fname > $fname.new
        mv -f $fname.new $fname
done

Try the scripts on dummy data before unleashing them on real data

Cheers!
sunnycoder
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Currently, there is not an RPM package available under the RHEL/Fedora/CentOS distributions that gives you a quick and easy way to allow PHP to interface with Oracle. As a result, I have included a set of instructions on how to do this with minimal …
rdate is a Linux command and the network time protocol for immediate date and time setup from another machine. The clocks are synchronized by entering rdate with the -s switch (command without switch just checks the time but does not set anything). …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now