Solved

Changing extension and content for ALL files in a folder

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
Linux FTP Error 5 46
When trying to install php-fpm on CentOS 7 - GPG error 2 98
Internal CA server 6 93
Edit smb.conf-file in Samba om a ReadyNAS 3 11
Daily system administration tasks often require administrators to connect remote systems. But allowing these remote systems to accept passwords makes these systems vulnerable to the risk of brute-force password guessing attacks. Furthermore there ar…
Fine Tune your automatic Updates for Ubuntu / Debian
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

733 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