2022-04-23 13:26:53 +01:00
---
tags:
- shell
---
2022-05-26 08:30:05 +01:00
# File permissions and executables
2022-09-06 15:44:40 +01:00
2024-02-02 15:58:13 +00:00
Every Unix file has a set of permissions that determine whether you can read,
write or run (execute) the file.
2022-09-06 15:44:40 +01:00
2022-04-23 13:26:53 +01:00
## Viewing file permissions
2024-02-02 15:58:13 +00:00
In order to see file permissions within the terminal, use the `-l` or `-rfl`
with the `ls` command. Remember this command can be applied at both the
directory and single-file level. For example:
2022-04-23 13:26:53 +01:00
2022-09-06 15:44:40 +01:00
```bash
2022-04-23 13:26:53 +01:00
drwxr-xr-x 7 thomas thomas 4096 Oct 2 19:22 angular-learning-lab
drwxr-xr-x 5 thomas thomas 4096 Oct 17 18:05 code-exercises
drwxr-xr-x 5 thomas thomas 4096 Sep 4 16:15 js-kata
drwxr-xr-x 9 thomas thomas 4096 Sep 26 18:10 sinequanon
drwxr-xr-x 12 thomas thomas 4096 Sep 19 17:41 thomas-bishop
drwxr-xr-x 5 thomas thomas 4096 Sep 4 19:24 ts-kata
2022-09-06 15:44:40 +01:00
```
2022-04-23 13:26:53 +01:00
2022-05-26 08:30:05 +01:00
### What the output means
2024-02-02 15:58:13 +00:00
The first column of the permissions output is known as the file's _mode_ . The
sequence from left to right is as follows:
2022-09-06 15:44:40 +01:00
2022-05-26 08:30:05 +01:00
```
2022-09-06 15:44:40 +01:00
- - - - - - - - - -
2022-05-26 08:30:05 +01:00
type user permissions group permissions other permissions
```
2022-09-06 15:44:40 +01:00
2022-05-26 08:30:05 +01:00
< dl >
< dt > type< / dt >
< dd > The file type. A dash just means an ordinary file. `d` means directory </ dd >
< dt > user permissions< / dt >
< dd > read, write or execute. A dash means 'nothing': the permissions for that slot in the set have not be assigned< / dd >
< dt > group and other< / dt >
< dd > group is obviously what anyone belonging to the current file's user group can do. Everyone else (outside of the user and the group) is covered by the other permissions, sometimes known as 'world' permissions< / dd >
< / dl >
## Modifying permissions: `chmod`
2022-04-23 13:26:53 +01:00
2024-02-02 15:58:13 +00:00
We use `chmod` for transferring ownership and file permissions quickly from the
command-line.
2022-04-23 13:26:53 +01:00
### Octal notation
2024-02-02 15:58:13 +00:00
`chmod` uses octal notation. Each numeral refers to a permission set. There are
three numerals. The placement denotes the user group. From left to right this
is:
2022-04-23 13:26:53 +01:00
2022-09-06 15:44:40 +01:00
- user
- group
- everyone else.
2022-04-23 13:26:53 +01:00
2024-02-02 15:58:13 +00:00
If you are working solo and not with group access to files, you can disregard
assigning the other numerals, by putting zeros in as placeholders.
2022-04-23 13:26:53 +01:00
2024-02-17 11:57:44 +00:00
[Permission codes ](685254916b2642f189e6316b876e09c9 )
2022-04-23 13:26:53 +01:00
### Example
2022-09-06 15:44:40 +01:00
```bash
2022-04-23 13:26:53 +01:00
$ chmod -v 700 dummy.txt
$ ls -l dummy.txt
$ -rwx------ 1 thomasbishop staff 27 13 May 15:42 dummy.txtExample
2022-09-06 15:44:40 +01:00
```
2022-04-23 13:26:53 +01:00
### Useful options
`-v` → verbose: tell the user what `chmod` is doing
`-r` → work recursively, i.e apply the action to directories as well as files
`-f` →silent: suppress most error messages
## Running bash files
2024-02-02 15:58:13 +00:00
In most cases, especially when you are working alone, the most frequent codes
you are going to need are 700 and 600. When shell scripting, you need to make
your scripts executable for them to work, therefore you should always
`chmod 700` when creating a `.sh` file.
2022-04-23 13:26:53 +01:00
Then to invoke the script from the shell you simply enter:
2022-09-06 15:44:40 +01:00
```bash
2022-04-23 13:26:53 +01:00
./your-bash-script.sh
2022-09-06 15:44:40 +01:00
```