I have subdirectories named as dates like 09 January 2020. I renamed all the directories to replace spaces with hyphens using the terminal command:

$ for i in *; do mv "$i" "`echo $i | sed -e 's, ,-,g'`"; done

Now I get subdirectories named as 09-January-2020.
I want to reverse the date order and get subdirectories named as 2020-January-09. I want to use the same for-loop as above but different sed command to reverse the order of date format in the name of subdirectories.


Using sed to create the new names for your directories:

for d in *; do
    echo mv -- "$d" "$(echo "$d" | sed -E 's/(.*)-(.*)-(.*)/\3-\2-\1/')"

The matched parts inside parentheses are being reused into the replacement in reversed order. Remove the first echo after testing.

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.