I have a directory that has files in it that are named 'o1.ray' to 'o293.ray'. I want to move them to an another directory while renaming them 'o132.ray' to 'o424.ray'.

How can I do this in the terminal?

cd directory
for i in {1...293};
do cp o$i.ray subdirectory/o$i+131.ray;

I know this is wrong because I get:

error message 'cp: cannot stat 'o{1...293}.ray': No such file or directory 
  • 3
    Assuming bash (you don't say). Double dots only in {1..293}. Double quotes around string that contain variable expansions. And $(( i + 131 )) is an arithmetic expansion. Use cp -i for safety. I won't turn this into an answer as it's mainly about syntax. – Kusalananda Feb 5 at 23:30
  • 1
    @michael-prokopec - careful adding the bash tag, unless it's absolutely clear from the OP that bash is involved. It's probably involved here, but let's be certain. – Jeff Schaller Feb 6 at 13:13

As Kusalananda hinted at, this is mostly an adjustment of syntax:

for index in {1..293}; do echo mv o"${index}".ray subdirectory/o"$((index+131))".ray; done

Remove the echo when the output looks correct.

Or, with zsh's zmv module:

autoload zmv
zmv -n 'o(<->).ray' 'subdirectory/o$[$1 + 131].ray'

The $[ ... ] syntax performs arithmetic; $1 is captured with the parenthesis around the <-> that follows the o. The <-> is a zsh wildcard that captures numeric ranges; without any endpoints, it's open-ended; for your case, you could be very specific with:

zmv -n 'o(<1-293>).ray' 'subdirectory/o$[$1 + 131].ray'

Remove the -n when the output looks correct.

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.