AlgoPlusAlgoPlus
Learn/Operating Systems
Lesson

Linux Commands

Drive the machine by typing small, composable commands into the shell — each a tiny program making system calls — all operating on one filesystem tree rooted at /.

8 min read Watch it move Build it

On Linux you drive the machine by typing small commands into the shell. The Unix philosophy is that each command is a tiny program that does *one thing well*, and you compose them to do bigger things. Every command is really just a program asking the kernel to do the work through system callsls ultimately calls into the kernel to read a directory.

The everyday commands

  1. 1pwdprint working directory: the folder you are currently "in". Commands act relative to it.
  2. 2lslist a directory's contents; ls -l shows the long form with permissions, owner, and size.
  3. 3cd pathchange directory; cd .. moves up one level toward the root.
  4. 4cat file — print a file's contents; mkdir name makes a new folder; rm file removes one.
  5. 5chmod — change a file's permissions (its read/write/execute settings).

One tree, rooted at /

Unlike Windows, Linux has no drive letters. Everything lives in a single tree starting at the root, written /. Extra disks and USB sticks are *mounted* into that one tree as folders. The standard layout is fixed by the FHS (Filesystem Hierarchy Standard), so the same folders mean the same things on every distribution.

$ pwd
/home/ada
$ ls -l
-rw-r--r--  1 ada  staff   42 Jun 30 09:00 notes.txt
drwxr-xr-x  3 ada  staff   96 Jun 30 09:01 projects
$ cd projects && ls
app.py  README.md
Reading the FHS
/bin and /usr/bin hold commands, /etc holds system config, /home holds users' folders, /var holds changing data like logs, and /tmp holds throwaway files. Knowing the map means you can find almost anything.

Reading permissions: rwx

In ls -l, the leftmost field like -rw-r--r-- is the permission string. The first character is the type (- file, d directory). The next nine are three groups of read/write/execute for the owner, the group, and others. So rw-r--r-- means the owner can read & write, while group and others can only read.

chmod 755 is not magic
Each rwx group is a 3-bit number: read=4, write=2, execute=1. So 755 means owner 4+2+1=7 (rwx), group 4+0+1=5 (r-x), others 5 (r-x). chmod 644 file gives rw-r--r--. The digits *are* the permission bits.
OperationTimeSpace
Navigation · where am I, what's here, movepwd, ls, cd
Files · read, create, removecat, mkdir, rm
Permissions · owner / group / otherschmod (rwx = 4/2/1)
Check yourself
What does the permission string rw-r--r-- mean?