Post

File permissions linux cheatsheet

How to update your file permissions on linux.

A Linux file system is made up of users, groups and files. The files need the appropriate permissions so that users can work with them.

File permissions

In order to create, remove, update and delete files on your linux system. Files and folders need to have the correct permissions. Heres a list of some of the most common commands.

List users of the file system

1
less /etc/passwd

List all files and folders in current directory with file permissions and ownerships

1
ls -l

Change permissions of a single file or folder

1
chmod 755 filename,

Change all files and folder permissions

Recursively change all the files in the directory permissions to 755, and all file permissions to 644:

1
2
sudo find /var/www/html/ -type d -exec chmod 775 {} \;
sudo find /var/www/html/ -type f -exec chmod 664 {} \;

View the permission settings of a specific directory

1
ls -ld directory_name

View information on a file

This will output lots of file information but also permission information in octal format

  • see mode in the output
1
stat filename

Return the numeric value of a directory

1
stat -c %a directory_name

See what groups a user belongs to

1
id -Gn user

Change file and folder ownership to the current logged in user Recursively

1
sudo chown -R $USER:www-data /var/www/html

How symbolic permissions map to numeric codes:

1
2
3
r (read) is 4
w (write) is 2
x (execute) is 1

So:

1
2
3
rwx (read, write, execute) is 7
r-x (read, execute) is 5
r-- (read only) is 4

For example, rwxr-xr-x corresponds to:

1
2
3
Owner: rwx = 7
Group: r-x = 5
Others: r-x = 5
This post is licensed under CC BY 4.0 by the author.