Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6008537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:50:20+00:00 2026-05-23T01:50:20+00:00

On a linux server I’m working on I need to create directories with the

  • 0

On a linux server I’m working on I need to create directories with the following structure:

/dir1/dir2/dir3/YYYY/MM/DD/file.ext

There are certain cases (since the directories depend on the date) that I need to create more than a single directory at a time, such as …/2011/01/01/, all of them needing the permission 0o2755.

When I use os.makedirs(dir, mode=0o2755) on my server it properly sets the 0o755 part of the permission, but ignores the SGID (2) bit. I’m guessing that this is because of os.makedirs ignoring permissions on some systems, as the documentation describes. I’ve read that other people have had problems with umask and os.makedirs(), but I’ve played around with umask (its set to 0002, I’ve tried 0000) and it still ignores the SGID bit.

My solution for this is the following function:

def special_makedirs(base_dir, target_dir, mode=0o2755):
    base_dir = os.path.abspath(base_dir)
    target_dir = os.path.abspath(target_dir)
    if os.path.commonprefix([base_dir, target_dir]) != base_dir:
        log.error("Target directory %s is not a subdirectory of %s" %(target_dir, base_dir))
        raise ValueError("Target directory %s is not a subdirectory of %s" % (target_dir, base_dir))

    # Create initial directory
    os.makedirs(target_dir, mode=mode)

    # Verify permissions
    temp = target_dir
    while temp != base_dir and stat.S_IMODE(os.stat(temp)) != mode:
        os.chmod(temp, mode)
        temp = os.path.split(temp)[0]

Now I’m sure this doesn’t meet all cases, but I’ll be the only one using this function for my specific need.

So…my questions are:

  1. Am I misunderstanding the os.makedirs documentation when it says that it ignores permissions on some systems? Have others had this problem?

  2. Does my function seem like the most efficient way to fix this problem?

Thanks for any tips you can give. Sorry if my situation doesn’t make sense.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:50:20+00:00Added an answer on May 23, 2026 at 1:50 am

    From the mkdir(2) man page:

    NOTES
            Under  Linux  apart from the permission bits, only the S_ISVTX mode bit
            is honored.  That is, under Linux the created directory  actually  gets
            mode (mode & ~umask & 01777).
    

    So yes, SGID is ignored. Set it after the fact.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to scan uploaded files for viruses on a Linux server, but I'm
I am working in PHP on a Linux server with MySQL. I have a
I´am with a problem for search a file on a linux server, i try
I have been working with linux server using epoll and almost finished it. And
I am working on a linux server programming and I am not sure the
I am working on a networking program and designing a Linux server using C++.
On one Linux Server running Apache and PHP 5, we have multiple Virtual Hosts
We have a Linux server application that is comprised of a number of open-source
I have a Linux server with multiple ips (so, multiple eth0, eth0:0, eth0:1 etc).
I'm running MySQL 5 on a linux server on my local network. Running windows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.