Dev Notes

Software Development Resources by David Egan.

Find and Replace Filenames


Command Line, Linux, Sysadmin
David Egan

This might be needed when starting a project by cloning a scaffold boilerplate from Github (e.g. the WordPress boilerplate plugin).

TODO Build a BASH script to scaffold projects with automatic renaming of files.

Rename filenames in WP Plugin Boilerplate

Rename files and directories recursively. Run from the project root:

# Test run
find . -iname "*" -exec rename -n 's/plugin-name/carawebs-codename/g' '{}' \;

# Apply the Rename, verbose output lists changes
find . -iname "*" -exec rename -v 's/plugin-name/carawebs-codename/g' '{}' \;

Options

  • The . runs the find command recursively in the current directory - in this case it finds all files & directories
  • The -exec argument executes rename for every matching file found - all files.
  • '{}' is replaced with the path name of the file.
  • \; marks the end of the exec expression.
  • The -n option on rename runs the command in “no act” mode.

The substitution takes the form s/target/replacement/[gi], where:

  • target is the string to be replaced
  • replacement is the replacing string

The g option is for ‘global’ action - all instances are replaced.

An i denotes a case insensitive rename.

Examples

Find .jpg files with the string ‘holiday’ in the filename, replacing this with ‘vacation’:

find . -type f -name '*.jpg' | rename 's/holiday/vacation/'

Resources


comments powered by Disqus