17.21 Rename File

20200915 The basic syntax of the rename command is:

$ rename [-n|-v] 's|<regexp>|<replacement>|' *   # s => substitute    y => transliterate

Numerous examples of the command are illustrated throughout this chapter. Here’s some typical use cases:

# Rename backup versions using ~n~ style to numeric versions.

$ rename -n 's/.pdf.&nbsp;([0-9]+)~/_$1.pdf/' *
rename(20201129_paper163.pdf.&nbsp;2&nbsp;, 20201129_paper163_2.pdf)

# Remove redundant final underscore.

$ rename -n 's/_\././' *
rename(20201129_paper163_.pdf, 20201129_paper163.pdf)

# Substitute underscore for spaces in filenames.

$ rename -n 's/ /_/g' *

# Replace various symbols with underscore.

$ rename -n 's/[&:\(\)\[\]]/_/g' *

# Remove strings of underscores.

$ rename -n 's/__*/_/g' *

Your donation will support ongoing development and give you access to the PDF version of this book. Desktop Survival Guides include Data Science, GNU/Linux, and MLHub. Books available on Amazon include Data Mining with Rattle and Essentials of Data Science. Popular open source software includes rattle, wajig, and mlhub. Hosted by Togaware, a pioneer of free and open source software since 1984. Copyright © 1995-2021 Graham.Williams@togaware.com Creative Commons Attribution-ShareAlike 4.0.