I have been using the rename command to get control over my naming conventions across my system. In converting spaces in file names to hyphens, I have inadvertently created consecutive hyphens in some file names. These are proving difficult to remedy using the rename command.

I have tried unsuccessfully with several different iterations of the following:

rename 's/--/-/g'

I do understand that double hyphens are reserved for end of arguments but a backslash escape doesn't seem to work here and I'm out of other ideas.

I am relatively new to command line processing so your patience is appreciated.

  • 1
    What was wrong with rename 's/--/-/g'? did you get error? – αғsнιη Nov 25 '14 at 16:31

To squash multiple hyphens (one hyphen followed by one or more hyphens) into a single one for all files in the current directory use:

rename 's/--+/-/g' -- *

The -- is important if files start with a hyphen, otherwise they would be interpreted as command line arguments. The * expands to the list of files in the current directory.

  • What does the + do? Just so I have a better understanding and possibly solve similar issues going forward. That worked like a champ by the way! – visyoual Nov 25 '14 at 16:59
  • 1
    It matches one or more of the preceding expression (the character -). – Marco Nov 25 '14 at 17:04

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.