20 Useful find One-Liners to Change File and Folder Permissions

Managing file and directory permissions is essential for maintaining both security and control in a Linux environment. The find command, when combined with chmod, offers a powerful and flexible method to apply permission changes recursively across many files or directories.

This guide provides 20 one-liner commands using find that let you adjust permissions for files and folders within the current directory. These commands are especially useful for system administrators, developers, or anyone who wants to efficiently manage permissions on a Linux system.

1. Add Execution Rights to All Files for the User

find . -type f -exec chmod u+x {} +

This command grants the user (file owner) execution rights on all regular files in the current directory and its subdirectories. It’s especially helpful when dealing with a batch of scripts or binaries that need to be made executable after being copied or downloaded.

2. Add Execution Rights to All Directories for the User

find . -type d -exec chmod u+x {} +

Directories require the execute (x) permission for the user to be able to access or list their contents. This command ensures the user can navigate into each directory found under the current path.

3. Remove Execution Rights for Group and Others on Files

find . -type f -exec chmod go-x {} +

To increase file security, this command removes execute permissions from group and other users. It’s ideal for environments where only the file owner should run specific scripts or binaries.

4. Remove Execution Rights for Group and Others on Directories

find . -type d -exec chmod go-x {} +

Removing execute permissions from directories for group and others means that only the owner can access the folders. This prevents unauthorized browsing of directories.

5. Set Full Permissions (rwx) for User on Files

find . -type f -exec chmod u=rwx {} +

This explicitly sets read, write, and execute permissions for the file owner. It’s useful when resetting files to a known safe state where the owner needs full control.

6. Set Full Permissions (rwx) for User on Directories

find . -type d -exec chmod u=rwx {} +

Same as above, but for directories. This gives the user complete access to all directories, allowing them to list contents, create files, and enter them.

7. Add Sticky Bit to All Directories

find . -type d -exec chmod +t {} +

The sticky bit restricts file deletion within a directory: only the file owner, directory owner, or root can delete files. It’s commonly used in shared spaces like /tmp.

8. Remove Sticky Bit from All Directories

find . -type d -exec chmod -t {} +

Removes the sticky bit, returning the directory to normal behavior where any user with write access can delete any file. Use cautiously in shared directories.

9. Add Setuid to All Files

find . -type f -exec chmod u+s {} +

The setuid permission causes programs to run with the privileges of the file’s owner. This is useful in specific cases, but it can be a major security risk if misused.

10. Remove Setuid from All Files

find . -type f -exec chmod u-s {} +

Removes the setuid bit from all files, preventing execution with elevated privileges. Recommended as part of routine security hardening.

11. Add Setgid to All Files

find . -type f -exec chmod g+s {} +

The setgid bit ensures files execute with the group ownership of the file, not the user’s group. It’s more common for directories, but applicable to files in specific scenarios.

12. Remove Setgid from All Files

find . -type f -exec chmod g-s {} +

This reverts any setgid-enabled files back to normal behavior. It’s a good cleanup command for files that no longer require group-based privilege escalation.

13. Remove Write for All, Set rwx for User on Files

find . -type f -exec chmod a-w,u=rwx {} +

This command secures your files by removing write access for group and others, while giving the user full control. It’s useful for personal scripts or sensitive documents.

14. Remove Write for All, Set rwx for User on Directories

find . -type d -exec chmod a-w,u=rwx {} +

Useful for locking down directory write access while ensuring the user can still manage contents inside. Ideal in shared development environments.

15. Set 755 Permissions on All Files

find . -type f -exec chmod 755 {} +

Sets files to be readable and executable by everyone, but writable only by the owner. This is a common permission set for public scripts or applications.

16. Set 644 Permissions on All Directories

find . -type d -exec chmod 644 {} +

This command removes execute permissions from directories, which makes them inaccessible (even if readable). It’s rarely used but may be appropriate in backup archives or special permission setups.

17. Copy Permissions from Another File

find . -type f -exec chmod --reference=file1 {} +

Copies permissions from a file named file1 to all other files. Great for standardizing permissions across a group of files.

18. Copy Permissions from Another Directory

find . -type d -exec chmod --reference=dir1 {} +

Applies the same permissions as dir1 to all other directories. Useful when replicating directory structures or templates.

19. Remove All Setuid and Setgid Bits from Files

find . -type f -exec chmod -s {} +

Removes both setuid and setgid bits in one go. Ideal for cleaning up inherited files from external sources.

20. Remove All Permissions for Group and Others on Files

find . -type f -exec chmod go= {} +

This command strips all group and other permissions, making files private to the owner. It’s a strong security measure for confidential data.

Conclusion

The find command paired with chmod gives you a powerful and flexible way to manage file and folder permissions at scale. Whether you’re tightening security, adjusting access for collaboration, or preparing files for deployment, these one-liners can save you time and reduce error.

Be cautious when applying permission changes recursively. Always test commands in a safe environment first, especially when working on production systems. A small mistake with chmod can lead to inaccessible data or broken services.

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *