v9.1.1
Menu

Chapter 2: Linux & Shell

Master the Linux command line for managing GPU servers, automating ML workflows, and maintaining production infrastructure.

Topics
8
Demo
1
Videos
3
Min read
63

Chapter Overview

Linux is the operating system of AI. Nearly every GPU server, cloud instance, HPC cluster, and container orchestration platform runs Linux. Your ability to navigate the filesystem, manage processes, write automation scripts, and configure services directly determines how productive you are as an ML engineer. Engineers who are comfortable on the command line ship experiments faster, debug problems more quickly, and build more reliable infrastructure.

This chapter covers the essential Linux skills for AI engineering, organized in order of daily usage. You will start with the filesystem — understanding where things live and how permissions work. Then move to the commands you will use hundreds of times a day for file manipulation, searching, and piping. Process management teaches you how to keep training jobs alive and monitor GPU resources. Users and permissions set up shared infrastructure for team collaboration. Shell scripting automates everything you do manually. Package management prevents dependency hell. Cron and systemd handle scheduling and service management. And environment variables keep your configuration secure and portable.

Each topic is taught through the lens of real ML workflows — not abstract system administration. You will learn to organize training projects, analyze logs with command-line tools, manage multi-user GPU servers, write experiment launcher scripts, and deploy model servers as auto-restarting services. These are the skills that distinguish an ML engineer who gets things done from one who spends hours fighting with infrastructure.

Chapter Roadmap

Click any topic to jump in

Explainer videoInteractive demo
1
Linux File System

FHS hierarchy, inodes, permission triplets, symlinks — the on-disk model every other Linux concept builds on.

Root HierarchyPermission TripletchmodchownSymbolic LinksDisk Space Management
Files are the substrate; commands act on them
2
Essential Commands

ls, find, grep, pipes, redirection — the core verbs of getting work done at the shell.

ls FlagsfindgrepPiping (|)RedirectionModern Alternatives
3
Process Management

PIDs, signals, nohup, tmux, nvidia-smi — how to launch, inspect, and recover long-running training jobs.

PIDnvidia-sminohuptmuxBackground JobsSignals
Commands launch processes

From files to running programs

4
Users & Permissions

Users, groups, sudo, umask, ownership — the security model that keeps multi-tenant GPU boxes sane.

Users & Home DirectoriesGroupssudoFile OwnershipSSH Key Managementumask
5
Shell Scripting

Shebangs, variables, conditionals, loops, error handling — gluing tools into reproducible pipelines.

Shebang LineVariablesConditionalsLoopsFunctionsError Handling
Processes run as users with permissions
6
Package Management

apt vs pip vs conda, virtual envs, requirements files — controlling exactly which versions actually run.

apt vs pipVirtual EnvironmentscondaRequirements FilespyenvMicromamba
7
Cron & Systemd

Scheduled jobs and managed services — how to make scripts run on a schedule and survive reboots.

Cron SyntaxcrontabSystemd Service FilesRestart PoliciesjournalctlTimer Units
Scripts orchestrate commands and processes
8
Environment Variables

export, .env, CUDA_VISIBLE_DEVICES, secrets management — the configuration channel between OS, shell, and apps.

export.env FilesCUDA Environment VariablesdirenvSecrets Managementenvsubst

Topics

8 topics in this chapter

You SSH into a new GPU server and need answers fast. Where is CUDA installed? Which disk has room for a 500 GB dataset? Why does ssh refuse the key you just copied over? Every one of those questions is answered by the same structure: the Linux file system, and the permissions attached to every file in it.

Linux has no drive letters. Everything, including extra disks, network shares, devices and even running processes, appears somewhere in a single tree that starts at /. The Filesystem Hierarchy Standard gives each top-level directory a job: /etc for configuration, /home for users, /var for logs and other changing data, /usr for installed software, /tmp for scratch files and /dev for devices such as GPUs. Learn that map once and you can find your way around any distribution.

Each file also carries an owner, a group and three sets of read, write and execute bits, which decide who can do what. This topic covers the hierarchy, the permission model and chmod and chown, symbolic links for keeping big datasets out of your project tree, and the tools for finding what is filling a disk.

Definition

The Linux file system is a single hierarchical namespace rooted at /, into which every storage device and pseudo-filesystem is mounted, with directory roles defined by the Filesystem Hierarchy Standard. Each file is an inode with an owner, a group and a permission mode of read, write and execute bits for owner, group and others.

In this topic

  1. 1Root Hierarchy
  2. 2Permission Triplet
  3. 3chmod
  4. 4chown
  5. 5Symbolic Links
  6. 6Disk Space Management
1 of 6
Root Hierarchy

The entire Linux filesystem is a single tree rooted at /. There are no drive letters like Windows. Every directory serves a specific purpose: /home stores user data and personal projects, /tmp provides temporary storage cleared on reboot, /var holds variable data like logs and caches, /etc contains system-wide configuration files, /usr houses installed programs and libraries, /opt stores optional software like CUDA, /proc exposes process information as virtual files, and /dev represents hardware devices including GPUs.

Knowing this layout means you can find things on any Linux machine. CUDA toolkit? Check /usr/local/cuda or /opt/cuda. System logs? /var/log. GPU device files? /dev/nvidia*. This consistency across all Linux distributions is one of the platform's strengths.

Mathematical Intuition

The Filesystem Hierarchy Standard (FHS) is a tree where every file lives at exactly one canonical path. Lookup is O(d)O(d) where dd is the depth — the kernel walks / → etc → nginx → nginx.conf doing one inode lookup per component. The dentry cache (dcache) hashes recently-resolved paths, dropping repeat lookups to O(1)O(1).

Example:

You SSH into a new GPU server for the first time. You need to verify CUDA is installed, check available GPU devices, find how much disk space is free, and locate your home directory. What commands do you run?

2 of 6
Permission Triplet

Every file in Linux has three sets of permissions — for the owner, the group, and others. Each set can include read (r=4), write (w=2), and execute (x=1). The numeric representation sums these values: 755 means owner has rwx (7), group has r-x (5), others have r-x (5).

Critical permission values for ML work: 600 for SSH keys (owner read-write only — SSH refuses keys with looser permissions), 755 for scripts you want to execute, 775 for shared project directories (group-writable), and 644 for regular files (owner read-write, everyone else read-only).

Mathematical Intuition

The rwx mode is encoded in 9 bits (3 sets of 3), giving 29=5122^9 = 512 possible permission combinations. With the setuid/setgid/sticky bits added, that's 12 bits, hence the 4-digit octal mode (e.g., 0755). Each octal digit packs r=4, w=2, x=1 — so chmod 755 mentally decomposes to rwxr-xr-x in O(1)O(1).

Example:

You get 'Permission denied' when running ssh -i ~/.ssh/gpu_key ubuntu@server even though the key file exists. ls -la ~/.ssh/gpu_key shows -rw-rw-r-- 1 alice alice. What's wrong?

3 of 6
chmod

The chmod command changes file permissions. It accepts either symbolic notation (chmod +x script.sh adds execute for all, chmod g+w file adds group write) or numeric notation (chmod 755 script.sh sets owner=rwx, group=rx, others=rx).

The -R flag applies permissions recursively to directories: chmod -R 775 /data/datasets/ sets all files and subdirectories to be group-writable. Be careful with recursive chmod on directories containing a mix of files and executables — you might accidentally make data files executable or remove execute permission from scripts.

Mathematical Intuition

Changing a mode is one metadata update per file, independent of file size, so a recursive chmod costs one system call per file in the tree. On a local disk that is fast, but on a network filesystem every change is a round trip to the server, so a dataset of millions of small files can take a long time. Avoid blanket recursion: chmod -R a+X adds execute only to directories, and find with -type f or -type d applies separate modes to files and directories.

4 of 6
chown

The chown command changes file ownership, setting both the user and group owner. The syntax is chown user:group file. This is critical when files created by root need to be accessed by service accounts, or when a new team member needs to take ownership of a project directory.

Common scenario: you install a Python package with sudo, and the resulting files are owned by root. Your training script can't write to those directories. Fix: sudo chown -R $USER:$USER ~/miniconda3/ transfers ownership back to you.

Mathematical Intuition

Ownership is two numbers stored in the inode, a user ID and a group ID, not names. Names come from /etc/passwd and /etc/group at display time. That is why files copied from another machine, or written by a container running as UID 1000, can show up owned by a stranger or a bare number: the IDs match a different user, or none, on this host. Matching UIDs across machines, or mapping them, is what makes shared storage behave.

5 of 6
Symbolic Links

Symbolic links (symlinks) are shortcuts that point to another file or directory. Created with ln -s target link_name, they appear as aliases in the filesystem. A common ML use case: keep large datasets on a separate high-capacity volume while maintaining a clean project structure with ln -s /mnt/data-volume/imagenet ~/project/data/imagenet.

Symlinks are also used extensively for managing CUDA versions (/usr/local/cuda is often a symlink to /usr/local/cuda-12.2) and for Python virtual environments. The -s flag creates a symbolic (soft) link; without it, ln creates a hard link, which has different semantics.

Mathematical Intuition

A symlink is a tiny inode (typically <60 bytes) holding a path string. Resolution adds 1 syscall per symlink in the path. The kernel caps symlink resolution at 40 hops (MAXSYMLINKS) to prevent infinite loops — exceed it and you get ELOOP. Hard links bypass this entirely: they're just additional dentries pointing to the same inode, O(1)O(1) at all access times.

6 of 6
Disk Space Management

GPU servers frequently run out of disk space from accumulated model checkpoints, training logs, and dataset caches. df -h shows filesystem-level usage in human-readable format. du -sh * shows the size of each item in the current directory. ncdu provides an interactive browser where you can navigate directories and see sizes in real-time — press 'd' to delete files directly.

A common cleanup workflow: find checkpoints older than a week (find ./checkpoints -name '*.pt' -mtime +7), identify the largest files (du -sh * | sort -rh | head), and remove all but the best N checkpoints. Automate this with a cron job to prevent disk-full emergencies during overnight training runs.

Mathematical Intuition

du walks every inode in O(n)O(n) time and returns block-level usage; df reads filesystem superblock metadata in O(1)O(1). They disagree when files are deleted but still held open by a process: the kernel keeps the inode, df reports the bytes as used, du can't see them. lsof | grep deleted finds the culprit. ML training logs are the canonical example.

Theory Exercise

Problem:

You join a team where three ML engineers share a GPU server. Each engineer has their own home directory, but they need shared access to datasets in /data/ and model checkpoints in /models/. Currently, Alice's training scripts can't read Bob's checkpoints, and Carol's datasets are only accessible with sudo. Design a permission scheme that allows all three engineers to read and write shared resources while keeping home directories private.

Hints:
  • Think about creating a shared group and using the setgid bit on shared directories
  • Consider what umask setting would be appropriate for the team
  • Remember that new files inherit the creating user's primary group unless setgid is set

Related Problems on PixelBank