How to change file permissions on linux?

Member

by jewel , in category: General Help , a year ago

How to change file permissions on linux?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by johann , a year ago

@jewel 

To change file permissions on Linux, you can use the chmod command. The chmod command allows you to modify the read, write, and execute permissions for the owner, group, and others.


Here is the basic syntax for the chmod command:

1
chmod options permissions filename


Options:

  • -v: Verbose mode, displays a message for each file processed.
  • -c: Like verbose mode, but displays a message only if a change is made.
  • -R: Recursively change permissions for directories and their contents.


Permissions:

  • u: User/owner permissions.
  • g: Group permissions.
  • o: Other (everyone else) permissions.
  • a: All permissions.


Permissions can be specified using two different methods:

  1. Symbolic method: + adds permissions. - removes permissions. = sets permissions explicitly. r for read permission. w for write permission. x for execute permission.
  2. Numeric method: Each permission has a numeric value. 4 for read permission. 2 for write permission. 1 for execute permission.


Examples:

  1. To give read, write, and execute permissions to the owner of a file: chmod u+rwx filename
  2. To remove read and write permissions from the group and others: chmod go-rw filename
  3. To give read and execute permissions to all users: chmod a+rx filename
  4. To recursively change permissions for all files and directories in a directory: chmod -R u+rwx directory


Note: You will need appropriate permissions or superuser (root) privileges to change permissions on files/directories you don't own. Be careful while altering permissions for system files.

by buster.gaylord , 7 months ago

@jewel 

Great overview! Here is a simple example to clarify:


Let's say you have a file named 'example.txt' and you want to give read and write permissions to the owner of the file, and only read permission to the group and others, you can use the following command:

1
chmod u+rw,go+r example.txt


This command will add read and write permissions to the owner, and add read permission to the group and others for the file 'example.txt'.