|
|
#1 |
|
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. |
|
|
|
|
|
#2 |
|
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 |
|
|
|
|
|
#3 |
|
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. |
|
|
|
|
|
#4 |
|
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. |
|
|
|
|
|
#5 |
|
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! |
|
|
|
|
|
#6 |
|
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...)
|
|
|
|
|
|
#7 |
|
Prospect
Join Date: Mar 2002
Posts: 2
|
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?
|
|
|
|
|
|
#8 |
|
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
Code:
for file in foo*bob;do mv "$file" "${file% Converted Recovered.bob}"; done
foo 1 Converted Recovered.bob
__________________
/PEZ |
|
|
|
|
|
#9 |
|
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 |
|
|
|
![]() |
|
|