The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
Google UNIX.COM


UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to find a file named vijay in a directory using find command amirthraj_12 UNIX for Dummies Questions & Answers 6 10-25-2008 09:37 AM
Can I know find syntax to find given date files bache_gowda Shell Programming and Scripting 3 03-26-2008 03:37 AM
Little bit weired : Find files in UNIX w/o using find or where command jatin.jain Shell Programming and Scripting 10 09-19-2007 03:47 AM
Find files older than 20 days & not use find halo98 Shell Programming and Scripting 2 05-18-2006 11:19 AM
command find returned bash: /usr/bin/find: Argument list too long yacsil Shell Programming and Scripting 1 12-15-2003 03:38 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 06-29-2006
Registered User
 

Join Date: Jun 2006
Posts: 15
find and mv files in single line

I have a.txt and b.txt (and so on ) in a directory.

I need to move these files to a.xml , b.xml and so on, basically I just need to rename all the XXX.txt files to XXX.xml using find and mv , how can we do it?
something like below, but it is not right
find . -name '*.txt' -exec mv {}\.xml \;
Any ideas?

Thanks
Muru

Last edited by muru; 06-29-2006 at 03:05 PM.
Reply With Quote
Forum Sponsor
  #2  
Old 06-29-2006
Registered User
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 988
If you have the mmv command, you can just do
Code:
mmv '[ab].txt' '#1.xml'
Find won't do exactly what you want, it can't return PART of a filename. You'd have to just leave it printing the output, piping the names into some other script that filters the names.
Reply With Quote
  #3  
Old 06-29-2006
Registered User
 

Join Date: Jun 2006
Posts: 15
Thanks for your reply, unfortunately SunOs 5.7 doesn't seem to have mmv.

is there any way we could achieve this by following pseudo

find
mv [the file given by find to] ["work on the result of find to cut just the file name without extension" and add our extension (ie .xml)]

instead using cut and appening the .xml, could we use sed to replace extension to .xml and move the original file to this name?

I am sorry if this is not achievable.

Thanks
Muru
Reply With Quote
  #4  
Old 06-29-2006
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,294
Code:
#!/bin/ksh
find /path -name '*.txt' | \
while read file
do
    newfile=${file%.txt}".xml"
    mv $file $newfile
done
I think Sun usually has ksh available
Reply With Quote
  #5  
Old 06-29-2006
Hitori's Avatar
Registered User
 

Join Date: Jun 2006
Posts: 360
You can use something like this:

#!/bin/sh

for vo in *.txt
do
ao=`echo $vo | sed "s/\..*$//"`
mv "$vo" "$ao".xml
done
Reply With Quote
  #6  
Old 06-29-2006
Registered User
 

Join Date: Jun 2006
Posts: 15
Thanks for the reply, both the above suggestions works.

Can we do it in a single line using something like this

for file in `ls *.txt` ;do mv $file `sed 's/\..*$/.txt/'`;done

this doesn;t work, but I feel that this may work with minor tweaks.

Thanks again for the reply!
Reply With Quote
  #7  
Old 06-29-2006
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,294
Code:
find /path -name '*.txt' | while read file ;do ; newfile=${file%.txt}".xml" ; mv $file $newfile ; done
why one line - it's harder to read....
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 04:27 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0