Golang Change permission "os.chmod" and "os.chowm" recursively

1.6k Views Asked by At

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

1

There are 1 best solutions below

0
h0ch5tr4355 On BEST ANSWER
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)
    }
}