Visit Our UNIX and Linux User Community


How can I rename multiple files depending on a string occuring in the filenames?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How can I rename multiple files depending on a string occuring in the filenames?
# 1  
Old 05-21-2007
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  
Old 05-21-2007
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  
Old 05-21-2007
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  
Old 05-21-2007
It works! Thanks.
# 5  
Old 05-21-2007
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  
Old 05-22-2007
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  
Old 05-22-2007
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.

 

Previous Thread | Next Thread
Test Your Knowledge in Computers #91
Difficulty: Easy
To change to another directory in Linux, you use the cd command.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rename multiple files in one go

OS : Oracle Linux 6.8 shell : bash As shown below, I have multiple files like below (query1-extract_aa, query1-extract_ab, query1-extract_ac, ....) $ ls -l total 235680 -rw-rw-r-- 1 reportusr reportusr 30M May 3 11:25 query1-extract_aa -rw-rw-r-- 1 reportusr reportusr 30M May 3 11:25... (5 Replies)
Discussion started by: kraljic
5 Replies

2. Shell Programming and Scripting

SBATCH trinity for multiple files and rename/move the output files

Hey guys, I have wrote the following script to apply a module named "trinity" on my files. (it takes two input files and spit a trinity.fasta as output) #!/bin/bash -l #SBATCH -p node #SBATCH -A <projectID> #SBATCH -n 16 #SBATCH -t 7-00:00:00 #SBATCH --mem=128GB #SBATCH --mail-type=ALL... (1 Reply)
Discussion started by: @man
1 Replies

3. Shell Programming and Scripting

There are multiple filenames in the directory.How to return the the lastest files for each file name

there are mutiple file nams in the directory. How to return the the lastest files for each file name. ex. abc1234_050201 abc1234_050206 abc1234_050208 xyz34_050204 xyz34_050210 xyz34_050218 thanks (4 Replies)
Discussion started by: grand_sam
4 Replies

4. UNIX for Dummies Questions & Answers

Is there any way to cat multiple files and show filenames?

Hi, Is there any way to do a cat * where it shows the name of each file in the process? Similar to what more does below? $ more ?.sql :::::::::::::: 1.sql :::::::::::::: set linesize 200 select db_unique_name, cast( from_tz( cast(... (5 Replies)
Discussion started by: newbie_01
5 Replies

5. Shell Programming and Scripting

How to rename multiple files at one go?

Hi, I have hundreds of files with XXX in their file name and I want to rename all of them with YYY in place of XXX. for ex: $ ls -1 123XXX789 345XXX678 Output $ ls -1 123YYY789 345YYY678 I know we can loop in each file and sed to replace and rename each file but ren *XXX* *YYY*... (4 Replies)
Discussion started by: reddyr
4 Replies

6. UNIX for Dummies Questions & Answers

Rename Multiple Files

Hey guys, I am the definition of a newbie. I am in the process of trying to rip all my dvds onto a new HTPC I setup. While doing this, I am also trying to organize a bunch of other files I already have to proper naming conventions. So far I have just been naming each file separately (I am on a... (4 Replies)
Discussion started by: Ralze34
4 Replies

7. Shell Programming and Scripting

write to multiple files depending on file type (solved)

I want a script that will add comments to a file before check-in files. comments depend on type of files. i have a script to list files in the directory that will be checked-in There are xml, js, html, vm files. vm will use comments like c/c++ ( // or /*..*/) can you help me add a comment line... (0 Replies)
Discussion started by: pradeepmacha
0 Replies

8. Shell Programming and Scripting

extract multiple cloumns from multiple files; skip rows and include filenames; awk

Hello, I am trying to write a bash shell script that does the following: 1.Finds all *.txt files within my directory of interest 2. reads each of the files (25 files) one by one (tab-delimited format and have the same data format) 3. skips the first 10 rows of the file 4. extracts and... (4 Replies)
Discussion started by: manishabh
4 Replies

9. Shell Programming and Scripting

Split file into multiple files depending upon first 4 digits

Hi All, I have a file like below: 1016D"ddd","343","1299" 1016D"ddd","3564","1299" 1016D"ddd","3297","1393" 1016D"ddd","32989","1527" 1016D"ddd","346498","1652" 2312D"ddd","3269","1652" 2312D"ddd","328","1652" 2312D"ddd","2224","2100" 3444D"ddd","252","2100" 3444D"ddd","2619","2100"... (4 Replies)
Discussion started by: deepakgang
4 Replies

10. Shell Programming and Scripting

Script to backup multiple files and amend their filenames

Hi, I'm trying to write a command that backs up certain files into my current directory and adds a prefix to the backed up file name. I realise this can be done in a script by specifying each individual file but would like to know if it can be done on one line and made an alias. I have the... (5 Replies)
Discussion started by: m223464
5 Replies

Featured Tech Videos