You can iterate through the directories you'd like to modify, then use os.walk
to iterate over all of the directories and files in each of these directories, like so:
for your_dir in dirs_to_modify:
for root, dirs, files in os.walk(your_dir):
for d in dirs:
os.chmod(os.path.join(root, d), 0o777)
for f in files:
os.chmod(os.path.join(root, f), 0o777)
I want to make an chmod command that's recursively will apply the privilegies to all directoris under that. Example:,Server Fault is a question and answer site for system and network administrators. It only takes a minute to sign up.,I want that every directory under "/usr" turn into permissions to 666., Does the EPL 2.0 allow recipients to use code under the terms of any later version of the license?
First, please learn to read man pages:
$ man chmod
Partway down that page, you will see:
-R, --recursive
change files and directories recursively
chmod -R 777 /www/store. The -R (or --recursive) options make it recursive.,Or if you want to make all the files in the current directory have all permissions type:,This will give permissions to all files currently in the folder and files added in the future without giving permissions to the directory itself.,Within store are several files and folders. I want to give the folder store and all files and folders within the store folder all permissions.
Navigate to folder and
chmod - R 777 .
Ideally, give 755
permission for security reasons to the web folder.
sudo chmod - R 755 / www / store
Each number has meaning in permission. Do not give full permission.
N Description ls binary
0 No permissions at all-- - 000
1 Only execute--x 001
2 Only write - w - 010
3 Write and execute - wx 011
4 Only read r--100
5 Read and execute r - x 101
6 Read and write rw - 110
7 Read, write, and execute rwx 111
To recursively give directories read&execute privileges:, 9 I think the most correct (but not fastest) version with respect to spaces and symbols in filenames and number of files is find /path/to/base/dir -type d -exec chmod 755 {} \; (find /path/to/base/dir -type f -exec chmod 644 {} \;). – Peter K Feb 21, 2018 at 11:49 ,It basically does the recursive chmod but also provides a bit of flexibility for command line options (sets directory and/or file permissions, or exclude both it automatically resets everything to 755-644). It also checks for a few error scenarios., 29 This pattern won't fix the situation when someone has done chmod -R 777 since the +X option will not reset existing execute bits on files. Using -x will reset directories, and prevent descending into them. – Andrew Vit Aug 7, 2012 at 4:57
To recursively give directories read&execute privileges:
find / path / to / base / dir - type d - exec chmod 755 {} +
To recursively give files read privileges:
find / path / to / base / dir - type f - exec chmod 644 {} +
Or, if there are many objects to process:
chmod 755 $(find / path / to / base / dir - type d)
chmod 644 $(find / path / to / base / dir - type f)
A common reason for this sort of thing is to set directories to 755 but files to 644. In this case there's a slightly quicker way than nik's find
example:
chmod - R u + rwX, go + rX, go - w / path
Example:
chmod - R ugo - x, u + rwX, go + rX, go - w path
To recursively give directories read&execute privileges:
find / path / to / base / dir - type d - exec chmod 755 {}\;
To recursively give files read privileges:
find / path / to / base / dir - type f - exec chmod 644 {}\;
chmodr.sh
#!/bin/sh # # chmodr.sh # # author: Francis Byrne # date: 2011 / 02 / 12 # # Generic Script for recursively setting permissions for directories and files # to defined or default permissions using chmod. # # Takes a path to recurse through and options for specifying directory and / or # file permissions. # Outputs a list of affected directories and files. # # If no options are specified, it recursively resets all directory and file # permissions to the default for most OSs(dirs: 755, files: 644). # Usage message usage() { echo "Usage: $0 PATH -d DIRPERMS -f FILEPERMS" echo "Arguments:" echo "PATH: path to the root directory you wish to modify permissions for" echo "Options:" echo " -d DIRPERMS, directory permissions" echo " -f FILEPERMS, file permissions" exit 1 } # Check if user entered arguments if [$ # - lt 1]; then usage fi # Get options while getopts d: f: opt do case "$opt" in d) DIRPERMS = "$OPTARG";; f) FILEPERMS = "$OPTARG";;\ ? ) usage;; esac done # Shift option index so that $1 now refers to the first argument shift $(($OPTIND - 1)) # Default directory and file permissions, if not set on command line if [-z "$DIRPERMS"] && [-z "$FILEPERMS"]; then DIRPERMS = 755 FILEPERMS = 644 fi # Set the root path to be the argument entered by the user ROOT = $1 # Check if the root path is a valid directory if [!-d $ROOT]; then echo "$ROOT does not exist or isn't a directory!"; exit 1 fi # Recursively set directory / file permissions based on the permission variables if [-n "$DIRPERMS"]; then find $ROOT - type d - print0 | xargs - 0 chmod - v $DIRPERMS fi if [-n "$FILEPERMS"]; then find $ROOT - type f - print0 | xargs - 0 chmod - v $FILEPERMS fi
Try this python script; it requires no spawning of processes and does only two syscalls per file. Apart from an implementation in C, it will probably be the fastest way of doing it (I needed it to fix a filesystem of 15 million files which were all set to 777)
#!/usr/bin/python3 import os for par, dirs, files in os.walk('.'): for d in dirs: os.chmod(par + '/' + d, 0o755) for f in files: os.chmod(par + '/' + f, 0o644)
file.info, file.exists, file.path, list.files, unlink, basename, path.expand. ,See also the section in the help for file.exists on case-insensitive file systems for the interpretation of path and paths. ,How modes are handled depends on the file system, even on Unix-alikes (although their documentation is often written assuming a POSIX file system). So treat documentation cautiously if you are using, say, a FAT/FAT32 or network-mounted file system. ,dir.create and Sys.chmod return invisibly a logical vector indicating if the operation succeeded for each of the files attempted. Using a missing value for a path name will always be regarded as a failure. dir.create indicates failure if the directory already exists. If showWarnings = TRUE, dir.create will give a warning for an unexpected failure (e.g., not for a missing value nor for an already existing component for recursive = TRUE).
Usage
dir.exists(paths)
dir.create(path, showWarnings = TRUE, recursive = FALSE, mode = "0777")
Sys.chmod(paths, mode = "0777", use_umask = TRUE)
Sys.umask(mode = NA)
Examples
# # Not run:
# # Fix up maximal allowed permissions in a file tree
Sys.chmod(list.dirs("."), "777")
f < -list.files(".", all.files = TRUE, full.names = TRUE, recursive = TRUE)
Sys.chmod(f, (file.info(f) $mode | "664"))
# # End(Not run)
can u write the command for making /var/www/ writable and giving it permission such that i get same permission (777) for site i recently add in it. – kamal Jan 20, 2011 at 9:39 ,I have changed ownership of my localhost file on /var/www/ and its sub folders and given it permission 777. However, whenever I add a new folder in it the new folder does not automatically get that permission. How can I give a folder 777 permission forever so that if I add a new folder or file it gets the same permission?,And umask is answer for Your question. It can be used to restrict default privileges for newly created folders and files. And distribution developers tend to use it to restrict access to some system folders.,Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.
I think you want to have write access to /var/www
to modify files and dirs. I think the best solution is to install apache2-mpm-itk
and in the virtual host config file add /etc/apache2/sites-available/default
:
<IfModule mpm_itk_module>
AssignUserId your-username your-group
</IfModule>