Aller au contenu

Online commands

Renommage en masse

Rename Files Using Bash Parameter Expansion

Suppose you have:

report_old.txt
data_old.txt
summary_old.txt

and want to replace _old with _new.

Instead of manually renaming each file:

for f in *_old.txt; do mv "$f" "${f/_old/_new}"; done

The important part is:

${f/_old/_new}

Bash takes the filename stored in $f and replaces _old with _new.

The result:

report_new.txt
data_new.txt
summary_new.txt

You can also remove part of a filename:

for f in *.backup; do mv "$f" "${f%.backup}"; done

For example:

report.txt.backup

becomes:

report.txt

This is extremely useful when dealing with batches of consistently named files.