The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Lots of questions about linux. Methal Linux 1 04-25-2008 08:06 AM
renaming xls file systemsb UNIX for Dummies Questions & Answers 2 10-19-2007 04:50 AM
Help in renaming file !!! kumarsaravana_s UNIX for Dummies Questions & Answers 5 05-02-2007 09:45 AM
Deleting lots of files..... B14speedfreak UNIX for Dummies Questions & Answers 8 06-09-2006 04:47 PM
Renaming a file name dbrundrett Shell Programming and Scripting 2 01-06-2004 07:36 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 07-29-2005
Registered User
 

Join Date: Jul 2005
Posts: 9
Lots of file copyingand renaming?

I have Approx 1000 files that all need to be copied from one directory to another. However, each file has to be renamed while it is copied. I have a Tab delimited text file containing all of the old and new file names (they are not all the same character length). Is there a way to read in the old and new file names from this text file into a script that can perform the copy and rename on all the .mat files in the directory.

Example of filenames.txt.
old_name1.mat newfile_name1.mat
old_name2.mat newfile_name2.mat
old_name3.mat newfile_name3.mat

Thank you in advance for any help you can give.
Reply With Quote
Forum Sponsor
  #2  
Old 07-29-2005
piltrafa's Avatar
Registered User
 

Join Date: Oct 2001
Location: Buenos Aires, Argentina
Posts: 77
Something like this.....

for foobar in * ; do mv $foobar newfile$foobar ; done

.... could help
Reply With Quote
  #3  
Old 07-29-2005
Registered User
 

Join Date: Jul 2005
Posts: 9
I was just giving the "newfile" as an example. Each filename is very complex (30ish characters) and the naming scheme between the old and new file is changing completely. That is why I need a script to read the first item in each line of the text file and rename it to the second item in that line then go on to the next for all 1000+ files. If it helps I could format the text file into something else like .csv or whatever.

Thanks
Reply With Quote
  #4  
Old 07-29-2005
fdarkangel's Avatar
Registered User
 

Join Date: Apr 2005
Location: /dev/null
Posts: 42
you can use sed.
it'd help if you tell what conversion you need exactly, though.
Reply With Quote
  #5  
Old 07-29-2005
piltrafa's Avatar
Registered User
 

Join Date: Oct 2001
Location: Buenos Aires, Argentina
Posts: 77
fdarkangel is right

use sed to read n character from each file , then put it in a variable.
For each file, reads the value from $VARIABLE and then mv to the desire pattern.

but be aware that sed could be a little bit tricky.
Reply With Quote
  #6  
Old 07-29-2005
Registered User
 

Join Date: Jul 2005
Posts: 9
Quote:
you can use sed.
it'd help if you tell what conversion you need exactly, though.
I wish could tell you the exact file names but I can not do to the nature of the project. However, I can tell you the old name starts with the date in YYMMDD format then a _ followed by anywhere between 15 to 30 characters detailing the specifics of the file. The new naming format is totally different, different order of information and the date is pushed to the end of the name in MMDDYY format. The old and new names may or may not have the same character lengths and the character lengths vary between files. That is why I was hoping to figure out how to read in the names to a script from the text file. Since I already have a Tab delimited text file containing the old verses new names for all 1000+ files.

It will be something like this YYMMDD_LongDesciption_of_File.mat changed to Totally_different_Char_string_MMDDYY.mat about a thousand times.

Sorry about my lack of knowledge about Unix but I kind of just got thrown into full force in the last month or so. I will do some research on the sed command and try to figure out how to use it. Thanks for your help and any additional help/code anyone can give.
Reply With Quote
  #7  
Old 07-29-2005
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,611
Try something like:
Code:
#! /usr/bin/ksh

exec < /file/with names
while read oldname newname ; do
       echo cp /old/dir/$oldname /new/dir/$newname
done
exit 0
With that echo, it won't actually copy anything, it will just display what it would do if you remove the echo. So try it like that first.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 03:06 AM.


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