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/"
- 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
- rename: renames the file using a regular expression as substitution pattern