General Info
Things that I find neat or wish I'd known earlier
pwd -Pto avoid symlinks from obscuring the true full path of the current working directory.diff -u file1 file2for unified diff - great for patchesheadshows 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
$PATHactually 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)<-8is from the sectionSystem commands and servers. I finally know what the numbers mean :) - Redirect standard output and error using
command > file 2>&1 - Useful
psflags:
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 -lshows mapping of signal numbers to names.jobscommand shows any suspended processes.nohupallows processes to live when the terminal gets killed.- Useful absolute permission modes:
| Mode | Used for | User | Group | Other |
|---|---|---|---|---|
| 644 | files | rw | r | r |
| 600 | files | rw | - | - |
| 755 | directories, programs | rwx | rx | rx |
| 700 | directories, programs | rwx | - | - |
| 711 | directories | rwx | x | x |
-
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
/vmlinuzor/boot/vmlinuz. The boot-loader loads this during system boot. However,/lib/modulescontain modules calledloadable kernel modulesthat 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 file2gives mefiles.tar - Compressing is reducing the size:
gzip file1gives mefile1.gz.gzipis for zipping, andgunzipis 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.gzfiles 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, wherezis forzcatwhich is pretty muchgunzipwith a couple flags.
Linux directory hierarchy
Here's a blogpost from linuxhandbook explaining it. Highlights:
/etcis pronounced EHT-see, like the store and not et cetera. π€¦/tmpis temporary files that get wiped, but/var/tmpisn't./optis for additional third-party software. Not widely used, but the general practice is to symlink the binary from/optto/bin./usr/local/includehas the header files used by the C compiler.
Devices
ls /devlists 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 bytesk= 1024 bytes Example:bs=1kis the same asbs=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/sdaand/dev/sdbare going to point to the same device. - A solid way to get the device name is to query the
udevdaemon (udevd) usingudevadm. - If you dont have access to
udevadmfor some reason, you could try to find the device in/sys, check the logs usingjournalctl -korcat /proc/devices. None of these are as easy as usingudevof 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/sdaand/dev/sdbbased 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
sddevices, uselsscsicommand. 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:
- How Linux Works - Brian Ward
- Julia Evans's free zines
- Linux Programming By Example - Kurt Ward