[Solved] Script to rename files using an input file

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
User avatar
catweazel
Level 19
Level 19
Posts: 9763
Joined: Fri Oct 12, 2012 9:44 pm
Location: Australian Antarctic Territory

[Solved] Script to rename files using an input file

Post by catweazel »

Hi, everyone

I need some help with a bash script. I have thousands of files that need certain words removed from their names. I have an input file of those words that need to be removed. I would like to use the rename command in a script with the contents of each line in the input file to replace manual commands:

Code: Select all

rename "s/word_1//g" *
rename "s/word_2//g" *
rename "s/Word_3//g" *
rename "s/WORD_4//g" *
A word to be removed only ever appears once in a file name but it may appear in multiple files, which is handled by rename "s/word_1//g" *.

Can anyone help me with this or set me off in the right direction, please?

I've done the obGoogle search but I'm getting scripts that use an input file with more than one parameter, and scripts that use mv. I don't know enough about scripting to be able to modify these scripts to do what I've described.

All suggestions, except rude ones, are welcome.
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 4 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
"There is, ultimately, only one truth -- cogito, ergo sum -- everything else is an assumption." - Me, my swansong.
User avatar
MrEen
Level 23
Level 23
Posts: 18345
Joined: Mon Jun 12, 2017 8:39 pm

Re: Script to rename files using an input file

Post by MrEen »

catweazel wrote: Sat Aug 25, 2018 11:18 pm Can anyone help me with this or set me off in the right direction, please?

All suggestions, except rude ones, are welcome.

Code: Select all

#!/bin/bash

#code goes here


:D
User avatar
catweazel
Level 19
Level 19
Posts: 9763
Joined: Fri Oct 12, 2012 9:44 pm
Location: Australian Antarctic Territory

Re: Script to rename files using an input file

Post by catweazel »

MrEen wrote: Sun Aug 26, 2018 12:10 am :D
You're not as funny as your face :mrgreen:
"There is, ultimately, only one truth -- cogito, ergo sum -- everything else is an assumption." - Me, my swansong.
User avatar
MrEen
Level 23
Level 23
Posts: 18345
Joined: Mon Jun 12, 2017 8:39 pm

Re: Script to rename files using an input file

Post by MrEen »

You'll probably be able to get me back in a day or two as I expect to posting my own question to this section if my google-fu doesn't make a quick u-turn.

And your topic is much more difficult than mine.
User avatar
catweazel
Level 19
Level 19
Posts: 9763
Joined: Fri Oct 12, 2012 9:44 pm
Location: Australian Antarctic Territory

Re: Script to rename files using an input file

Post by catweazel »

MrEen wrote: Sun Aug 26, 2018 12:22 am And your topic is much more difficult than mine.
And yet I solved it...

Code: Select all

#!/bin/bash
for file in $( cat words.txt ) ; do
    rename "s/$file//g" *
done
"There is, ultimately, only one truth -- cogito, ergo sum -- everything else is an assumption." - Me, my swansong.
User avatar
MrEen
Level 23
Level 23
Posts: 18345
Joined: Mon Jun 12, 2017 8:39 pm

Re: [Solved] Script to rename files using an input file

Post by MrEen »

Ahhh. I see you used that first line I gave you! :P
Locked

Return to “Scripts & Bash”