Skip to content

Commit 4123473

Browse files
committed
discuss file permissions
1 parent 95c6149 commit 4123473

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

02-Bash-Basics.md

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,68 @@ drwx------ 2 nmjxv3 mst_users 0 Dec 28 2015 oclint-0.10.2
153153
-rwxr-xr-x 1 nmjxv3 mst_users 960 Jan 15 2016 vector.hpp
154154
```
155155

156-
The first column shows file permissions --- who can read, write, or execute your files; the fifth file size; the sixth the last time the file was modified; and the last the name of the file itself.
156+
The columns in `ls`'s output are as follows:
157+
158+
1. File permissions --- who can read, write, or execute your files
159+
2. Number of hard links (don't worry about this for now)
160+
3. The user who owns the file
161+
4. The group that owns the file
162+
5. The file size
163+
6. The last time the file was modified
164+
7. The file name.
157165

158166
Another `ls` option lets you show hidden files. In Linux, every file whose name begins with a `.` is a **hidden** file.[^dotfiles]
159167
(This is the reason that many configuration files, such as `.vimrc`, are named starting with a `.`.)
160168
To include these files in a directory listing, use the `-a` flag.
161169
You may be surprised by how many files show up if you run `ls -a` in your home directory!
162170

171+
#### A brief note on file permissions
172+
173+
Linux has separate permissions for the user who owns the file, users in the 'group' that owns the file, and everyone else.
174+
(Group permissions are useful in the case of shared documents --- imagine making an `accounting` group that allows
175+
all accountants in a company to edit various spreadsheets on a shared drive.)
176+
177+
For each of these collections of users (the owning user, the users in the owning group, and other users), you can set whether
178+
those users can `r`ead, `w`rite, or e`x`ecute the file.
179+
(Setting e`x`ecute on a directory allows users to `cd` into it, which is why directories almost always are marked executable.)
180+
181+
To change file permissions (also known as the "file mode"), you use the `chmod` (change mode) command like so: `chmod <mode> <filename>`.
182+
Modes are written like so: `<collection><+/-><permission>`[^octal]
183+
184+
- `<collection>` is `u` for the owning user, `g` for users in the owning group, `o` for other users, or `a` for all users
185+
- `+` adds a permission; `-` removes the permission
186+
- `<permission>` is `r` for read, `w` for write, or `x` for execute.
187+
188+
So, for example, let's say you've downloaded some cool program (`not-a-virus`) from the internet and you want to run it.
189+
190+
~~~
191+
$ ./not-a-virus
192+
bash: ./not-a-virus: Permission denied
193+
~~~
194+
195+
Darn! Guess you'll just have to install that ransomware on purpose...or, we could change the file permissions!
196+
We can see that right now, nobody can execute this cool program:
197+
198+
~~~
199+
$ ls -l ./not-a-virus
200+
-rw-r--r-- 1 nmjxv3 mst-users 31 Jan 15 00:07 ./not-a-virus
201+
~~~
202+
203+
Let's change that! We'll make it so anyone can execute that file.
204+
205+
~~~
206+
$ chmod a+x ./not-a-virus
207+
$ ls -l ./not-a-virus
208+
-rwxr-xr-x 1 nmjxv3 mst-users 31 Jan 15 00:07 ./not-a-virus
209+
~~~
210+
211+
Nice! Now you can edit the file, and everyone can read it and execute it...
212+
213+
~~~
214+
$ ./not-a-virus
215+
HACKED
216+
~~~
217+
163218
#### Change your Location with `cd`
164219

165220
Speaking of directories, if you ever forget which directory you are currently in, `pwd` (short for "print working directory") will remind you.
@@ -519,3 +574,5 @@ however, copying a directory requires `cp` to copy every file in the directory a
519574
[^still]: Okay, they never really stop looking scary, but after a while they start to feel less like a horror movie jump scare
520575
and more like the monster you just know is there in the hall waiting to eat you if you were to get out of bed.
521576
[^superficial]: The distinction is mostly superficial; under the hood they look the same to the operating system. Don't worry about it too much.
577+
[^octal]: There is another notation for these permissions that uses the octal (base 8, as opposed to base 10 or base 16) representation
578+
of the bitfield where the file permissions are stored. We won't go into it, but you can read about it in the `chmod` man page.

0 commit comments

Comments
 (0)