File and Directory Permissions
ls -l shows the permissions string to the left.
broken down into 4 groups, e.g. drwxr-xr-x or -rw-rw-r--.
First group is a single character;
d: directory
-: file
l: link
next groups are 3 characters each. In order, they are:
permissions for the user, the group and the other / world / everybody else
r: read
w: write
x: execute
Modify permissions
Suppose file testfile.txt contains the command ls -l,
make file executable for everybody:
chmod +x testfile.txt
remove execution permission for user:
chmod u-x testfile.txt
remove execution permission for group:
chmod g-x testfile.txt
remove execution permission for other:
chmod o-x testfile.txt
make file executable for user:
chmod u+x testfile.txt
remove read permission for group:
chmod g-r testfile.txt
remove read permission for other:
chmod o-r testfile.txt
remove write permission for group:
chmod g-w testfile.txt
example:
chmod u-w testfile.txt
will make the following command fail:
echo "https://github.com/TheNewThinkTank/code-vault" >> testfile.txt
chmod u+w testfile.txt
echo "https://github.com/TheNewThinkTank/code-vault" >> testfile.txt
now the content was appended to the file.
Combining chmod commands
Allow group to read and write with a single command:
chmod g+rw testfile.txt
Bit scores - numerical representations of r, w and x
r: 4
w: 2
x: 1
example
chmod 770 testfile.txt
results in permission string
-rwxrwx---
generally, chmod ??? testfile.txt
with ??? equal to ugo, user, group and other.
example
ls -l Downloads/
Recursively change permissions for all files in dir, without affecting the dir:
chmod 600 Downloads/*
Use with caution, applies to everything beneath the object:
chmod -R 700 Downloads
Or use find command for more granular controls
Change ownership
Change ownership of all files in Downloads/ dir to new-user and new-user-group
sudo chown -R new-user:new-user-group Downloads/
Shorthand (leaving out group defaults to the user's group):
sudo chown -R new-user: Downloads/