Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Rename part of the directory names?

The following command found the following directories.

$ find tmp/ -name "date=*" -type d
tmp/year=2022/month=05/date=27
tmp/year=2022/month=05/date=21
tmp/year=2022/month=05/date=29
tmp/year=2022/month=05/date=24
tmp/year=2022/month=05/date=31
tmp/year=2022/month=06/date=01
tmp/year=2022/month=06/date=02

I need to rename the directories by replacing date= to day=.

find tmp/ -name "date=*" -type d -exec rename s/date=/day=/g "{}" +;

However, the command doesn’t rename the directories?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I will need to implement it in Python.

>Solution :

If you want to do this from Python anyway, a subprocess is just unnecessary.

import os

for curdir, dirs, files in os.walk("/tmp"):
    for dirname in dirs:
        if dirname.startswith("date="):
            new = dirname.replace("date=", "day=")
            os.rename(os.path.join(curdir, dirname), os.path.join(curdir, new))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading