5

I have a set of files that are named in the vein of "some_file-nameN", where N is a number starting at 1, incrementing, and ending somewhere between 10 and 30. I now need to insert a file at the start. How can I increment the number of the file using ZMV and ZSH?

2

Starting from the directory containing the files, and assuming the range is 1 to 30:

for n in {30..1};
mv some_file-name$n some_file-name$(( $n+1 ))
  • worked for me--thanks for posting. It looks like zmv doesn't provide this feature. – Paul Oliver Jul 16 '14 at 2:28
2

Of course zsh can do it.

zmv '(some_file_name)(<->)' '$1$(($2 + 1))'

This should work for any number of files.

  • This would overwrite FILE2 with the contents of FILE1, then overwrite FILE3 with the contents of FILE2, and so on, leaving you with a single file. As such, it actually returns an error: % ls FILE1 FILE2 FILE3 FILE4 FILE5 FILE6 FILE7 FILE8 FILE9 % cat * FILE1 FILE2 FILE3 ... % zmv -n '(FILE)(<->)' '$1$(($2 + 1))' zmv: error(s) in substitution: file exists: FILE2 file exists: FILE3 ... – Dave Mar 31 at 0: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.