The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



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
Split file into multiple files depending upon first 4 digits deepakgang Shell Programming and Scripting 4 04-09-2008 01:21 AM
rename multiple files antointoronto Shell Programming and Scripting 13 03-20-2008 10:16 AM
Script to backup multiple files and amend their filenames m223464 Shell Programming and Scripting 5 11-25-2005 06:30 AM
Rename multiple files luiz_fer10 UNIX for Dummies Questions & Answers 7 06-11-2002 08:06 AM
Rename Multiple Files molonede UNIX for Dummies Questions & Answers 1 11-14-2000 12:40 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-21-2007
karman karman is offline
Registered User
  
 

Join Date: May 2007
Posts: 12
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.
  #2 (permalink)  
Old 05-21-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,513
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)
	    cmd | getline #or system(cmd)
}'
  #3 (permalink)  
Old 05-21-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
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
-rw-r--r-- 1 Administrator None 0 May 21 16:53 ABCnewstring123
  #4 (permalink)  
Old 05-21-2007
karman karman is offline
Registered User
  
 

Join Date: May 2007
Posts: 12
It works! Thanks.
  #5 (permalink)  
Old 05-21-2007
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
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
  #6 (permalink)  
Old 05-22-2007
karman karman is offline
Registered User
  
 

Join Date: May 2007
Posts: 12
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"

But good to know how to write a script yourself.
  #7 (permalink)  
Old 05-22-2007
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
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.

Closed Thread

Bookmarks

Tags
linux

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:25 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0