2

Code in question:

ls | cat -n | while read n f; do mv "$f" `printf "video_%03d.mp4" $n`; done

The above code will rename all files/folders within the directory executed to:

video_001.mp4
video_002.mp4
video_003.mp4
and so on... 

However, it would be nice to only target files of specific type, such as only renaming the .mp4 files, rather than renaming everything, which includes directories.

Anonymous
  • 503

1 Answers1

11

This renames all .mp4 files, without parsing ls:

i=1; for file in *.mp4; do mv "$file" $(printf "video_%03d.mp4" "$i"); i=$((i + 1)); done

Any glob can be used in the for file in ... statement. You could apply other criteria by using find instead.

Stephen Kitt
  • 434,908