Solved

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

Posted on 2012-03-12
9
292 Views
Last Modified: 2012-03-12
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.
0
Comment
Question by:bakerg1
  • 5
  • 4
9 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 37711401
#!/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
0
 

Author Comment

by:bakerg1
ID: 37711490
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"?
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 37711543
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:bakerg1
ID: 37711826
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!
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 37711931
#!/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
    done

This will rename the files and change their content in one go.

But please be aware that this will work only against the OLD filenames!
So if you already ran the first version of the script please let me know.
0
 

Author Closing Comment

by:bakerg1
ID: 37712001
Phenomenal help, both in quality and timeliness.  Thanks woolmilkporc!
0
 

Author Comment

by:bakerg1
ID: 37712096
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?
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 37712113
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
0
 

Author Comment

by:bakerg1
ID: 37712229
That worked.  Thanks again!
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to show how we can create Linux Mint virtual machine using Oracle Virtual Box. To install Linux Mint we have to download the ISO file from its website i.e. http://www.linuxmint.com. Once you open the link you will see …
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
This video is the second in a two-part series that discusses PaperPort's "Send To Bar" feature . The first video tutorial (http://www.experts-exchange.com/VP_207.html) explains the purpose of the Send To Bar, how to use it, and how to hide unwanted …
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question