Filesystem and Permissions
7 pages · ~5 min✓ Reviewed
Start here. Next up: Text Processing.
Filesystem Hierarchy and Paths
1. One Tree, Rooted at /
- Everything hangs off one tree rooted at
/ - No drive letters: disks and devices are mounted into the tree
| Dir | Holds |
|---|---|
/etc | config files |
/var | logs, changing data |
/home | user files |
/usr | installed programs |
/tmp | scratch space |
/dev | devices |
/proc | kernel and process info |
2. Paths and Shortcuts
- Absolute starts with
/:/var/log/syslog - Relative starts from the current directory:
../logs
| Symbol | Means |
|---|---|
. | current dir |
.. | parent dir |
~ | your home |
- | previous dir (cd -) |
3. Common Mistakes
- Paths are case-sensitive:
Docsis notdocs - A leading dot hides a file:
.bashrc
Spaces
Spaces need quotes or a backslash: cd "My Docs" or cd My\ Docs
Everyday File Commands: ls, cd, cp, mv, rm, find
1. Look Around
ls -lah: long format, all files incl. hidden, human-readable sizespwdprints where you are
2. Copy, Move, Delete
| Command | Does | Note |
|---|---|---|
cp -r src/ dest/ | copies directories | needs -r |
cp -a | also keeps permissions, timestamps | archive copy |
mv old.txt new.txt | moves and renames | never needs -r |
rm -r dir | deletes recursively | no trash, no undo |
- ls /path/check the target
- rm -r /path/no undo
Danger line
rm -rf /path/ is the one to fear. Run ls on the path first.
3. find
findmatches by name, age and type- Unlike
locate, it walks the live disk - Add
-exec rm {} \;to act on each result
bash
find /var -name "*.log" -mtime -1 -type f find /var -name "*.log" -mtime -1 -type f -exec rm {} \;
-mtime -1 = changed within the last day
Permissions: rwx and Octal
1. Reading a Mode
-rwxr-xr--= type, owner, group, others- r=4, w=2, x=1; rwx=7, r-x=5, rw-=6, r--=4
640= owner rw, group r, others none
| Bit | File | Directory |
|---|---|---|
r | read contents | list names |
w | modify contents | create or delete entries |
x | run it | cd into it |
2. chmod
- Octal sets all bits; symbolic changes only the bits named
- Symbolic: who
u g o a, op+ - =, bitsrwx
bash
chmod 755 script.sh
chmod u+x file
chmod g-w file
chmod o=r file
chmod a+r fileoctal first, then symbolic
3. Mistakes
chmod 777opens the file to everyone; give the exact bit neededchmod -R 755gives plain files x too: the mode is applied as given
Capital X
chmod -R u=rwX,go=rX dir adds x to directories only.
Users, Groups, chown and sudo
1. Owners and accounts
- Each file has one owner user and one owner group
idandwhoamishow who you are- Accounts:
/etc/passwd; groups:/etc/group
2. chown, chgrp, groups
| Command | Effect | Who |
|---|---|---|
chown alice:devs f | owner + group | root only |
chown -R | recurses | root only |
chgrp devs f | group only | owner, if member |
bash
sudo usermod -aG docker alice
without -a, other groups are dropped; log in again
3. sudo
sudo cmdruns as root with your password- Only if you are listed in
/etc/sudoers sudo -iopens a root shell- Edit the list only with
visudo
4. Mistakes
sudoon everything leaves root-owned files in ~- Fix ownership with
chown, don't run as root
Redirect runs as you
sudo echo x > f fails. Use echo x | sudo tee f
Links: Hard vs Symbolic
1. Names and Inodes
- Each file name points to an inode
- Inode = metadata + pointers to the data blocks
- A link is another name or pointer to it
2. Hard vs Symbolic
| Hard link | Symlink | |
|---|---|---|
| Make | ln target link | ln -s target link |
| Is | 2nd name, same inode | small file holding a path |
| Target deleted | data survives to last name | dangling |
| Cross fs / dirs | no | yes |
ls -l | plain - | l and -> |
bash
ln a.txt hard.txt ln -s a.txt soft.txt ls -li
-i shows the inode number
3. Common Mistakes
ln -sorder: target first, then link name- Relative target resolves from the link's folder, not your cwd
Cheat Sheet: Files, Permissions, Users and Links
1. Navigate and Manage
pwd,cd -,ls -lah,tree -L 2- Paths:
/absolute,../parent,~home
bash
cp -a src dst # keeps perms, times mv old new # move or rename rm -r dir # no undo mkdir -p a/b/c touch f find . -name "*.py" -type f
2. Octal Modes
| Octal | Bits |
|---|---|
| 7 | rwx |
| 6 | rw- |
| 5 | r-x |
| 4 | r-- |
| 0 | --- |
- 644 files, 755 scripts and dirs, 600 SSH keys, 700 private dirs
3. Access, Users and Links
chmod u+x f;chown user:group f(new owner needs root)usermod -aG group user;idlists your groupssudo -iroot shell;visudoedits sudoers- Hard link: second name, same inode. Symlink: a path pointer
bash
ln target hard ln -s target link ls -li # inode numbers readlink -f link # resolve symlink
Mistakes
chmod -R 755 makes plain files executable: use X. usermod without -a drops other groups.
Check yourself
A file is -rw-r----- alice devs. Bob is in devs. Can Bob read it? Write to it?
- Read: yes, group bits are
r--. - Write: no, the group has no
w.
sudo echo x > /etc/motd says permission denied. Why, and what fixes it?
- The redirect
>is done by your shell, not by sudo. - Fix:
echo x | sudo tee /etc/motd.
After these commands, what do the two cat lines show?
cat hprints the contents: the inode lives on while a name remains.cat sfails with no such file: the symlink is dangling.
ln data.txt h ln -s data.txt s rm data.txt cat h; cat s