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:

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.


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.

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.