Linux: how to recursively rename files

One of the most common tasks when it comes to file manipulation is renaming files, to rename one or two files manually is easy, but how we can automate a mass file renaming task?

Scenario: we have a directory with sub-directories that contain files with xlsx extension that we want to change to xls (clarification: we don’t change the format of the file, just the extension for example purposes).

Solution: We can use find, xargs and rename to create an one liner that will do the job

$ find . -type f -name "*.xlsx"  | xargs -r rename "s/xlsx/xls/"

Explanation:

  • find: From current directory searches recursively all files of extension “xlsx”
find . -type f -name "*.xlsx"
  • xargs: passes output of find to rename as parameters
xargs -r
  • rename: renames the file using a regular expression as substitution pattern
rename "s/xlsx/xls/"

DevOps engineer, loves Linux, Python, cats and Amiga computers

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store