Solved

Changing extension and content for ALL files in a folder

Posted on 2006-10-26
1
183 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Best way to virtualise a remote Linux server 2 66
Coding C# in Linux 8 62
awk to variable in bash 2 41
Bash script - Exit out of choice loop 2 27
I am a long time windows user and for me it is normal to have spaces in directory and file names. Changing to Linux I found myself frustrated when I moved my windows data over to my new Linux computer. The problem occurs when at the command line.…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

776 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