Go Back   The macosxhints Forums > OS X Help Requests > UNIX - General



Reply
 
Thread Tools Rate Thread Display Modes
Old 08-01-2002, 09:09 PM   #1
saint.duo
MVP
 
Join Date: Jan 2002
Location: Seattle
Posts: 1,084
renaming

I want to take a bunch of files in a folder that end in "Converted Recovered.bob" and remove that part.

So, if my files were:
foo1 Converted Recovered.bob
foo2 Converted Recovered.bob
foo3 Converted Recovered.bob

I want their names to change to:
foo1
foo2
foo3

anyone? merv, you seem to be Mr. Scripter sometimes.
saint.duo is offline   Reply With Quote
Old 08-01-2002, 10:02 PM   #2
mervTormel
League Commissioner
 
Join Date: Jan 2002
Posts: 5,536
you'll want to test this with excrutiating meticulocity...
Code:
% cat bin/zren

#!/bin/sh
#
#  string-rename - Script to rename with replaced strings
#
#  Example: To replace all "_" with " " do
#           string-rename "_" "\ " filename ...
#
#  tkchan@rescomp.berkeley.edu
#

case $# in
  0|1|2)
     echo "Usage: string-rename STRING_TARGET STRING_REPLACE FILENAME..."
     echo
     echo "Example: string-rename \"_\" \"\ \" my_file"
     echo "  will rename \"my_file\" to \"my file\""
     exit 1
     ;;
  *)
     PAT="s/$1/$2/g"
     shift
     while [ $# -gt 1 ]
     do
        shift
        NEWNAME=`echo $1 | sed -e "$PAT" -e "s/  / /g"`
        echo Renaming $1 to $NEWNAME
        mv "$1" "$NEWNAME"
     done
     ;;
esac
exit 0

% zren "Converted Recovered.bob" "" "foo1 Converted Recovered.bob" 
Renaming foo1 Converted Recovered.bob to foo1

# it appears to work here
mervTormel is offline   Reply With Quote
Old 08-01-2002, 10:15 PM   #3
saint.duo
MVP
 
Join Date: Jan 2002
Location: Seattle
Posts: 1,084
holy smokes! Can't be done with one line using a mv command, I would gather.

thanks merv, I'll save that one into my archives.
saint.duo is offline   Reply With Quote
Old 08-01-2002, 10:25 PM   #4
mervTormel
League Commissioner
 
Join Date: Jan 2002
Posts: 5,536
it could be done in a one liner, but it would be hamstrung for larger jobs, ergo, the loop. this is a more general purpose script, hedging that you may want/need to do this in the future.

and it's self documented.

and i think it'll handle globbing.
mervTormel is offline   Reply With Quote
Old 08-01-2002, 10:33 PM   #5
vickishome
MVP
 
Join Date: Jul 2002
Location: Texas
Posts: 1,075
Try FileRenamer. I used it last week to rename about 500 files in 30 folders. Worked very well. Simple to use. Freeware too!
__________________
Vicki
• 15" MacBook Pro 2.66GHz i7, Mavericks 10.9.1, 8GB RAM
• iPad 4G WiFi 64GB
• iPhone 5 64GB
• 15" MacBook Pro 2.4GHz, Tiger 10.4, 4GB RAM
• G5 Dual 2GHz, Panther 10.3, 1.5GB RAM
• G4 Dual 1GHz, Tiger 10.4, 1.5GB RAM

Using Macs since 1986!
vickishome is offline   Reply With Quote
Old 08-01-2002, 10:51 PM   #6
saint.duo
MVP
 
Join Date: Jan 2002
Location: Seattle
Posts: 1,084
I would use that program, except for the fact that I needed a command line solution. This is part of the admin work for our Xserve *drool*, and Apple Remote Desktop doesn't work too well over my connection at home, due to my nasty uplink speed. (speaking of which, I need to call my ISP and get that fixed...)
saint.duo is offline   Reply With Quote
Old 08-14-2002, 07:13 AM   #7
Tapp_Darden
Prospect
 
Join Date: Mar 2002
Posts: 2
Question counting?

can you make it count?

like rename files

rararchive.part01.rar -> rararchive.r01
rararchive.part02.rar -> rararchive.r02
rararchive.part03.rar -> rararchive.r03
...

how would you do this?
Tapp_Darden is offline   Reply With Quote
Old 08-14-2002, 07:53 AM   #8
osxpez
Major Leaguer
 
Join Date: May 2002
Location: Sweden
Posts: 282
If you are using a ksh based shell (like zsh or bash) you could take advantage of parameter expansion:
Code:
for file in foo*.bob;do mv "$file" "${file%% *.bob}"; done
Or more specific if you have lots of other files in the directory:
Code:
for file in foo*bob;do mv "$file" "${file% Converted Recovered.bob}"; done
In ksh (and it's derivates) ${param %pattern} deletes pattern from the value of parameter . %% is the "greedy" version and would make the first pattern ( *.bob) fail if your files were named like:
foo 1 Converted Recovered.bob
__________________
/PEZ
osxpez is offline   Reply With Quote
Old 08-14-2002, 08:43 AM   #9
osxpez
Major Leaguer
 
Join Date: May 2002
Location: Sweden
Posts: 282
Re: counting

Tapp: I'm sure you could use a "counting" approach for that, but you could also again take advantage of parameter expansion:
Code:
for file in *part*.rar;do tmp="${file/part/r}"; echo mv "$file" "${tmp%.rar}"; done
__________________
/PEZ
osxpez is offline   Reply With Quote
Reply


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

Forum Jump



All times are GMT -5. The time now is 10:35 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.