The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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-20-2008 12:54 AM
renaming files jxh461 UNIX for Dummies Questions & Answers 1 02-04-2008 09:32 PM
Renaming files Tygoon UNIX for Dummies Questions & Answers 7 01-06-2008 10:59 PM
renaming files systemsb UNIX for Dummies Questions & Answers 3 05-25-2006 12:56 AM
renaming files raguramtgr UNIX for Dummies Questions & Answers 4 09-21-2004 10:57 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-23-2008
bbbngowc bbbngowc is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 194
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?
  #2 (permalink)  
Old 04-23-2008
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
  
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,628
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
  #3 (permalink)  
Old 04-23-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
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.
  #4 (permalink)  
Old 04-23-2008
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
  
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,628
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
  #5 (permalink)  
Old 04-23-2008
bbbngowc bbbngowc is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 194
Thanks. I used the for file in *; do

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

Much appreciated!!!!
  #6 (permalink)  
Old 04-23-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
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.
Closed Thread

Bookmarks

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 On




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


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