![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | 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 !! |
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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | 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? |
|
||||
|
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 |
|
||||
|
Code:
for file in *; do
noext="${file%.*}"
echo mv "$file" "U010${noext#*.}PQRS"
done
|
|
||||
|
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.
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|