V
V
Vitaly Gusev2019-05-29 14:51:06
linux
Vitaly Gusev, 2019-05-29 14:51:06

How to write a bash script that searches directories recursively and renames them?

Good afternoon!
There is a computer with CentOS 6. There is a directory where users' mail was transferred from the Windows server.
The problem is that the MDaemon mail server, which runs on a Windows server, double-encodes all Russian-language user folders: first it encodes in UTF-16BE, and then the resulting result encodes base64. After that, the " & " sign is added before the received word , and the double equals (==) is replaced by a hyphen (well, or a minus). And at the very end, " .IMAP " is added to the folder name. That is, the name of the Russian-language folder looks like this (together with the path): "Users\domain.ru\ivanov.i\&BBoEPgQ9BEIEMAQ6BEIESw-.IMAP".
I need to recode all such folders in UTF8. That is, I need the script to recursively go through all the directories, find directories in them that start with the symbol & , replace the minus with 2 equals, remove the symbol & from the directory name , decode with base64 and convert from UTF-16BE to UTF -eight.
I've already broken my head.
At the moment I'm trying to write my own version, but if someone has a simpler solution, then help the slow-witted.
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Zhurkin, 2019-05-29
@icCE

You need to remove the & symbol and everything after -
To do this, you can use sed sed -e 's|.*&||; s|-.*|| '
Further, you can approach the question in different ways, you can use find .
find both from base64 and everything else, I think you will find yourself.
As a result, it will be possible to combine everything into one code.
PS These are all examples, before you try create a separate test bench and backup your data. This is not a ready made solution.
PPS

change minus to 2

I missed the condition, I thought it was necessary to remove it. But in general, sed examples, I think, and so you will find how to replace, not delete.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question