I want to change permission to 755 for directories and 644 for files recursivly
To change permissions recursively so that:
- Directories are set to
755
(read, write, execute for owner and read, execute for group and others), and - Files are set to
644
(read and write for owner, read-only for group and others),
follow these steps:
-
Navigate to Your Repository or Target Directory
Change directory to the root of your repository (or the folder where you want to update the permissions):cd /www/wwwroot/7seen
-
Set Directory Permissions to 755
Use thefind
command to locate all directories and apply the permission change:find . -type d -exec chmod 755 {} \;
.
tellsfind
to start in the current directory.-type d
restricts the operation to directories.-exec chmod 755 {} \;
executeschmod 755
on each found directory ({}
is a placeholder for the file name).
-
Set File Permissions to 644
Similarly, usefind
to locate all files and set their permissions:find . -type f -exec chmod 644 {} \;
-type f
restricts the operation to files.
-
Directories (755):
- Owner: read, write, and execute
- Group: read and execute
- Others: read and execute
-
Files (644):
- Owner: read and write
- Group: read
- Others: read
After running these commands, all directories under /www/wwwroot/7seen
will have 755
permissions, and all files will have 644
permissions. If you need to verify the changes, you can list a few items using:
find . -type d -exec ls -ld {} \; | head -n 5
find . -type f -exec ls -l {} \; | head -n 5
This will display a few of the directories and files with their updated permission settings.
I want one command for 'find . -type d -exec chmod 755 {} ;' and 'find . -type f -exec chmod 644 {} ;'
You can combine both operations into a single command using the logical OR operator (-o
) in find
. For example, try:
find . \( -type d -exec chmod 755 {} \; \) -o \( -type f -exec chmod 644 {} \; \)
-
find .
Starts searching from the current directory. -
\( -type d -exec chmod 755 {} \; \)
For each item that is a directory (-type d
), it executeschmod 755
. -
-o
Acts as a logical OR. If the first test fails (i.e., the item is not a directory), it moves on to the next test. -
\( -type f -exec chmod 644 {} \; \)
For each item that is a file (-type f
), it executeschmod 644
.
This single command will recursively set directories to 755
and files to 644
throughout the directory tree starting at the current location.
Another approach is to use a shell snippet with find
that tests each file:
find . -exec sh -c 'for f; do
if [ -d "$f" ]; then
chmod 755 "$f"
elif [ -f "$f" ]; then
chmod 644 "$f"
fi
done' _ {} +
This command runs a shell for a batch of files ({} +
), checking if each is a directory or a file, and applies the corresponding permission.
Choose the method that best fits your environment. The first solution is more concise, while the second gives you more control if you need to extend the logic further.