![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
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? |
| Forum Sponsor | ||
|
|
|
|||
|
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
Code:
find /some/path -name "*\.*\.ext" -exec /path/to/script.sh {} \;
I hope this helps. bakunin |
|
|||
|
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 |
|
|||
|
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.
|
|||
| Google The UNIX and Linux Forums |
| Thread Tools | |
| Display Modes | |
|
|