Sponsored Content
Top Forums UNIX for Dummies Questions & Answers How can I rename multiple files depending on a string occuring in the filenames? Post 302118385 by cfajohnson on Monday 21st of May 2007 05:47:26 PM
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

 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
RPMDEV-BUMPSPEC(1)						   User Commands						RPMDEV-BUMPSPEC(1)

NAME
rpmdev-bumpspec - manual page for rpmdev-bumpspec version 1.0.11 SYNOPSIS
rpmdev-bumpspec [OPTION]... SPECFILE... DESCRIPTION
rpmdev-bumpspec bumps release tags in specfiles. OPTIONS
-h, --help show this help message and exit -c COMMENT, --comment=COMMENT changelog comment (default: "- rebuilt") -u USERSTRING, --userstring=USERSTRING user name+email string (default: output from rpmdev-packager(1)) -r, --rightmost bump trailing .<DIGIT> component if found, append .1 if not; no-op if -s is specified -s STRING, --string=STRING bump trailing .STRING<DIGIT> component if found, append .STRING1 if not; trumps -r -V, --verbose more output -v, --version output version number and exit COPYRIGHT
Copyright (C) 2005-2010 Fedora Project This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later ver- sion. rpmdev-bumpspec version 1.0.11 June 2014 RPMDEV-BUMPSPEC(1)
All times are GMT -4. The time now is 08:42 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy