How to change double hyphen to single hyphen of file names

command linefilesrename

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.

Best Answer

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.

Related Question