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
Renaming Files abch624 Shell Programming and Scripting 2 03-19-2008 08:54 PM
renaming files jxh461 UNIX for Dummies Questions & Answers 1 02-04-2008 05:32 PM
Renaming files Tygoon UNIX for Dummies Questions & Answers 7 01-06-2008 06:59 PM
renaming files systemsb UNIX for Dummies Questions & Answers 3 05-24-2006 09:56 PM
renaming files raguramtgr UNIX for Dummies Questions & Answers 4 09-21-2004 07:57 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-22-2008
Registered User
 

Join Date: Jul 2007
Posts: 88
Stumble this Post!
Need help renaming files

I just can't figure this one out.

I have a lot of files name (for example) ABC1234.5678.ext

I need to rename these files U0105678PQRS

So I'm removing everything before the first "."
I'm keeping "5678" in the file name
Adding "U010" to the front of "5678"
Dropping the ".ext" extension

How can I do this?
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 04-22-2008
Bughunter Extraordinaire
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,099
Stumble this Post!
Create a little script which takes the name of one file as an argument, derives the new name for the file from that argument and then changes its name.

Use "find" to get a list of the files in question one by one, then use the "-exec" clause of find to send these names to the script you have written.

Here it is in detail. First the script:

Code:
#! /bin/ksh
typeset oldname="$1"
typeset newname="$( print - "$oldname" | sed 's/^[^.]*\.//;s/\.[^.]*$//' )"

newname="U010${newname}PQRS"

mv "$oldname" "$newname"

return 0
Save this as "/path/to/script.sh", call it the following way:

Code:
find /some/path -name "*\.*\.ext" -exec /path/to/script.sh {} \;
to change all the files with extension ".ext" and at least one additional dot (".") in their names.

I hope this helps.

bakunin
Reply With Quote
  #3 (permalink)  
Old 04-22-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
Stumble this Post!
Code:
for file in *; do
  noext="${file%.*}"
  echo mv "$file" "U010${noext#*.}PQRS"
done
I left in the echo so you can test without moving. If it looks correct, take out the echo.
Reply With Quote
  #4 (permalink)  
Old 04-22-2008
Bughunter Extraordinaire
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,099
Stumble this Post!
Quote:
Originally Posted by era View Post
Code:
for file in *; do
This will work usually, but if the number of files is really big it might lead to an error because expanding the wildcard will produce a string bigger than the size limit of command strings for the shell (according to POSIX this is 4096 characters IIRC).

bakunin
Reply With Quote
  #5 (permalink)  
Old 04-23-2008
Registered User
 

Join Date: Jul 2007
Posts: 88
Stumble this Post!
Thanks. I used the for file in *; do

It worked. The other one I'll keep for next time.

Much appreciated!!!!
Reply With Quote
  #6 (permalink)  
Old 04-23-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
Stumble this Post!
I believe the wildcard should be safe for functions which are internal to the shell, but I'd welcome any additional insight into this. My own understanding (relatively recently revised) is that the ARG_MAX limit only applies when there is an exec() involved, but of course, different shells might cope differently with internal wildcard handling, too, and have similar limits for that. But anyway, maybe this should go in a separate thread.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




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


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

Content Relevant URLs by vBSEO 3.2.0