3

This will for example recursively find and replace all hyphens in all filenames with a single space:

find . -type f -name "*-*" -execdir prename 's/-/ /g' "{}" \;

how would I modify this to only replace hyphens that are not within the first 8 characters of the file name.

Jieiku
  • 489
  • 4
  • 15

1 Answers1

3

Pathnames passed to prename are prepended with ./ because of -execdir primary. So, you need to keep the first ten characters intact and substitute each dash with a space in the rest of the path string, which can be achieved fairly easily with a while loop (because g flag doesn't work when matches overlap) and PCRE's zero-width positive lookbehind assertion thingy*.

find -name '????????*-*' -type f -execdir prename -n '1 while s/.{10,}\K-/ /' {} +

This invokes prename at least once for each directory, and thus, may be slow due to overhead from initialization. If that is a concern, you can use -exec instead of -execdir, and adjust the Perl expression accordingly. Below is my amateur attempt at it, use with caution.

-exec prename -n '/(.*\/.{8})(.*)/; $_ = $1 . $2 =~ y/-/ /r' {} +

Drop -n if the output looks good.

oguz ismail
  • 1
  • 16
  • 47
  • 69