Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save muath-ye/4cb9e35e83562d8e188c6cad527bc9cc to your computer and use it in GitHub Desktop.
Save muath-ye/4cb9e35e83562d8e188c6cad527bc9cc to your computer and use it in GitHub Desktop.

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:

  1. 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
  2. Set Directory Permissions to 755
    Use the find command to locate all directories and apply the permission change:

    find . -type d -exec chmod 755 {} \;
    • . tells find to start in the current directory.
    • -type d restricts the operation to directories.
    • -exec chmod 755 {} \; executes chmod 755 on each found directory ({} is a placeholder for the file name).
  3. Set File Permissions to 644
    Similarly, use find to locate all files and set their permissions:

    find . -type f -exec chmod 644 {} \;
    • -type f restricts the operation to files.

Explanation

  • 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 {} \; \)

How It Works

  • find .
    Starts searching from the current directory.

  • \( -type d -exec chmod 755 {} \; \)
    For each item that is a directory (-type d), it executes chmod 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 executes chmod 644.

This single command will recursively set directories to 755 and files to 644 throughout the directory tree starting at the current location.


Alternate Single-Command Option

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment