?
Solved

Changing extension and content for ALL files in a folder

Posted on 2006-10-26
1
Medium Priority
?
187 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month12 days, 18 hours left to enroll

777 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