Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Changing extension and content for ALL files in a folder

Posted on 2006-10-26
1
Medium Priority
?
188 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 45

Accepted Solution

by:
sunnycoder earned 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
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.
Suggested Courses
Course of the Month10 days, 14 hours left to enroll

618 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