Avatar of bakerg1
bakerg1
Flag for United States of America asked on

Need script to change portions of filenames in a directory from user input

We use a program that creates multiple files based on an event number that is manually entered.  If the event number is entered incorrectly, we have to change the name on many created files; however, the event number is only a portion of the file name.  I only need to change the event number in the file name, is there a way to do that on all files within a specific directory through a script that requests the wrong event number and the correct event number from the user?  I have researched sed for this but have not been able to get it to work.  I am using Bash to write the script.
Linux DistributionsLinuxDocument Management

Avatar of undefined
Last Comment
bakerg1

8/22/2022 - Mon
woolmilkporc

#!/bin/bash

OLD="1234"
NEW="4567"
DIR="/path/to/dir"

ls $DIR/*OLD* | while read FILE
    do
      echo mv $FILE $(echo $FILE |sed "s/$OLD/$NEW/");
    done

Please note that I put echo in front of the "mv" statement so you can do kind of a "dry run".
Remove the bolded echo statement to perform the real operation.

Further, I added the old and new numbers as well as the directory as variables.
If you don't know how to request user input to fill these variables please let me know.

wmp
bakerg1

ASKER
I will give this a try as soon as I can.  Will this work if the filenames are structured like "123456-009-23-051" and I need to change it to "123789-009-23-051"?
woolmilkporc

As long as your intention is changing "456" to "789", yes.

Please note that the old as well as the new values must each consist of one contiguous  string, i.e.
my script cannot change "aaa123abc456zzz" to "aaa321abc654zzz" if you just give "123456" for OLD and "321654" for NEW. It can only work if you give "123abc456" for OLD and "321abc654" for NEW.

A problem can arise when the OLD string appears more than once in the filename. The script will change only the first occurrence. Changing all occurrences is fairly easy to accomplish, changing e.g the second occurrence if there are two but the first if there is just one would require significantly more effort.
Your help has saved me hundreds of hours of internet surfing.
fblack61
bakerg1

ASKER
That worked perfectly.  Would it be possible to also scan the contents of each file and make the same change for the event number?  This would then fully update the file with a single script.  Thanks for the help!
ASKER CERTIFIED SOLUTION
woolmilkporc

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
bakerg1

ASKER
Phenomenal help, both in quality and timeliness.  Thanks woolmilkporc!
bakerg1

ASKER
The updated script works; however, it doesn't remove the old files.  The new files are listed along with the old ones - the new ones are completely changed though, which is good.  What am I missing?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
woolmilkporc

Nothing. It was my mistake. Sorry.

#!/bin/bash

OLD="1234"
NEW="4567"
DIR="/path/to/dir"

ls $DIR/*OLD* | while read FILE
    do
      FILENEW=$(echo $FILE |sed "s/$OLD/$NEW/")
      sed "s/$OLD/$NEW/" $FILE > $FILENEW
       rm $FILE
    done
bakerg1

ASKER
That worked.  Thanks again!