2

I have several files with these names:

file1, A.ext  
file1, The.ext  

I need to change there names as

A file1.ext  
The file1.ext

That is I need to move the article from the end to the beginning erasing the comma. How would I do that?

I have looked into rename command but I have no experience with perl regex, I came up with this ridiculous command which doesn't work, I'm leaving it for showing my hopelessness:

rename 's/(?<=[A-Z a-z \s]*),\sA//' *
2

You would need to have:

rename -n 's/^([^,]*),\s*(.*)\.ext$/$2 $1.ext/' *
  • This ^([^,]*), matches everything but a comma until a first comma seen from the begging of the files name.

  • This \s*(.*)\.ext$ matches zero-or-more whitespaces followed by anything and ends with .ext.

  • The $2 and $1 is back-references to the (.*) and ([^,]*) respectively.

will do rename as following:

rename(a single file,.ext,  a single file.ext)
rename(a single file, some thing.ext, some thing a single file.ext)
rename(file1, A.ext, A file1.ext)
rename(file1, the.ext, the file1.ext)

P,s: remove -n to do rename on files which is used for testing.

  • I came up with the following before seeing your answer: rename 's/(\d\d)\s((\w|\s)*),\s(A|The)/\1 \4 \2/' * – klaus May 28 '18 at 7:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.