How To Copy Permissions From One File to Another On Linux

To copy permissions from one file to another in Linux, you can use the chmod command in combination with the stat command. Here’s how you can do it:

Step 1: Using stat and chmod: First, get the permissions of the source file using stat:

stat -c "%a" source_file

    This will return the permissions of source_file in octal format.

    Then, apply these permissions to the target file using chmod:

    chmod $(stat -c "%a" source_file) target_file

    Using a one-liner: You can combine the above commands into a single one-liner:

    chmod --reference=source_file target_file

    The --reference option allows chmod to take the permissions from the file specified (in this case, source_file) and apply them to target_file.

    Remember to replace source_file with the name of the file from which you want to copy the permissions, and target_file with the name of the file to which you want to apply the permissions.

    Leave a Comment

    Scroll to Top