The UNIX Forums
"Join the Network of UNIX System Users"


 
Subject: how can i rename multiple files depending on a string occuri
heinlein99
Newbie
Rank: 1



UID 173
Digest Posts 0
Credits 0
Posts 31
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 18, 2007 11:57 AM  Profile | P.M. 
how can i rename multiple files depending on a string occuri



how can i rename multiple files depending on a string occuring in the filenames?



i have many files that have "inputstring" somewhere in their filename (without the quotes), and i want to rename them all so that "inputstring" is replaced with "newstring". and i also want to specify arbitrary text for "inputstring" and "newstring" so that i can call the scripts that does this like this:

batchrename apple peer

this should rename all files in present directory that have "apple" somewhere in their filename, and change "apple" into "peer".


i have found some batch rename scripts, but they all depend on fixed values of the string that should be replaced, and that means i have to rewrite a new script each time i want to do this with different strings.

thanks for your help.


Top
ing
Newbie
Rank: 1



UID 187
Digest Posts 0
Credits 0
Posts 9
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 18, 2007 11:57 AM  Profile | P.M. 
code:

#!/bin/sh
ls -1 *$1* | awk -v old="$1" -v new="$2" 'begin{q="\047"}
{           oldfile=$0
            sub(old,new)            
            cmd = sprintf("mv %s%s%s %s%s%s",q,oldfile,q,q,$0,q)
Top
matrix37a
Newbie
Rank: 1



UID 255
Digest Posts 0
Credits 0
Posts 23
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 18, 2007 11:57 AM  Profile | P.M. 
with zsh:


code:
$ autoload -u zmv
$ touch 123inputstringabc abcinputstring123
$ ls -l *string*
-rw-r--r-- 1 administrator none 0 may 21 16:53 123inputstringabc
-rw-r--r-- 1 administrator none 0 may 21 16:53 abcinputstring123
$ zmv '(*)inputstring(*)' '${1}newstring${2}'
$ ls -l *string*
-rw-r--r-- 1 administrator none 0 may 21 16:53 123newstringabc
Top
jops
Newbie
Rank: 1



UID 210
Digest Posts 0
Credits 0
Posts 41
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 18, 2007 11:57 AM  Profile | P.M. 
it works! thanks.
Top
matrix37a
Newbie
Rank: 1



UID 255
Digest Posts 0
Credits 0
Posts 23
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 18, 2007 11:58 AM  Profile | P.M. 
quote:
originally posted by karman
i have many files that have "inputstring" somewhere in their filename (without the quotes), and i want to rename them all so that "inputstring" is replaced with "newstring". and i also want to specify arbitrary text for "inputstring" and "newstring" so that i can call the scripts that does this like this:

batchrename apple peer

this should rename all files in present directory that have "apple" somewhere in their filename, and change "apple" into "peer".

i have found some batch rename scripts, but they all depend on fixed values of the string that should be replaced, and that means i have to rewrite a new script each time i want to do this with different strings.



(untested)


code:

## name: batchrename
## usage: batchrename string1 string2
## description:
##    rename all files with string1 in their name
##    by replacing string1 with string2

string1=$1
string2=$2
for file in *"$string1"*
do
  mv "$file" "${file%%"$string1"*}$string2${file#*"$string1"}"
done
Top
ajscottsr
Newbie
Rank: 1



UID 44
Digest Posts 0
Credits 0
Posts 51
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 18, 2007 11:58 AM  Profile | P.M. 
there is a built-in linux utility "rename" that does the trick



i discovered that my linux comes with the "rename" utility.
usage: "rename inputstring newstring"
Top
ch1one
Newbie
Rank: 1



UID 85
Digest Posts 0
Credits 0
Posts 58
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 18, 2007 11:58 AM  Profile | P.M. 
quote:
originally posted by karman
i discovered that my linux comes with the "rename" utility.
usage: "rename inputstring newstring"

but good to know how to write a script yourself.


there are at least two different versions of rename on linux systems, and their syntax is not the same. if you write a script that uses rename, it may not work on another linux system, and almost certainly will not work on any other *nix system.
Top
 

 

All times are GMT, the time now is Jul 31, 2010 03:39 AM

Powered by Discuz! 5.0.0  © 2001-2006 UNIX Forums
Processed in 0.006014 second(s), 8 queries

Clear Cookies - Contact Us - UNIX Help - Archiver - WAP