0

I have a situation where I have hundreds of files that have been incorrectly named. The files all follow a pattern of _XXXX_other.jpg and I need to move all of them over to _XX_XX_other.jpg.

The solution here, obviously, will have multiple pieces. Unfortunately I couldn't even put together a simple regex to match the file pattern. I tried using:

ls -l | grep -e *.[0-9]{4}"_other.jpg"$

Unfortunately this matched everything in the directory instead of just the files w/ that pattern. If I could have figured out the grep I was going to do a for loop, as such:

for i in $(<grep here>); do mv "$i" "${i/<same regex here/?????}"; done

I gave up on my method but I am curious if there is actually a way to do this in a bash script or just from the command line.

  • This is basically a duplicate of a lot of other questions. Are the X's in your examples actually digits as your trial regex suggests? You also seem to have things other than a leading underscore in mind if your regex is to be believed. Writing the regex to `grep` outside single quotes is an act of extreme self-confidence, or maybe extreme foolhardiness; I'd not risk it. The `*` is a candidate for expansion by the shell. Anyway, look for a Perl-based `rename` or `prename` command. If you have one, it will be fairly straight-forward. – Jonathan Leffler Oct 23 '15 at 02:51
  • See also [Get the Perl `rename` utility instead of the built-in one](https://stackoverflow.com/questions/22577767/get-the-perl-rename-utility-instead-of-the-built-in-rename/) – Jonathan Leffler Oct 23 '15 at 02:56

0 Answers0