Home > Software design >  Golang Change permission "os.chmod" and "os.chowm" recursively
Golang Change permission "os.chmod" and "os.chowm" recursively

Time:09-28

I am trying to change ownership and permission of files and directory using os.Chmod and os.Chown. How can I do this recursively. For Illustration linux equivalent of this chmod -R and chown -R

CodePudding user response:

func ChownRecursively(root string) {
    err := filepath.Walk(root,
        func(path string, info os.FileInfo, err error) error {
            if err != nil {
                return err
            }
            err = os.Chown(path, os.Getuid(), os.Getgid())
            if err != nil {
                return err
            } else {
                fmt.Printf("File ownership of %s changed.\n", path)
            }
            return nil
        })
    if err != nil {
        log.Println(err)
    }
}
  • Related