The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
grep unix.com with google



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-09-2008
Registered User
 

Join Date: Apr 2008
Posts: 3
Recursicely search and rename file extension

Greetings to all!!

I have one root folder containing several other folders inside it. This tree structure is deep. And the files are of similar extension.

I need to start at the top level and recursively search and rename all the files with say .a extension to .b .

This is the code to rename, but then I need to reach out each folder.
for i in *.vw ; do mv $i `echo $i | sed 's/vw/view/'` ; done

Could anybody help me by providing the outer for loop to traverse all the folders in between?
  #2 (permalink)  
Old 04-09-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
man find or search these forums for examples; there are plenty.

You can avoid the sed call, there's a simple substitution operator built into the shell itself.


Code:
mv "$i" "${i%.wv}.view"

Also note the use of double quotes.
  #3 (permalink)  
Old 04-09-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
In relation to http://www.unix.com/shell-programmin...-new-post.html just pipe the output of find to sed and trim away everything except the extension.
  #4 (permalink)  
Old 04-09-2008
rubin's Avatar
rubin rubin is offline Forum Advisor  
Registered User
 

Join Date: Nov 2007
Posts: 321
Find utility will go recursively down the line ...


Code:
for i in `find . -name "*.vw"`
 do
      mv "$i" "${i%.vw}.view"
 done

  #5 (permalink)  
Old 04-09-2008
Registered User
 

Join Date: Sep 2006
Posts: 2,604
Quote:
Originally Posted by rubin View Post
Find utility will go recursively down the line ...


Code:
for i in `find . -name "*.vw"`
 do
      mv "$i" "${i%.vw}.view"
 done
except that this method is prone to error with spaces in file names. use while loop instead

Code:
find . -name "*.mp3" | while read line; 
  do 
        mv "$line" .......
 done

  #6 (permalink)  
Old 04-10-2008
Registered User
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,125
Hi,

Below can list out all the different extension names in the current directory.
Hope it can help you a little.


Code:
getDel()
{
	cd $1
	for i in *
	do
		if [ $i != "*" ]
		then
			if [ -d $i ]
			then
				getDel $i
			else
				ls -l $i
			fi
		fi
	done
	cd ..
}

for i in *
do
if [ -d $i ]
then
	getDel $i
else
	ls -l $i
fi
done  > temp

awk '{
if (index($9,".")!=0)
{
	t=substr($9,index($9,".")+1,length($9)-index($9,"."))
	a[t]=1
}
}
END{
for ( i in a)
print i
}' temp
rm temp

  #7 (permalink)  
Old 04-10-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Isn't that a bit excessive, code-wise?


Code:
find . -type f | sed -e 's/.*\.//' | sort | uniq -c | sort -rn

Sponsored Links
Closed Thread

Bookmarks

Tags
awk, awk trim, trim, trim awk

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
search pattern in a string and rename deepakgang UNIX for Dummies Questions & Answers 6 06-02-2008 02:43 AM
Search for all the unique file extension riverside Shell Programming and Scripting 1 04-09-2008 05:47 PM
File extension search and copy bsandeep_80 UNIX for Advanced & Expert Users 20 04-04-2008 11:19 AM
How to get file extension shirleyeow Shell Programming and Scripting 17 01-17-2008 08:40 AM
string search in folders with particular multiple file extension anikanch UNIX for Dummies Questions & Answers 2 10-28-2005 11:09 AM



All times are GMT -4. The time now is 10:05 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0