Keyboard shortcuts

Press ← or β†’ to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

General Info

Things that I find neat or wish I'd known earlier

  • pwd -P to avoid symlinks from obscuring the true full path of the current working directory.
  • diff -u file1 file2 for unified diff - great for patches
  • head shows the first 10 lines of a file.
  • Shell variables are not passed to programs that the shell runs. However, Environment variables are. Example:
FOO=bar # shell variable
export FOO # makes it an environment variable
  • The order of adding directories to $PATH actually matters. The shell looks through the directories in order. This means that, adding your new directory to the beginning of the path makes the shell look in it first.
  • man -k <keyword> lets you search for a manpage by keyword.
  • manpages have sections. ping(8) <- 8 is from the section System commands and servers. I finally know what the numbers mean :)
  • Redirect standard output and error using command > file 2>&1
  • Useful ps flags:
ps u # more info
ps w # show full command, no truncation
ps x # all of my running processes
ps ax # all running processes, not just mine
  • kill -l shows mapping of signal numbers to names.
  • jobs command shows any suspended processes.
  • nohup allows processes to live when the terminal gets killed.
  • Useful absolute permission modes:
ModeUsed forUserGroupOther
644filesrwrr
600filesrw--
755directories, programsrwxrxrx
700directories, programsrwx--
711directoriesrwxxx
  • Note how the directories are being given execute permissions. This is because you can list the contents of the file, if it is readable, but you can only access a file in a directory, if the directory is executable.

  • The kernel is generally a binary file /vmlinuz or /boot/vmlinuz. The boot-loader loads this during system boot. However, /lib/modules contain modules called loadable kernel modules that the kernel loads and unloads on demand.

Disks and File Systems

Archiving vs Compressing

  • Archiving packs multiple files and directories into a single file: tar -cf files file1 file2 gives me files.tar
  • Compressing is reducing the size: gzip file1 gives me file1.gz. gzip is for zipping, and gunzip is for unzipping. It is not gun-zip, it is g-unzip. It all makes a lot of sense now. 🀦
  • These generally go hand in hand, which is how we end up with .tar.gz files or .tgz.
  • If you want to view the contents of a file before extracting them, tar -tvf <filename>.tar.
  • To decompress and unarchive a file we do: tar -xvfz <filename>.tar.gz, where z is for zcat which is pretty muchgunzip with a couple flags.

Linux directory hierarchy

Here's a blogpost from linuxhandbook explaining it. Highlights:

  • /etc is pronounced EHT-see, like the store and not et cetera. 🀦
  • /tmp is temporary files that get wiped, but /var/tmp isn't.
  • /opt is for additional third-party software. Not widely used, but the general practice is to symlink the binary from /opt to /bin.
  • /usr/local/include has the header files used by the C compiler.

Devices

  • ls /dev lists all devices. The first character represents the type of device:
    • b: block
    • c: character
    • s: socket
    • p: pipe

dd-command

The dd command can do a looooot of things, including copying files, wiping disks, creating backups. Here's a basic command to copy data from /dev/zero (an endless stream of zero bytes) to new_file:

$ dd if=/dev/zero of=new_file bs=1024 count=1
  • if=file β€” Input file (defaults to standard input).

  • of=file β€” Output file (defaults to standard output).

  • bs=size β€” Block size for both reading and writing. You can use suffixes:

    • b = 512 bytes
    • k = 1024 bytes Example: bs=1k is the same as bs=1024.
  • ibs=size, obs=size β€” Input and output block sizes when they differ. If they’re the same, just use bs.

  • count=num β€” Number of blocks to copy. This prevents endless copying when the input is very large or infinite (like /dev/zero).

  • skip=num β€” Skip the first num blocks of the input before copying.

Device names

  • Device names are kind of a hassle in linux. I think they get assigned device names based on the order in which they're encountered during boot, so there is no guarantee that /dev/sda and /dev/sdb are going to point to the same device.
  • A solid way to get the device name is to query the udev daemon (udevd) using udevadm.
  • If you dont have access to udevadm for some reason, you could try to find the device in /sys, check the logs using journalctl -k or cat /proc/devices. None of these are as easy as using udev of course.

Device types

There are a lot of types, so I'll note down the most useful ones:

Hard disks: /dev/sd*

  • The kernel makes separate files like /dev/sda and /dev/sdb based on the partitions.
  • The sd stands for SCSI disk, and SCSI stands for Small Computer System Interface. This is an old hardware and protocol for communication between devices. SCSI hardware is not used anymore, but the protocol was pretty good that it stuck around.
  • USBs and SATA devices are represented using sd.
  • To get a list of sd devices, use lsscsi command. Pretty sure it is supposed to mean: ls scsi

Non-Volatile Memory Devices: /dev/nvme*

  • Is used to talk to some SSDs.

Device Mapper: /dev/dm-*, /dev/mapper/*

LVM uses a device mapper to create block devices?

Creating Device Files

mknod /dev/sda1 b 8 1

b - block device 8 and 1 are the major and minor numbers respectively. The major number and minor number tell the kernel how to access the device. A common major number is assigned to all devices that are being controlled by the same device driver. The minor number helps to distinguish between the exact device type/controller using the same device driver.

Don't use mknod though, it's a lil unstable.

udev

The udev daemon, i.e., udevd is responsible for sending notifications to the user space when a new device is detected. For example, plugging in a USB device.

[There's a lot more to understand about udev but the Linux in a Nutshell book pretty much told me to skip ahead until I'm more familiar with linux, so yep.]

References:

  1. How Linux Works - Brian Ward
  2. Julia Evans's free zines
  3. Linux Programming By Example - Kurt Ward