Linux, terminal and Git: macOS

Sefism.

Chapter 04

Permissions, processes and packages

Who can read, write and run what, what is running and how to stop it, and how to install tools and language versions cleanly.

Permission bits

Also called rwx, ls -l

What
Each file has read, write and execute permissions for its owner, its group and everyone else, shown by ls -l as, for example, rwxr-xr--. On a folder, execute means you can enter it.
Why
They decide who can change your files, whether a script can run and whether SSH accepts your private key.
Example
-rw-r--r-- means the owner can read and write, and the group and others can only read.

chmod

Also called 644, 755, 600

What
Changes permissions, either symbolically (u+x, go-w) or numerically, where read is 4, write 2 and execute 1 for each of owner, group and others.
Why
Makes scripts runnable and keeps private files, such as SSH keys, private.
Shell
chmod +x deploy.sh
chmod 644 index.html   # rw-r--r--
chmod 755 bin/tool     # rwxr-xr-x
chmod 600 ~/.ssh/id_ed25519
chmod -R go-w project/

Watch out

Using chmod 777 to make an error go away, which lets anyone change the file.

Ownership and chown

Also called chown, owner, group

What
Every file belongs to a user and a group. chown user:group file changes them, usually with sudo.
Why
Files created with sudo end up owned by root, which later causes "permission denied" in your own home.
Shell
ls -l
sudo chown -R "$USER":"$USER" ~/code/app
id

Watch out

Changing ownership of system folders such as /usr to fix a single tool.

root, sudo, users and groups

Also called root user, superuser, sudo, id, usermod

What
root can do anything. sudo runs one command as root after asking for your password. Users belong to groups, and group membership grants shared access, such as the sudo or docker group.
Why
Working as a normal user and using sudo only when needed limits the damage of mistakes and malware.
Shell
sudo apt install nginx
id
groups
sudo usermod -aG docker "$USER"   # log out and in after

Watch out

Using sudo for npm, pip or Git inside your projects, which leaves root-owned files behind.

Processes

Also called PID, ps, top, htop

What
A running program, with a process ID, an owner and a parent. ps aux lists them and top or htop show live CPU and memory use.
Why
Stuck programs, high CPU and "port already in use" usually come down to a process you need to find.
Shell
ps aux | grep -i node
pgrep -fl python
top          # q to quit
htop         # install it first

Watch out

Assuming closing a terminal window stopped the server you started in it.

Signals and kill

Also called signal, SIGTERM, SIGKILL, SIGINT, kill -9

What
Messages sent to a process. Ctrl+C sends SIGINT, kill PID sends SIGTERM (please stop cleanly), kill -9 PID sends SIGKILL (stop now, no clean-up).
Why
Programs can catch SIGTERM to save state and close connections; SIGKILL gives them no chance.
Shell
kill 4821
kill -9 4821      # only if it ignores SIGTERM
pkill -f "node server.js"

Watch out

Reaching for kill -9 first and leaving lock files or half-written data behind.

Jobs: foreground and background

Also called &, fg, bg, Ctrl+Z, nohup

What
cmd & starts a job in the background, Ctrl+Z pauses the foreground job, bg resumes it in the background, fg brings it back and jobs lists them.
Why
You can keep using the terminal while something long runs.

Watch out

Background jobs still end when the terminal closes; use tmux or nohup for anything that must survive.

Installing developer tools

Also called virtual environment, pip, npm, venv

What
System tools come from the system package manager; project libraries come from language managers such as pip and npm, installed per project.
Why
Keeping the layers separate avoids breaking system tools and makes projects reproducible.
Shell
brew install python gh ripgrep
python3 -m venv .venv && source .venv/bin/activate
pip install requests
npm install express        # local to the project

Watch out

sudo pip install or sudo npm install -g, which can break the system and leave root-owned files.

Version managers

Also called nvm, pyenv, mise, .nvmrc

What
Tools that install several versions of a language in your home folder and switch between them by changing PATH, such as nvm for Node and pyenv for Python.
Why
Different projects need different versions, and a .nvmrc or .python-version file records the one a project expects.
Shell
nvm install 22
nvm use 22
node -v > .nvmrc
nvm use            # reads .nvmrc
pyenv install 3.12
pyenv local 3.12

Watch out

Installing the same language through the system package manager and a version manager, then not knowing which runs.

Disk and system checks

Also called df, du, free, uname

What
df -h shows free space per file system, du -sh shows how big folders are, free -h shows memory on Linux and uname -a shows the kernel.
Why
A full disk or exhausted memory causes baffling failures in builds, databases and Git.
Shell
df -h
du -sh * | sort -h
du -sh ~/.cache ~/code/*/node_modules
free -h       # Linux and WSL
uptime

Watch out

Deleting files at random instead of measuring what is actually taking the space.

04 · Permissions, processes and packages4 / 9