I'm trying to search recursively in all subfolders for files with have dots inside their filename and remove the dots (except the last one)

The rename command works on its own, the find command too, but they're not working together:

find ./ -type f -execdir rename -n 's/\.(?=[^.]*\.)//g' *.txt \;

A glob in an -execdir command won't work, better let find search for the files you want and run rename only on them:

find ./ -type f -name "*.txt" -exec rename -n 's/\.(?=[^.]*\.)//g' {} \;

Turns out your rename expression doesn't work with paths, try this instead:

find ./ -type f -name "*.txt" -exec rename -n 's:\.(?=[^./]*\.)::g' {} \;
  • Thanks for the answer. Unfortunately I get this when trying to execute the command (without -n): Can't rename ./test/t.e.s.t.txt /test/test.txt: No such file or directory – Henry Feb 13 '18 at 10:41
  • 1
    I think @Henry's regex can be fixed to require no / after the .: 's:\.(?=[^./]*\.)::g' – muru Feb 13 '18 at 11:07
  • Note that you can use -execdir in place of -exec to prevent rename from messing with the whole path, although in this case I'm not sure how much it helps, since find will prefix each filename with ./ (I guess a bit, since you can then just replace all dots except the leading one) – steeldriver Feb 13 '18 at 19:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.