0

I run OS X and have multiple photos and started naming them xyz-100.jpg, xyz-101.jpg, xyz-102.jpg up to xyz-402.jpg and now want to rename them by basically reducing the number by 99, making it xyz-1.jpg, xyz-2.jpg up to xyz-303.jpg.

I know I can use the mv command for that, but I don't seem to be able to implement a variable into the filename, such as xyz-$reduced.jpg. How can I properly do that?

1
  • 1
    Try xyz-${reduced}.jpg This will help variable expansion know what to expand and what not to. Commented Apr 25, 2017 at 20:31

2 Answers 2

1

I strongly recommend you move them to another directory, since otherwise you run the risk of confusion if something bad happens midway through. So try:

mkdir tmp
for ((n=100; n<=402; n++)); do 
  ln xyz-$n.jpg tmp/xyz-$((n-99)).jpg
done

That should give you the files you want inside tmp/. Then you can move that directory where you'd like and remove the old files. If you're not familiar with ln, think of it as half of a mv. Basically mv foo bar is the same as ln foo bar; rm foo. By linking the newly named files in a subdirectory, you won't have any conflicts trying to figure out if file 150 is really 150 or if it is the renamed 249. Also, instead of iterating through the existing files and trying to parse the number, just use jot to generate the names that you know you want.

Sign up to request clarification or add additional context in comments.

2 Comments

@mklement0 asks in an edit "why not use seq?" The OP mentioned that he was using OSX (although I think the marketing folks have returned to calling in MacOS!) so I thought jot more appropriate. Unfortunately, I'm fairly certain that neither jot nor seq is standardized, but any method for doing the loop will do. Probably better to do for ((n=100; n<=402; n++)); do
for ((n=100; n<=402; n++)); do is indeed the best approach. Indeed, neither jot nor seq are POSIX-mandated utilities. jot is specific to BSD-like platforms, including the recently rechristened macOS (note the lowercase "m"); by contrast , similar - but not identical - implementations of seq can be found on both Linux and BSD/macOS, and that's what prompted my suggestion: if it's easy to make an answer work on additional platforms, future readers may benefit.
1

something like this should work. but i'm not sure how you're obtaining the number of files. you could also cat the output of another file that list filenames to the same effect as this. but this should get you started

num_array=(1 2 3 4 5)

for i in "${!num_array[@]}"; do
       mv -i ./"myfile_${i}.jpg" /somehwere_else
done

1 Comment

You should always use mv -i for bulk moves like this; otherwise if there's a naming conflict, one file can silently and irreversibly overwrite another.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.