[Solved] Script to rename files using an input file

Forum rules
Before you post please read how to get help
Post Reply
User avatar
catweazel
Level 17
Level 17
Posts: 7449
Joined: Fri Oct 12, 2012 9:44 pm
Location: Australian Antarctic Territory

[Solved] Script to rename files using an input file

Post by catweazel » Sat Aug 25, 2018 11:18 pm

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 catweazel on Sun Aug 26, 2018 12:32 am, edited 3 times in total.
¡uʍop ǝpısdn sı buıɥʇʎɹǝʌǝ os ɐıןɐɹʇsnɐ ɯoɹɟ ɯ,ı

User avatar
MrEen
Level 6
Level 6
Posts: 1462
Joined: Mon Jun 12, 2017 8:39 pm

Re: Script to rename files using an input file

Post by MrEen » Sun Aug 26, 2018 12:10 am

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 17
Level 17
Posts: 7449
Joined: Fri Oct 12, 2012 9:44 pm
Location: Australian Antarctic Territory

Re: Script to rename files using an input file

Post by catweazel » Sun Aug 26, 2018 12:14 am

MrEen wrote:
Sun Aug 26, 2018 12:10 am
:D
You're not as funny as your face :mrgreen:
¡uʍop ǝpısdn sı buıɥʇʎɹǝʌǝ os ɐıןɐɹʇsnɐ ɯoɹɟ ɯ,ı

User avatar
MrEen
Level 6
Level 6
Posts: 1462
Joined: Mon Jun 12, 2017 8:39 pm

Re: Script to rename files using an input file

Post by MrEen » Sun Aug 26, 2018 12:22 am

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 17
Level 17
Posts: 7449
Joined: Fri Oct 12, 2012 9:44 pm
Location: Australian Antarctic Territory

Re: Script to rename files using an input file

Post by catweazel » Sun Aug 26, 2018 12:33 am

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
¡uʍop ǝpısdn sı buıɥʇʎɹǝʌǝ os ɐıןɐɹʇsnɐ ɯoɹɟ ɯ,ı

User avatar
MrEen
Level 6
Level 6
Posts: 1462
Joined: Mon Jun 12, 2017 8:39 pm

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

Post by MrEen » Sun Aug 26, 2018 12:36 am

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

Post Reply

Return to “Scripts & Bash”