0

I want to strip filenames recursively that have multiple dashes, so that file---name-3.jpg becomes file-name-3.jpg. This works fine within a single folder but I would like to move into the parent folder, and run the command. It also works fine until it runs into a folder name that also has multiple dashes, and then it tries to rename them.

find . -type f -iname '*---*' -depth -exec rename 's/---/-/gi' {} +

parent_folder
  \sub--folder-1
     file--name-1.jpg <-- rename file-name-1.jpg
     file----name-2.jpg <-- rename file-name-2.jpg
  \sub----folder-2
     file--name-1.jpg <-- rename file-name-1.jpg

edit using bash on a CentOS server

Poe
  • 2,971
  • 6
  • 30
  • 38
  • You should tag your question with the language/shell you are using. – assylias Feb 28 '13 at 23:35
  • i remember doing that using sed to parse the final name, as you are using "-type f" and you have the full path name, take a look on this http://stackoverflow.com/questions/4793892/recursively-rename-files-using-find-and-sed – hamilton.lima Mar 01 '13 at 00:00

1 Answers1

1

Use -execdir to run the command from the file's directory with just the filename, rather than -exec which runs it from the current directory with relative path:

find . -depth -type f -iname '*---*' -execdir rename 's/---/-/gi' {} +
that other guy
  • 116,971
  • 11
  • 170
  • 194