If have the following problem: I have a series of files that come in pairs (but not always). There is 2400??????_001.jpg and 2400??????_002.jpg. I need to swap the _001 and _002. So I thought I could do this:
for f in $(find -type f -name "*_002.jpg"); do mv "${f}" "${f%_002.jpg}_003.jpg"; done
for g in $(find -type f -name "*_001.jpg"); do mv "${g}" "${g%_001.jpg}_002.jpg"; done
for h in $(find -type f -name "*_003.jpg"); do mv "${h}" "${h%_003.jpg}_001.jpg"; done
Strangely after step 2 I have *_003.jpg and *_002.jpg that are identical. What is going on here?
Then the problem gets a little more difficult: I only want to swap if both members of the pair exist. Sometimes only 2400??????_001.jpg exists and 2400??????_002.jpg is missing. If this is the case then I want to leave 2400??????_001.jpg alone.