Script for batch renaming


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script for batch renaming
# 1  
Old 11-05-2015
Script for batch renaming

What is wrong with this script to rename batch of similar files? For example renaming all *.txt files to *.tab .
Code:
$ for i in seq {01..10}; do touch ${i}.txt; done

$ ./rename.sh *.txt txt tab

Error:
mv: ‘01.txt’ and ‘01.txt’ are the same file.

Code is:
Code:
#!/usr/bin/bash 
# renames.sh 
# Usage: ./rename.sh regex match replacement 
 
pattern=$1 
match=$2 
replace=$3 
 
for i in $( ls *$pattern* );  
do 
 src=${i} 
 dst=$( echo ${i} | sed -e "s/${match}/${replace}/" ) 
 mv ${src} ${dst} 
done

I am aware of the rename command, and read the thread. The job can be done with
Code:
for i in *.text*; do mv ${i} ${i%.text}.tab; done

but here I am trying to understand passing arguments in shell script.
Anybody give me a clue, please?
Thanks!

Last edited by yifangt; 11-05-2015 at 02:32 PM..
# 2  
Old 11-05-2015
*.txt will be expanded by the shell to yield a long parameter list in which $1 is 01.txt, $2 is 02.txt. and $3 is 03.txt, resulting in sed -e s/02.txt/03.txt/ which doesn't substitute anything, so it will try to mv 01.txt 01.txt ---> error!
This User Gave Thanks to RudiC For This Post:
# 3  
Old 11-05-2015
Thanks!
Do you mean the problem is with the part: for i in $( ls *$pattern* ) ?
I changed the command line to:
Code:
$ ./rename.sh txt txt tab

which worked well, but it looks strange, doesn't it?!
And it has a bug as all the file names containing "txt" will be changed. That does not match what I intended.
I am still not quite sure about the expansion for *.txt Could you please elaborate more?
Code:
$ ./rename.sh \.txt txt tab

also worked, how is it working by a simple escape of the dot?

Last edited by yifangt; 11-05-2015 at 03:55 PM..
# 4  
Old 11-05-2015
How about quoting the parameter?
Code:
./rename.sh "*.txt" txt tab

This User Gave Thanks to RudiC For This Post:
# 5  
Old 11-05-2015
It works without suprise, but I am wondering why it did not work if "*.txt" is not quotated, or \.txt dot is not escaped.
# 6  
Old 11-05-2015
Quote:
Originally Posted by yifangt
It works without suprise, but I am wondering why it did not work if "*.txt" is not quotated, or \.txt dot is not escaped.
In the same directory where you ran your previous script, install this script:
Code:
#!/bin/bash
printf '$#=%d\n$0="%s"\n' $# "$0"
cnt=$#
for((i = 1; i <= $cnt; i++))
do	printf '$%d="%s"\n' $i "$1"
	shift
done

and compare the output you get when you run the commands:
Code:
./scriptname *.txt txt tab

./scriptname \*.txt txt tab

./scriptname "*.txt" txt tab

./scriptname '*.txt' txt tab

./scriptname "*".txt txt tab

./scriptname *".txt" txt tab

This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 11-06-2015
Thanks Don!
Now I understand why my first command did not work, on which RudiC had explained *.txt will be expanded by the shell to yield a long parameter list in which $1 is 01.txt , $2 is 02.txt . and $3 is 03.txt, but I did not get it.
Thanks you both!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Renaming batch by removing substring

I wish to rename all files ending in .txt by removing .tex. Currently I have me@me-Inspiron-518:~$ ls a.tex.txt bin b.tex.txt c.tex.txt Desktop Documents Downloads d.tex.txt Music Pictures Public Templates VideosDesired outcome me@me-Inspiron-518:~$ ls a.txt ... (2 Replies)
Discussion started by: Xubuntu56
2 Replies

2. Shell Programming and Scripting

SFTP batch not renaming file with "put"

I have a .ksh script that creates an sftp batch file and runs it through sftp. It works except for one thing. If I try to "put" to a different name, it doesn't use the specified remote name...it still "puts" the original local name. I've tried both of these, and neither work...it will always... (4 Replies)
Discussion started by: dbiggied
4 Replies

3. Shell Programming and Scripting

Renaming file in batch

Hi guys, I need a script to change the file names e.g.:- below are the mentioned files, i want t0 change the last character of the name 00000.cdr with e.g bep01.smsc.191214210500-00000.cdr to bep01.smsc.191214210500-92311.cdr next file be like bep01.smsc.191214210500-92312.cdr # ls -ltr... (9 Replies)
Discussion started by: mfaizan40
9 Replies

4. Shell Programming and Scripting

Need help in batch renaming files with bash shell script.

I have some 50+ files in the following format : abcd_vish_running_ZEBRA_20140818.dat_08-14-2014_23:08:23 abcd_vish_running_ZEB-RA_20140818.dat_08-14-2014_23:08:35 abcd_vish_running_ZEB_RA_20140818.dat_08-14-2014_23:08:37 abcd_vish_running_RI-NG_20140818.dat_08-14-2014_23:08:42... (5 Replies)
Discussion started by: SriRamKrish
5 Replies

5. Shell Programming and Scripting

BASH Batch renaming insert additional zero into filename

Hi all, Wondering how this could be accomplished........ a directory contains sequentially numbered files from fw01 to fw999. How would I insert an additional zero so that the directory lists these files in a proper manner? (i.e. all double digit files from fw01 to fw99 would become... (3 Replies)
Discussion started by: putter1900
3 Replies

6. Shell Programming and Scripting

Executing a batch of files within a shell script with option to refire the individual files in batch

Hello everyone. I am new to shell scripting and i am required to create a shell script, the purpose of which i will explain below. I am on a solaris server btw. Before delving into the requirements, i will give youse an overview of what is currently in place and its purpose. ... (2 Replies)
Discussion started by: goddevil
2 Replies

7. Shell Programming and Scripting

script for renaming a batch of files

hi i have a folder full of files. some of the names are quite off because the dimensions were the same and i had to put a 'b' after the initial number so that it didnt overwrite. what i want is a script in unix to overwrite the filwe name leaving some of the title intact, e.g. below are some... (3 Replies)
Discussion started by: shabs1985
3 Replies

8. UNIX for Dummies Questions & Answers

Batch Renaming of Files

Hello all, thanks for your time (and this forum, what an awesome resource for newbs like myself!) Anyways, I've been given the task of importing content from a directory of about...7000 HTML files. They are all named appropriately and broken down by name depending on what book they belong too.... (8 Replies)
Discussion started by: gratefulhokie
8 Replies

9. Shell Programming and Scripting

batch renaming ...

hi all, given a path, for example : /<pwd>/artist/album/ what i would like to do is to rename the album directory like that : /<pwd>/artist/artist | album/ and i would like to do the latter for all the "artist" directories and for all the "album" directories that belong to an artist ... (4 Replies)
Discussion started by: OneDreamCloser
4 Replies

10. UNIX for Dummies Questions & Answers

Batch Renaming: Change files' extensions in many sub-directories

Hi all - I'm trying to rename a large number of files all at once and need some help figuring out the command line syntax to do it. I've already done quite a bit of research with the rename and mv commands, but so far haven't found a solution that seems to work for me. So: The files exist... (10 Replies)
Discussion started by: dave920
10 Replies
Login or Register to Ask a Question