Link to home
Start Free TrialLog in
Avatar of bakerg1
bakerg1Flag 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.
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

#!/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
Avatar of 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"?
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.
Avatar of 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
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bakerg1

ASKER

Phenomenal help, both in quality and timeliness.  Thanks woolmilkporc!
Avatar of 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?
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
Avatar of bakerg1

ASKER

That worked.  Thanks again!