Recursicely search and rename file extension


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Recursicely search and rename file extension
# 1  
Old 04-09-2008
Recursicely search and rename file extension

Greetings to all!!Smilie

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  
Old 04-09-2008
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  
Old 04-09-2008
In relation to https://www.unix.com/shell-programmin...-new-post.html just pipe the output of find to sed and trim away everything except the extension.
# 4  
Old 04-09-2008
Find utility will go recursively down the line ...

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

# 5  
Old 04-09-2008
Quote:
Originally Posted by rubin
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  
Old 04-10-2008
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  
Old 04-10-2008
Isn't that a bit excessive, code-wise?

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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rename specific file extension in directory with match to another file in bash

I have a specific set (all ending with .bam) of downloaded files in a directory /home/cmccabe/Desktop/NGS/API/2-15-2016. What I am trying to do is use a match to $2 in name to rename the downloaded files. To make things a more involved the date of the folder is unique and in the header of name... (1 Reply)
Discussion started by: cmccabe
1 Replies

2. Shell Programming and Scripting

Rename except dot file extension

After reading the manual of the command rename I would like to apply it to a folder with a couple of files containing old style dots before the file-type, e.g. up.to.the.roof.avi. So I'd like to rename them without the dots in between. Therefore I tried it the following way rename -f -n ... (4 Replies)
Discussion started by: 1in10
4 Replies

3. Shell Programming and Scripting

How to rename the extension of a file?

Hello, I have multiple files named rscclog_2013-03-25.txt;3 in a directory, where 2013-03-25 is the previous day's date and the number after extension .txt preceded by a ';' is any number which i do not know beforehand. Now, i have to rename all such files as rscclog_2013-03-25.txt thus,... (2 Replies)
Discussion started by: rahulkt1987
2 Replies

4. Shell Programming and Scripting

Rename file extension.

I have a list file that contains names of many files. I am reading one file name at a time using for loop Then I like to create one more list file but with the file extension changed to "ctl". Note: The file name can have any number of dots ".". But the extension after the last dot should be... (4 Replies)
Discussion started by: pinnacle
4 Replies

5. Shell Programming and Scripting

Rename file to uppercase except extension

Hi, I am trying to make all file in the directory uppercase but not their extension (ex: image.jpg becoming IMAGE.jpg) here is code i am trying. $ ls | while read file do name=${file%%.*} newfilename=$(echo $name | tr 'a-z' 'A-Z') mv $file $newfilename done any suggestions of... (4 Replies)
Discussion started by: johninweb
4 Replies

6. Shell Programming and Scripting

rename a file with new extension

Hi guys, i had many files like filename.20110520_20110519_050030 i have to rename the file by removint the last numerics .. i.e filename.dat i tried with cut command and removed the numerics but i'm not able to add .dat to the files. is there any command insted of cut command to... (1 Reply)
Discussion started by: apple2685
1 Replies

7. UNIX for Dummies Questions & Answers

Shell script to rename or change file extension case.

I searched the forum, but there was different type of rename. Hello. I have files in folder. Like: xxxxxxxx1.html or xxxxxxxx2.txt or xxxxxxxx3.tar.gz and how to rename or change file extension case to xxxxxxxx1.htm or xxxxxxx2.TXT or (5 Replies)
Discussion started by: Sheldon
5 Replies

8. Shell Programming and Scripting

search for a file extension

hi all, i'm new to shell scripting, i need help from u guys to do my task now.. i just need to check a file extension existence in a directory, and if it exists then i have to continue my processing. pls give me the command to check the extension of the files (6 Replies)
Discussion started by: divak
6 Replies

9. Shell Programming and Scripting

rename file extension

I am trying for loop to rename file extension from .txt to .html : as below : for i in *.txt; do mv "$i" `basename $i`.html; done ------------------------------------------- But this renames a file file1.txt as file1.txt.html anyone know how get avoid .html added after .txt ? it... (4 Replies)
Discussion started by: sriram003
4 Replies

10. UNIX for Advanced & Expert Users

File extension search and copy

Hi need to know if we can write a shell script to find files for a particular format;s ie both .csv and .txt in a particular folder and then copy them to a new folder on a dialy basis. Does anyone know how this can be accomplished? Thanks, Sandeep (20 Replies)
Discussion started by: bsandeep_80
20 Replies
Login or Register to Ask a Question