# Linux Command Line

## Lecture <a href="#lecture" id="lecture"></a>

{% embed url="https://youtu.be/w7nQFk6bi_k" %}
Linux Command Line
{% endembed %}

## Command Line

The command line (aka **"shell"**) is a powerful interface to a computer. Suppose we type a command `cat flag`:

![cat flag](https://lh4.googleusercontent.com/MHy87YEeKja1ReA3wvxOP6UNkVtViwlS5ih4uyprbgxtKZduFEDYZP61Mc4qlDjQGByJ_nZjwFR45mCXyjyz_Wkt-TsrlGXQAoYpZQwUehQghHWt5S4OaqjlyxRPxdFmM1QrCCpwl_w)

Under the hood, Linux does the following things:

1. I told the shell to run the program `cat` with the argument `flag`.
2. The shell found the `cat` program file and launched it into a `cat` process with a `flag` argument.
3. `cat` is a program that outputs files. It reads the `flag` argument and knows to output the `flag` file, which contains "pwn\_college{1337}".

## Process

* A **process** is a running program.
* A **program** is a file on your computer.
* Files live in a **file system**.
* Your web browser, your command line interpreter ("shell"), your text editor, all start out as **files** on the file system and become **processes** when they are executed.

## File System

Unlike Windows (which traditionally has different file systems at different anchor points `C:\`, `D:\`, `E:\`, etc.), Linux presents a unified file system view:

| /          | The "anchor" of the filesystem. Pronounced "root".       |
| ---------- | -------------------------------------------------------- |
| /usr       | The Unix System Resource. Contains all the system files. |
| /usr/bin   | Executable files for programs installed on the computer. |
| /usr/lib   | Shared libraries for use by programs on the computer.    |
| /usr/share | Program resources (icons, art assets, etc).              |
| /etc       | System configuration.                                    |
| /var       | Logs, caches, etc.                                       |
| /home      | User-owned data.                                         |
| /home/ctf  | Data owned by you in the pwn.college infrastructure.     |
| /proc      | Runtime process data.                                    |
| /tmp       | Temporary data storage.                                  |

## Absolute Paths vs. Relative Paths

* **Absolute paths** start with `/`, such as `/usr`, `/home/yans/flags/TOPSECRET`, etc.
* **Relative paths** don't start with `/`, and are relative to the current working directory.

![Absolute Paths vs. Relative Paths](https://223316867-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MWVtlSxURaW2QQu6RU5%2Fuploads%2FDk8ka3x7HYlgJJ41TxNz%2Fimage.png?alt=media\&token=8f90f8f8-1b00-485b-ae84-9d7852df726c)

## **Environment Variables**

In the command `cat flag`, how does Linux know where `cat` is? The answer is the `$PATH` environmental variable.

**"Environment variables"** are a set of Key/Value pairs passed into every process when it is launched. Critical variables:

* **$PATH:** a list of directories to search for programs in.
* **$PWD:** the current working directory (same as the pwd command)
* **$HOME:** the path to your home directory
* **$HOSTNAME:** the name of your system

You can print environment variables with the `env` command, and set them with `export`:

![env and export](https://223316867-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MWVtlSxURaW2QQu6RU5%2Fuploads%2F7VpL2ahBZuMRTAs93XG0%2Fimage.png?alt=media\&token=47001622-3001-4235-b78c-c271fce8dc95)

If you're curious about what program file ends up becoming your cat process after it's found using the `$PATH` variable, use `which`.

## File Types

Examine `/home/yans/flags`:

![/home/yans/flags](https://223316867-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MWVtlSxURaW2QQu6RU5%2Fuploads%2FskSXPLjJZZTDRPUf6LDH%2Fimage.png?alt=media\&token=e4c73591-713d-4055-a469-4a5e16eefea3)

* `-`  is a **regular file**
* `d` is a **directory** (yes, directories are actually just special files!)
* `l` is a **symbolic link** (a file that transparently points to another file or directory)
* `p` is a **named pipe** (also known as a FIFO. You will get very familiar with these this module!)
* `c` is a **character device file** (i.e., backed by a hardware device that produces or receives data streams, such as a microphone)
* `b` is a **block device file** (i.e., backed by a hardware device that stores and loads blocks of data, such as a hard drive)
* `s` is a **unix socket** (essentially a local network connection encapsulated in a file)

## Symbolic Links vs. Hard Links

### Symbolic Links (Symlinks, Soft Links)

A **symbolic/soft link** is a special type of file that references another file. They are created `ln -s` (`-s` stands for symbolic):

![ln -s](https://223316867-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MWVtlSxURaW2QQu6RU5%2Fuploads%2FoVSWfwPpwRlkmc9m2SLn%2Fimage.png?alt=media\&token=6dea978e-35bc-4f48-a840-4e07240a6461)

You can also link directories:

![Link a directory](https://223316867-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MWVtlSxURaW2QQu6RU5%2Fuploads%2FoSHbPJPrtN8h42hYOLJN%2Fimage.png?alt=media\&token=814b926a-8f39-4de1-b6d6-65a459cb32a1)

**Beware:** Symbolic links to relative paths are relative to the directory containing the link!

![Symbolic link to a relative path](https://223316867-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MWVtlSxURaW2QQu6RU5%2Fuploads%2FuXUQjDJLpTYMO8PVdfgk%2Fimage.png?alt=media\&token=fccdcaeb-7832-4e29-aed2-0a5986fea09c)

![Symbolic link to an absolute path](https://223316867-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MWVtlSxURaW2QQu6RU5%2Fuploads%2Fb4H9xxiq3xWueA7w8m7N%2Fimage.png?alt=media\&token=80820015-c05e-4f1e-a023-1fdc58b3b215)

### Hard Links

A **hard link** (created with ln without the `-s` argument) is a direct reference to a file via its **inode**. You can also only hardlink files and not directories.

![Hard link](https://223316867-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MWVtlSxURaW2QQu6RU5%2Fuploads%2F0VPIY63tUOK6jxSFwxTf%2Fimage.png?alt=media\&token=e00de93d-20fd-41a3-858d-3f2ce56281f2)

The **inode** is a database that describes the file/directory attributes such as metadata and the physical location on the hard drive. They are essentially the numerical equivalent of a full address. With an inode, the OS can retrieve information about the file such as permission privileges and the physical location of the data on the hard drive to access the file. Should a file be moved from one folder to another, the file will be moved to a different location on the hard drive and its inode value will change with it automatically.

A hard link is an equally "valid" reference to the original file as the original file itself. It is a file that happens to be backed by the same data as the original.

## Pipes <a href="#pipes" id="pipes"></a>

**Pipes** facilitate a unidirectional flow of information. There are two types of pipes:

1. **Unnamed pipes**, ethereal channels of information between processes. Most commonly used to direct data from one command to another.
2. **Named pipes**, also known as **FIFOs**, created using the `mkfifo` command. Also used to help facilitate data flow in certain situations.

![](https://lh5.googleusercontent.com/0rJjHS99xMOTbsiSxZjY4iatXjauwtkIBCO2cpighJpML8EXKqiYxkoI8qN0caS-j5a7XrvTJWmyS2FCUmRcne-NpRId2_dIoLZU_y34oDGXnmlaAJpZ7xdo20l7dOM03kO2G7ltRF8)

## Input and Output Redirection <a href="#input-and-output-redirection" id="input-and-output-redirection"></a>

Command output can be redirected to files, and command input can be provided from files.

* `<in_file`
  * Redirect in\_file into the command's input
* `>out_file`
  * Redirect the command's output into out\_file, overwriting it
* `>>out_file`
  * Redirect the command's output into out\_file, appending to it
* `2>error_file`
  * Redirect the command's errors into error\_file, overwriting it
* `2>>error_file`
  * Redirect the command's errors into error\_file, appending to it

Input and Output Redirection

![Input and Output Redirection](https://lh5.googleusercontent.com/VfghwVfhFPySJCRH2m7hzNlqOSHIcMmdzfjdyERxeoLUcr5bBZA71lFrUwt4q0urZj28tUQngfUO40BBqa2E4pAGCqiUCdM89qIkRF-Wg_oITETauCSiEb7_Mey-bJypj0o2eayuv6c)
