Skip to content

Instantly share code, notes, and snippets.

@stephanb
Last active August 29, 2015 14:13
Show Gist options
  • Save stephanb/3f02636bcf1fe9300b89 to your computer and use it in GitHub Desktop.
Save stephanb/3f02636bcf1fe9300b89 to your computer and use it in GitHub Desktop.
Rename and copy/move all files or directories matching a pattern
find . -type f -name "*your-pattern*" | while read oldname; do newname=`echo $oldname | sed 's/your-pattern/substitute-value/g'`; cp $oldname $newname; done
@stephanb
Copy link
Author

This command:

  1. finds all files containing your-pattern in their filenames in the current directory and its sub-directories
  2. replaces your-pattern with substitute-value in the filename
  3. saves the new file/filename and keeps the old one

Replace your-pattern and substitute-value with your own values.
If you want to rename the file rather than copy it, replace the command cp with mv.
If you want to work with directories only, use -type d instead of -type f.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment