0

Apologies if this question has been asked in another way somewhere else; I have looked but couldn't find a specific answer.

Anyway, what I want to do is as follows:

I have a folder of files sequentially named with the year and month in their titles, i.e. Fetch_1961-01_Actual.txt, Fetch_1961-02_Actual.txt, ..., Fetch_2002-12_Actual.txt.

What I want to do is rename them so that they are named with a simple index of month by year, i.e. 01.1961.txt, 02.1961.txt, ..., 12.2002.txt.

Any help would be greatly appreciated.

Thanks in advance.

Gregg

PS. I have managed to get this sorted using the following code in R:

mypath <- "path/to/files"
a <- list.files(path = mypath)
mn <- 1:12
yr <- sort(rep(1961:2002, 12))
for (i in yr) {
    b <- paste(rep(mn, 2002-1961), ".", yr, ".txt", sep = "")
}
file.rename(a,b)

Not very elegant, but it saved me a few hours.

Cheers

Aacini
  • 65,180
  • 12
  • 72
  • 108
  • If you're using Windows, you could see the accepted answer to [Rename multiple files in cmd](http://stackoverflow.com/questions/17271586/rename-multiple-files-in-cmd?rq=1) – Simon Jan 10 '15 at 22:46
  • Sorry, I am new to this. I am using the Terminal in OS X Yosemite. My Bad! – GreggMilligan Jan 10 '15 at 22:49

1 Answers1

0

Here is what I would do:

  1. Use sed to make a mv command for each file
  2. Save the sed output to a file
  3. Run the commands in that file and you'll have your files renamed.

Assuming your files are all as well named as your examples, this command will generate what you want:

ls | sed 's/Fetch_\([0-9]*\)-\([0-9]*\)_Actual.txt/mv & \2.\1.txt/'

Run that in the folder of files you want to rename and make sure the output looks good. If it does, save it to a file:

ls | sed 's/Fetch_\([0-9]*\)-\([0-9]*\)_Actual.txt/mv & \2.\1.txt/' > commandList

Then (once you've double checked the commandList file looks right in a text editor) run the commandList file you just made:

bash commandList

Hope this helps!

redlightbulb
  • 1,466
  • 11
  • 9