← Back to all snippets
BASH

Batch Rename Files by Pattern

Learn how to batch rename multiple files in a directory using a specified pattern or string replacement with a simple Bash script.

for file in *old_suffix*; do mv "$file" "${file/old_suffix/new_suffix}"; done
How it works: This script iterates through all files in the current directory whose names contain "old_suffix". For each file, it uses parameter expansion `${file/old_suffix/new_suffix}` to replace the first occurrence of "old_suffix" with "new_suffix" in the filename, then renames the file using `mv`. This is highly useful for managing static assets or code files.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs