While I don’t think one should necessarily “learn” Linux (as opposed to figuring it out as you go along), I do think there are fundamental ideas that should be known.
* Yes, it’s not necessarily Linux, just a variety of concepts shared amongst most Unix-like systems.
The Terminal
The terminal allows you to interface with the shell—the thing that actually runs the commands. Otherwise, it just renders text and allows the user to issue commands.
Linux and its flavors tend to use bash, MacOS uses zsh, and Windows uses both cmd.exe
and powershell (yikes). You can find configuration files for these shells which are usually hidden and looks like .bashrc
and .zshrc
which allows for commands to run on boot and whatnot.
Variables
Set a local variable by simply doing var={value}
and retrieve it with $var
. Variables are stored in the memory allocated to that terminal, so when the terminal closes, they will be cleared.
Luckily, you can set global environment variables using export
. It can be read exactly as a local variable. However, once again, when the terminal closes these changes will be reset. You can mitigate this by modifying the config script to add the export
command. A common trick when adding to your path is to reference the old path so the old path isn’t overwritten, like so: export PATH="$PATH:/path/to/new"
.
Filesystems (Paths)
Absolute Path: Anything that starts with a slash. This /
is the root of the filesystem, in the case of a website it would be the domain root.
Relative Path: Generally speaking, paths not prefixed with /
will be assumed to be a relative path starting the current working directory. That means cd ./app
and cd app
will be interpreted the same.
Fun fact: Reference your home directory by using
~
, e.g.cd ~
.
So what’s with the trailing slash in some applications? It can change the behavior in some tools by marking it as a directory, particularly in web development where routing to /about/
will try to retrieve index.html
, but /about
will try to retrieve a file named about
. Or when loading relative assets in HTML, it affects the base path. Ultimately, it’s tool specific.
Permissions
There are 3 permissions (read, write, execute) that apply to 3 entities (owner, group, others).
A permission string looks something like this drwxrwxrwx
, or maybe -rwxr--r--
. The first character defines whether it is a directory or not. The next three groups apply to owner, group, others, respectively.
Owner: Usually the creator of the file/directory, but can be transferred using chown
.
Group: Seldom used, however the file/directory can be assigned a group, and members of that groups will respect these permissions.
Others: Everyone else.
Important to note that
others
does NOT include the Owner or Group.
Change permissions using chmod
, such as chmod 755 script.sh
. By default, it will accept a 3 digit number. Each digit represent the corresponding group. A digit is the decimal representation of a 3 bit string for rwx
. That means r
represents 4, w
represents 2, and x
represents 1. Doing read and write is as simple as adding them, so 6. That can be confusing though, especially for small changes so we can also make singular changes symbolically.
u+x
adds execute to user (owner)
g-w
removes write from group
o=rx
sets others (everyone) to read and execute
a+r
adds a read permission for everyone
Programs
You can run any compiled program by just typing the path into the shell. However, if you want to run an interpreted program you either need to use its relevant program e.g. python.exe
or include a shebang
which is a string at the beginning of a file telling the system to use the interpreter located at the specified path. For example #!/usr/bin/python3
.
Upon final execution, a program will return an exit code with 0
representing success and anything else, usually 1
, indicating an error. You may access the error code of the last program you ran with the $?
variable.
I/O
Streams
Unix-like systems use streams which are communication channels that connect commands and programs. There are three:
- Standard Input (stdin)
fd=0
- Standard Out (stdout)
fd=1
- Standard Error (stderr)
fd=2
There isn’t much use for this from a shell perspective, but may have use in systems programming like pipes/sockets.
Operators
>
is output redirection, placing the output in the specified file (or creating one if it doesn’t exist)
“
|
allows piping the output of one program into the input of another
Interrupts
Ctrl + C
to send SIGINT
(kill signal) to running program to handle. Sometimes a program can be in a bad state where it’s unable to handle the signal, so you may use the kill
command with the corresponding program PID. List processes in current shell with ps
, all of this with ps -A
.
Package Managers
Package managers are for installing, updating, or removing system wide software. They can manage dependencies. WSL (Ubuntu) uses APT as the primary packager manager.
There are also language specific package managers where the scope is closer to per-project/user/environment. More detail can be read in Package Managers.
Useful Commands
TODO Move this into a reference sheet perhaps.
Command | Purpose |
---|---|
history | Nicely formatted way to view previous commands (as opposed to arrow keys) |
pwd | Prints current working directory |
cat , head , tail | Prints contents of a file. Using head /tail with the -n 10 argument will print only the corresponding number of lines from head/tail. |
man | Quickly look up a comprehensive reference for the program in question. Type a / to begin a search, great for figuring out what a flag does. |
grep , wc , mkdir | Useful commands |
curl | Does more than fetch content, allows you to make a network request of any type and content. |