Handbooks / Linux / Chapter 1

Filesystem and Permissions

7 pages · ~5 min✓ Reviewed

Start here. Next up: Text Processing.

LinuxPage 1

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
DirHolds
/etcconfig files
/varlogs, changing data
/homeuser files
/usrinstalled programs
/tmpscratch space
/devdevices
/prockernel and process info

2. Paths and Shortcuts

  • Absolute starts with /: /var/log/syslog
  • Relative starts from the current directory: ../logs
SymbolMeans
.current dir
..parent dir
~your home
-previous dir (cd -)

3. Common Mistakes

  • Paths are case-sensitive: Docs is not docs
  • A leading dot hides a file: .bashrc
Spaces

Spaces need quotes or a backslash: cd "My Docs" or cd My\ Docs

LinuxPage 2

Everyday File Commands: ls, cd, cp, mv, rm, find

1. Look Around

  • ls -lah: long format, all files incl. hidden, human-readable sizes
  • pwd prints where you are

2. Copy, Move, Delete

CommandDoesNote
cp -r src/ dest/copies directoriesneeds -r
cp -aalso keeps permissions, timestampsarchive copy
mv old.txt new.txtmoves and renamesnever needs -r
rm -r dirdeletes recursivelyno trash, no undo
  1. ls /path/check the target
  2. rm -r /path/no undo
Danger line

rm -rf /path/ is the one to fear. Run ls on the path first.

3. find

  • find matches 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

LinuxPage 3

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
BitFileDirectory
rread contentslist names
wmodify contentscreate or delete entries
xrun itcd into it

2. chmod

  • Octal sets all bits; symbolic changes only the bits named
  • Symbolic: who u g o a, op + - =, bits rwx
bash
chmod 755 script.sh
chmod u+x file
chmod g-w file
chmod o=r file
chmod a+r file

octal first, then symbolic

3. Mistakes

  • chmod 777 opens the file to everyone; give the exact bit needed
  • chmod -R 755 gives plain files x too: the mode is applied as given
Capital X

chmod -R u=rwX,go=rX dir adds x to directories only.

LinuxPage 4

Users, Groups, chown and sudo

1. Owners and accounts

  • Each file has one owner user and one owner group
  • id and whoami show who you are
  • Accounts: /etc/passwd; groups: /etc/group

2. chown, chgrp, groups

CommandEffectWho
chown alice:devs fowner + grouproot only
chown -Rrecursesroot only
chgrp devs fgroup onlyowner, if member
bash
sudo usermod -aG docker alice

without -a, other groups are dropped; log in again

3. sudo

  • sudo cmd runs as root with your password
  • Only if you are listed in /etc/sudoers
  • sudo -i opens a root shell
  • Edit the list only with visudo

4. Mistakes

  • sudo on 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

LinuxPage 5

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 linkSymlink
Makeln target linkln -s target link
Is2nd name, same inodesmall file holding a path
Target deleteddata survives to last namedangling
Cross fs / dirsnoyes
ls -lplain -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 -s order: target first, then link name
  • Relative target resolves from the link's folder, not your cwd
LinuxPage 6

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

OctalBits
7rwx
6rw-
5r-x
4r--
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; id lists your groups
  • sudo -i root shell; visudo edits 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.

LinuxPage 7

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 h prints the contents: the inode lives on while a name remains.
  • cat s fails with no such file: the symlink is dangling.
ln data.txt h
ln -s data.txt s
rm data.txt
cat h; cat s