Copy Permissions from One File to Another on Linux

Copy Permissions from One File to Another on Linux

Linux uses a permission system that determines who can read, write, or execute a file. If you have a file with specific permissions and want another file to have the same settings, you don’t need to manually adjust them. Instead, you can use a single command to instantly replicate the permissions. This tutorial explains how to copy permissions from one file to another on Linux.

Create two files for testing:

touch main.txt other.txt

Set specific permissions on main.txt:

chmod 600 main.txt

This sets main.txt to read and write for the owner, with no permissions for others. Check the current permissions of other.txt:

ls -l other.txt

Example output before changing permissions:

-rw-rw-r-- 1 john john 0 Mar 18 15:26 other.txt

Copy permissions from main.txt to other.txt:

chmod --reference main.txt other.txt

This command applies the same permissions from main.txt to other.txt, eliminating the need to set them manually. Verify the updated permissions:

ls -l other.txt

Example output after copying permissions:

-rw------- 1 john john 0 Mar 18 15:26 other.txt

Now, other.txt has the same 600 permission as main.txt.

Leave a Comment

Cancel reply

Your email address will not be published.