I need to use the rename command to rename hundreds of files which contain .. (2 periods) within the filename and change .. to - (hyphen). The fact that it contains periods seems to be confusing the rename command.

Here are some example filenames:


I need to rename these files to look linke:


This command:

rename 's/../-/' *

results in these filenames:

-180201_190000_TX_AJAX..HHE_instrument_0456 -180201_190000_TX_AJAX..HHN_instrument_0456 -180201_190000_TX_AJAX..HHZ_instrument_0456

It seems that periods have some special purpose so it's getting confused. I then tried this:

rename 's/..H/-H/' *

The AJAX in the sample represents a station and these vary. The above syntax works and results in "20180201_190000_TX_AJAX-HHE_instrument_0456" as expected for many stations but unexpected results for others:

20180201_000000_SA.AKHBR..HNE_instrument_0542 renamed as 20180201_000000_SA.-HBR..HNE_instrument_0542

20180201_000000_SA.KSHB4..HHE_instrument_0935 renamed as 20180201_000000_SA.-HB4..HHE_instrument_0935

20180201_000000_SA.HTM01..HHE_instrument_0933 renamed as 20180201_000000_S-HTM01..HHE_instrument_0933

It seems any station which contains the letter H gets confused.

For some reference, AJAX is a station code, and station codes are either 4 or 5 characters in length.

Next I tried this:

rename ‘s/SA.????..H/SA.????-H/ * -n 

but this generates an error:

"Nested quantifiers in regex; marked by <-- HERE in m/SA.??? <-- HERE ?..H/ at (eval 1) line 1."

so I guess it doesn't like wildcards.

Any help you can provide is appreciated.


Try this :

rename -n 's/\.\./-/' ./*

The . character in , means any character, so it matches everything!

Remove -n when you are satisfied with your tests


The dot will match any character, so the double dot matches the first two characters in the filenames, which is why these are being replaced by a dash by rename. Either escape each dot in the regular expression as \. or use a loop (this is written for bash):

for name in ./*..*; do
    printf 'Would move %s to %s\n' "$name" "$newname"
    # mv -i "$name" "$newname"

Run once and make sure that it outputs the correct thing before removing the # and running again.

The loop will iterate over all names in the current directory that contains two consecutive dots. The ${name/../-} parameter substitution will replace the first occurrence of two consecutive dots with a dash. This is a shell filename globbing pattern, so the dot is not special in any way.

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.