We'll be using a few new *words* to reference the command line such as *shell*, *bash* and *terminal* through out the course.
They all pretty much mean the same thing but with some small, and not so important, differences between them.
Essentially a command line is a **textual interface** for humans to operate a computer.
What is very important to understand is that textual commands and graphical actions operate on the **same** computer.
For example, if you create a file via the command line, it will show up in you file explorer and vice versa.
The graphical and textual interfaces are just different *representations* of the same machine.
Now open up a terminal and you'll see the following.
```
waldek@hellodebian:~$
```
This is what we call a [prompt](https://en.wikipedia.org/wiki/Command-line_interface#Command_prompt).
It's not much but it's our *window* into the computer.
As with most things in life you can question it's *who, where, what and when*.
This information is actually **embedded** in the prompt.
Let's break it down.
*`waldek` is **who** I am on this computer
*`hellodebian` is the **what**, as in what computer I'm operating on
*`~` is **where** I am located on this computer
What about the *when* then?
Let's type in `date` and see what happens.
```
waldek@hellodebian:~$ date
Fri 18 Feb 2022 03:46:59 PM CET
waldek@hellodebian:~$
```
There we see our **when**!
But this miniscule operation illustrates us the **fundamental** operation of a command line!
1. we have a prompt where we can run a program
2. the program runs and outputs it's information on the terminal
3. once the program **finishes** we can run an other program
I'm deliberately saying program here but is `date`*really* a program?
It's a bit basic no?
Well, it is a program and most *commands* you'll type into your terminal are actually programs.
We can illustrate this as follows.
```
waldek@hellodebian:~$ vlc
VLC media player 3.0.16 Vetinari (revision 3.0.13-8-g41878ff4f2)
[0000559c46ee95b0] main libvlc: Running vlc with the default interface. Use 'cvlc' to use vlc without interface.
[0000559c46f89790] main playlist: playlist is empty
```
Vlc is now running and the terminal is *blocked* meaning we can't run other programs or commands in it.
If you try to run the `date` command again, or `ls` or `htop`, it won't work!
Try it out if you don't believe me.
But what happens if you **close** vlc?
The commands you typed get executed!
This is an illustration of the **sequential** nature of a command line.
Now, I don't think we have sufficiently *proven* that `date` is a full blown program so let's dig a bit deeper.
```
waldek@hellodebian:~$ which vlc
/usr/bin/vlc
waldek@hellodebian:~$ which date
/usr/bin/date
waldek@hellodebian:~$ which which
/usr/bin/which
```
There is quite bit to *unpack* in the example above.
First, what on earth is `which`?
Well, it's *also* a program and it's sole purpose in life is to tell you **where** a program is located on your system.
Because `which` by itself does not make a lot of sense it needs what we call an **argument**.
Here the argument is the name of the program we want to know it's location of.
The existence of arguments is the second big thing we discovered here.
The third new thing we can observe here is what we call **paths**, meaning locations on the system.
For example, `vlc` is a *binary* program located in a **folder** called `bin` which is located in a folder called `usr` which is at the *root* of your system.
If this sounds complicated, don't worry, we'll go into detail a bit later.
Now that we know **where** some of our programs are located, let's find out **what** they are.
The methodology is the same as with `which` but we'll use an other program called `file` who's purpose in life is to tell more about the **content** of a certain file.
Logically, `file` needs an argument and this argument is the **path** to the file you want to inspect.
```
waldek@hellodebian:~$ file /usr/bin/vlc
/usr/bin/vlc: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=51c40f8234213415771b3a344cab25a140543f8a, for GNU/Linux 3.2.0, stripped
waldek@hellodebian:~$ file /usr/bin/date
/usr/bin/date: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=b740f054aaef6a85aff024858c914c7eae70a6a5, for GNU/Linux 3.2.0, stripped
waldek@hellodebian:~$ file /usr/bin/which
/usr/bin/which: POSIX shell script, ASCII text executable
waldek@hellodebian:~$ file $(which which) # this is some command line kung fu...
/usr/bin/which: POSIX shell script, ASCII text executable
waldek@hellodebian:~$
```
Here we learn that both `vlc` and `date` are **executables**, compiled for an x86-64 system.
I would say they are both created *equally* no?
Both are actual programs.
But what about `which`?
It's also an executable but not compiled, it's a *POSIX shell script*.
So not all programs *are* created equally?
## Compiled vs interpreted
Executing a file, or program, means you take this file and tell the computer it needs to execute the actions that are stored in the file.
Compiled programs contain actual [instructions](https://en.wikipedia.org/wiki/Instruction_set_architecture#Instructions) the computer understands out of the box.
This means that programs that are compiled are always compiled for a specify architecture which in our case is x86-64.
On a Raspberry Pi this would be armhf or arm64.
Interpreted programs are not compiled to machine code but when we run them each line is passed to an appropriate **interpreter** and executed **line by line**.
The first line of a script is often the path to the interpreter that *understands* the code that will follow.
Popular interpreted languages are `bash`, `sh`, `python`, `php`, ...
We can take it *one* more step forward and peak into the *content* of the files.
A nice little program to do this is called `head` who's purpose in life is to show the first few lines of a file.
!"<22><><EFBFBD><EFBFBD>H<EFBFBD>H<EFBFBD><48>DESKTOP_STARTUP_ID--no-ignore-config--media-library--dbusvlcorg.VideoLAN.VLCVLC/3.0.16VLC media playerglobalhotkeys,noneVLC is not supposed to be run as root. Sorry.
If you need to use real-time priorities and/or privileged TCP ports
you can use %s-wrapper (make sure it is Set-UID root and
```
And now of `which`.
```
waldek@hellodebian:~$ head /usr/bin/which
#! /bin/sh
set -ef
if test -n "$KSH_VERSION"; then
puts() {
print -r -- "$*"
}
else
puts() {
printf '%s\n' "$*"
waldek@hellodebian:~$
```
Notice how the `vlc` file is mostly **not human readable** but we can still make out some keywords?
The `which` file however is perfectly readable!
The first line of the `which` file is `#! /bin/sh` which is the path to the interpreter.
The `#!` is called a [shebang](https://en.wikipedia.org/wiki/Shebang_(Unix)).
We can dig into this to learn more about this mysterious `sh`.
```
waldek@hellodebian:~$ file /bin/sh
/bin/sh: symbolic link to dash
waldek@hellodebian:~$ which dash
/usr/bin/dash
waldek@hellodebian:~$ file /usr/bin/dash
/usr/bin/dash: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=cb6911fd56559717336c938bef1ce479b0a85b35, for GNU/Linux 3.2.0, stripped
waldek@hellodebian:~$
```
Surprise surprise!
The *interpreter* is a compiled program!
When you think about it it does make sense because at the end of the day the computer *only* understands machine instructions.
So when you execute a script each line of code is converted to machine instructions on the spot.
This makes scripts easier to write but slower at execution.
The above is a *generalization* so take it with a grain of salt please!
## Note on file extensions
Linux does not rely as much on file extensions as windows does.
We could even say it often does not care about them.
Due to the decentralized nature of Linux development, some programs *do* care about them while others don't.
It's a bit of hit and miss but I advise you to read [this](https://askubuntu.com/questions/803434/do-file-extensions-have-any-purpose-in-linux) post on AskUbuntu for a more detailed explanation.
# More about paths
The prompt in our terminal tells us who we are, on which machine, and **where** we are located.
This where is symbolized with the `~` character.
But where is this where?
```
waldek@hellodebian:~$ pwd
/home/waldek
waldek@hellodebian:~$
```
The `pwd` program **p**rints our **w**orking **d**irectory, meaning where we are located on our system.
A user's home directory is symbolized with this tilde character.
We can move around our system with the `cd` command which is an abbreviation of **c**hange **d**irectory.
`cd` by itself seems to to *nothing* but this is not the case.
It's a *shortcut* to go to your user's home directory.
Let's try this out.
```
waldek@hellodebian:~$ cd
waldek@hellodebian:~$ cd /usr/bin
waldek@hellodebian:/usr/bin$ cd
waldek@hellodebian:~$ cd -
/usr/bin
waldek@hellodebian:/usr/bin$ cd
waldek@hellodebian:~$
```
The first line appears to do nothing because we are already in our home directory.
Next we move to the directory which contains the programs we discovered before and we see our prompt change.
It now shows *where* we are located!
The next line takes us back to our home and from there we can move *back to where we were before* with the `cd -` command.
This last command is also a handy *shortcut*!
Moving around is nice but once we arrive at our destination it would be practical to be able to list the files in that directory no?
There are multiple ways to go to the same location on your system.
You can always go either in an **absolute** way, or in *multiple***relative** ways.
The root of your system is `/` and to list what we can find there we can use `ls`.
```
waldek@hellodebian:~$ cd /
waldek@hellodebian:/$ ls
bin dev home initrd.img.old lib32 libx32 media opt root sbin sys usr vmlinuz
boot etc initrd.img lib lib64 lost+found mnt proc run srv tmp var vmlinuz.old
waldek@hellodebian:/$
```
From here we can go back home in multiple ways.
```
waldek@hellodebian:/$ cd
waldek@hellodebian:~$ cd -
/
waldek@hellodebian:/$ cd home/waldek/
waldek@hellodebian:~$ cd -
/
waldek@hellodebian:/$ cd /home/waldek/
waldek@hellodebian:~$
```
The first one is the handy shortcut we learned, and we go back to the root of our system with the `cd -` shortcut.
The following two manipulations look very similar but there is a subtle difference.
The first one `home/waldek` is a **relative** path, and the second one `/home/waldek` is an **absolute** one.
Relative paths depend on where you are located, absolute ones always point to the same location.
An example.
```
waldek@hellodebian:~$ cd /usr/bin/
waldek@hellodebian:/usr/bin$ cd /home/waldek/
waldek@hellodebian:~$ cd -
/usr/bin
waldek@hellodebian:/usr/bin$ cd home/waldek
-bash: cd: home/waldek: No such file or directory
waldek@hellodebian:/usr/bin$
```
The second command fails because from where I'm standing, there is no folder called `home/waldek`!
I can however still go to my home in a relative way but I need to *go back* a few directories first.
```
waldek@hellodebian:/usr/bin$ cd ../../home/waldek/
waldek@hellodebian:~$ pwd
/home/waldek
waldek@hellodebian:~$
```
The `..` means go **back** one directory so in our example we go back **two** directories, which brings us to the `/` of our system and from there we go **up** to `home` and then `waldek`.
## `root` and `/` and `/root` are not the same thing
The word *root* has multiple meanings and can be a bit confusing at the start.
Every Linux machine will have a `root` user **and** a `/` of the system.
The `root` user is an account essential to the proper working of a Linux system, whereas `/` is a location, the *root* of the filesystem.
The `/root` is also a location but is the **home** of the user `root`, located at the `/` of the filesystem.
If this sounds confusing read the above paragraph slowly until you see the differences.
I do want to note that the name of the user `root`*can* change but it's ID will always be `0`.
We'll go into detail about users and ID's later down the line.
# Exercise
Explore your system a bit using `cd` and `ls`.
You'll probably encounter some weird messages along the way.
Keep note of them so we can discuss it together.
# A pit stop to review what we've learned so far
Below is a list of programs and command we've seen so far.
We've seen that some take arguments, some don't.
This list will grow as we go so I advise you to take some notes of your own.
|command|description|
|---|---|
|systemctl|manipulate running services|
|apt|the Debian package manager|
|htop|a command line task manager we installed|
|vlc|a video player we installed|
|date|display the time of day|
|which|show the path to an executable|
|file|print more information about a file's content|
|head|show the first lines of a file|
|pwd|print working directory|
|cd|change directory|
|ls|list content of a directory|
# Arguments and flags
The `ls` program is one of the most used commands.
It's like a very basic *file explorer* in a graphical environment and just like it's graphical counter part you can use it to display it's data in different ways.
Think of the *details* versus *thumbnail* views in your favorite explorer.
But how do you do this?
We can **add flags or options** to the program to modify it's behavior.
Have a look at the output below.
```
waldek@hellodebian:~$ ls
Desktop Documents Downloads Music Pictures Public Templates Videos
waldek@hellodebian:~$ ls -l
total 32
drwxr-xr-x 2 waldek waldek 4096 Feb 18 12:50 Desktop
drwxr-xr-x 2 waldek waldek 4096 Feb 18 12:50 Documents
drwxr-xr-x 2 waldek waldek 4096 Feb 18 12:50 Downloads
drwxr-xr-x 2 waldek waldek 4096 Feb 18 12:50 Music
drwxr-xr-x 2 waldek waldek 4096 Feb 18 12:50 Pictures
drwxr-xr-x 2 waldek waldek 4096 Feb 18 12:50 Public
drwxr-xr-x 2 waldek waldek 4096 Feb 18 12:50 Templates
drwxr-xr-x 2 waldek waldek 4096 Feb 18 12:50 Videos
The `-l` and `-a` additions are what we call *flags* or *options*.
Aren't they just extra *arguments*?
There is no real rule to the naming of this concept but [this](https://unix.stackexchange.com/questions/285575/whats-the-difference-between-a-flag-an-option-and-an-argument) is a rather interesting take.
We can combine arguments if we want, and some programs even allow *concatenation* of arguments.
Have a look below to understand what I mean by that.
```
waldek@hellodebian:~$ ls -a -l
total 112
drwxr-xr-x 16 waldek waldek 4096 Feb 21 18:12 .
drwxr-xr-x 3 root root 4096 Feb 18 12:49 ..
-rw------- 1 waldek waldek 2772 Feb 22 10:45 .bash_history
-rw-r--r-- 1 waldek waldek 220 Feb 18 12:49 .bash_logout
-rw-r--r-- 1 waldek waldek 3526 Feb 18 12:49 .bashrc
drwx------ 16 waldek waldek 4096 Feb 18 14:24 .cache
drwx------ 20 waldek waldek 4096 Feb 18 14:23 .config
drwx------ 3 waldek waldek 4096 Feb 18 12:52 .dbus
drwxr-xr-x 2 waldek waldek 4096 Feb 18 12:50 Desktop
-rw-r--r-- 1 waldek waldek 35 Feb 18 13:33 .dmrc
drwxr-xr-x 2 waldek waldek 4096 Feb 18 12:50 Documents
drwxr-xr-x 2 waldek waldek 4096 Feb 18 12:50 Downloads
drwx------ 2 waldek waldek 4096 Feb 18 15:07 .gnupg
drwxr-xr-x 3 waldek waldek 4096 Feb 18 12:50 .local
drwxr-xr-x 2 waldek waldek 4096 Feb 18 12:50 Music
drwxr-xr-x 2 waldek waldek 4096 Feb 18 12:50 Pictures
-rw-r--r-- 1 waldek waldek 807 Feb 18 12:49 .profile
drwxr-xr-x 2 waldek waldek 4096 Feb 18 12:50 Public
-rw------- 1 waldek waldek 11 Feb 21 18:12 .python_history
drwx------ 2 waldek waldek 4096 Feb 21 12:45 .ssh
drwxr-xr-x 2 waldek waldek 4096 Feb 18 12:50 Templates
-rw-r----- 1 waldek waldek 5 Feb 18 15:06 .vboxclient-clipboard.pid
-rw-r----- 1 waldek waldek 5 Feb 18 15:06 .vboxclient-display-svga-x11.pid
-rw-r----- 1 waldek waldek 5 Feb 18 15:06 .vboxclient-draganddrop.pid
-rw-r----- 1 waldek waldek 5 Feb 18 15:06 .vboxclient-seamless.pid
drwxr-xr-x 2 waldek waldek 4096 Feb 18 12:50 Videos
-rw------- 1 waldek waldek 56 Feb 18 13:33 .Xauthority
-rw------- 1 waldek waldek 2701 Feb 18 13:33 .xsession-errors
waldek@hellodebian:~$ ls -la
total 112
drwxr-xr-x 16 waldek waldek 4096 Feb 21 18:12 .
drwxr-xr-x 3 root root 4096 Feb 18 12:49 ..
-rw------- 1 waldek waldek 2772 Feb 22 10:45 .bash_history
-rw-r--r-- 1 waldek waldek 220 Feb 18 12:49 .bash_logout
-rw-r--r-- 1 waldek waldek 3526 Feb 18 12:49 .bashrc
drwx------ 16 waldek waldek 4096 Feb 18 14:24 .cache
drwx------ 20 waldek waldek 4096 Feb 18 14:23 .config
drwx------ 3 waldek waldek 4096 Feb 18 12:52 .dbus
drwxr-xr-x 2 waldek waldek 4096 Feb 18 12:50 Desktop
-rw-r--r-- 1 waldek waldek 35 Feb 18 13:33 .dmrc
drwxr-xr-x 2 waldek waldek 4096 Feb 18 12:50 Documents
drwxr-xr-x 2 waldek waldek 4096 Feb 18 12:50 Downloads
drwx------ 2 waldek waldek 4096 Feb 18 15:07 .gnupg
drwxr-xr-x 3 waldek waldek 4096 Feb 18 12:50 .local
drwxr-xr-x 2 waldek waldek 4096 Feb 18 12:50 Music
drwxr-xr-x 2 waldek waldek 4096 Feb 18 12:50 Pictures
-rw-r--r-- 1 waldek waldek 807 Feb 18 12:49 .profile
drwxr-xr-x 2 waldek waldek 4096 Feb 18 12:50 Public
-rw------- 1 waldek waldek 11 Feb 21 18:12 .python_history
drwx------ 2 waldek waldek 4096 Feb 21 12:45 .ssh
drwxr-xr-x 2 waldek waldek 4096 Feb 18 12:50 Templates
-rw-r----- 1 waldek waldek 5 Feb 18 15:06 .vboxclient-clipboard.pid
-rw-r----- 1 waldek waldek 5 Feb 18 15:06 .vboxclient-display-svga-x11.pid
-rw-r----- 1 waldek waldek 5 Feb 18 15:06 .vboxclient-draganddrop.pid
-rw-r----- 1 waldek waldek 5 Feb 18 15:06 .vboxclient-seamless.pid
drwxr-xr-x 2 waldek waldek 4096 Feb 18 12:50 Videos
-rw------- 1 waldek waldek 56 Feb 18 13:33 .Xauthority
-rw------- 1 waldek waldek 2701 Feb 18 13:33 .xsession-errors
waldek@hellodebian:~$
```
# Getting help
## Options
OK, so we have *a lot* of programs we can use on the command line, plus we can change how they work by adding obscure characters.
How on earth can we *discover* what a program can and can't do?
Introducing the **most important flag**: `--help`.
Most programs come with builtin help to explain you how to use the program.
Often, but not always, this help can be displayed on the terminal by adding the `--help` flag.
Let's investigate this.
```
waldek@hellodebian:~$ ls --help
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.
Mandatory arguments to long options are mandatory for short options too.
-a, --all do not ignore entries starting with .
-A, --almost-all do not list implied . and ..
--author with -l, print the author of each file
-b, --escape print C-style escapes for nongraphic characters
--block-size=SIZE with -l, scale sizes by SIZE when printing them;
e.g., '--block-size=M'; see SIZE format below
-B, --ignore-backups do not list implied entries ending with ~
-c with -lt: sort by, and show, ctime (time of last
modification of file status information);
with -l: show ctime and sort by name;
otherwise: sort by ctime, newest first
-C list entries by columns
--color[=WHEN] colorize the output; WHEN can be 'always' (default
if omitted), 'auto', or 'never'; more info below
-d, --directory list directories themselves, not their contents
-D, --dired generate output designed for Emacs' dired mode
-f do not sort, enable -aU, disable -ls --color
-F, --classify append indicator (one of */=>@|) to entries
--file-type likewise, except do not append '*'
--format=WORD across -x, commas -m, horizontal -x, long -l,
single-column -1, verbose -l, vertical -C
--full-time like -l --time-style=full-iso
-g like -l, but do not list owner
--group-directories-first
group directories before files;
can be augmented with a --sort option, but any
use of --sort=none (-U) disables grouping
-G, --no-group in a long listing, don't print group names
-h, --human-readable with -l and -s, print sizes like 1K 234M 2G etc.
--si likewise, but use powers of 1000 not 1024
-H, --dereference-command-line
follow symbolic links listed on the command line
--dereference-command-line-symlink-to-dir
follow each command line symbolic link
that points to a directory
--hide=PATTERN do not list implied entries matching shell PATTERN
(overridden by -a or -A)
--hyperlink[=WHEN] hyperlink file names; WHEN can be 'always'
(default if omitted), 'auto', or 'never'
--indicator-style=WORD append indicator with style WORD to entry names:
none (default), slash (-p),
file-type (--file-type), classify (-F)
-i, --inode print the index number of each file
-I, --ignore=PATTERN do not list implied entries matching shell PATTERN
-k, --kibibytes default to 1024-byte blocks for disk usage;
used only with -s and per directory totals
-l use a long listing format
-L, --dereference when showing file information for a symbolic
link, show information for the file the link
references rather than for the link itself
-m fill width with a comma separated list of entries
-n, --numeric-uid-gid like -l, but list numeric user and group IDs
-N, --literal print entry names without quoting
-o like -l, but do not list group information
-p, --indicator-style=slash
append / indicator to directories
-q, --hide-control-chars print ? instead of nongraphic characters
--show-control-chars show nongraphic characters as-is (the default,
unless program is 'ls' and output is a terminal)
-Q, --quote-name enclose entry names in double quotes
--quoting-style=WORD use quoting style WORD for entry names:
literal, locale, shell, shell-always,
shell-escape, shell-escape-always, c, escape
(overrides QUOTING_STYLE environment variable)
-r, --reverse reverse order while sorting
-R, --recursive list subdirectories recursively
-s, --size print the allocated size of each file, in blocks
-S sort by file size, largest first
--sort=WORD sort by WORD instead of name: none (-U), size (-S),
time (-t), version (-v), extension (-X)
--time=WORD change the default of using modification times;
access time (-u): atime, access, use;
change time (-c): ctime, status;
birth time: birth, creation;
with -l, WORD determines which time to show;
with --sort=time, sort by WORD (newest first)
--time-style=TIME_STYLE time/date format with -l; see TIME_STYLE below
-t sort by time, newest first; see --time
-T, --tabsize=COLS assume tab stops at each COLS instead of 8
-u with -lt: sort by, and show, access time;
with -l: show access time and sort by name;
otherwise: sort by access time, newest first
-U do not sort; list entries in directory order
-v natural sort of (version) numbers within text
-w, --width=COLS set output width to COLS. 0 means no limit
-x list entries by lines instead of by columns
-X sort alphabetically by entry extension
-Z, --context print any security context of each file
-1 list one file per line. Avoid '\n' with -q or -b
--help display this help and exit
--version output version information and exit
The SIZE argument is an integer and optional unit (example: 10K is 10*1024).
Units are K,M,G,T,P,E,Z,Y (powers of 1024) or KB,MB,... (powers of 1000).
Binary prefixes can be used, too: KiB=K, MiB=M, and so on.
The TIME_STYLE argument can be full-iso, long-iso, iso, locale, or +FORMAT.
FORMAT is interpreted like in date(1). If FORMAT is FORMAT1<newline>FORMAT2,
then FORMAT1 applies to non-recent files and FORMAT2 to recent files.
TIME_STYLE prefixed with 'posix-' takes effect only outside the POSIX locale.
Also the TIME_STYLE environment variable sets the default style to use.
Using color to distinguish file types is disabled both by default and
with --color=never. With --color=auto, ls emits color codes only when
standard output is connected to a terminal. The LS_COLORS environment
variable can change the settings. Use the dircolors command to set it.
Exit status:
0 if OK,
1 if minor problems (e.g., cannot access subdirectory),
2 if serious trouble (e.g., cannot access command-line argument).
GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Full documentation <https://www.gnu.org/software/coreutils/ls>
or available locally via: info '(coreutils) ls invocation'
waldek@hellodebian:~$
```
The output above teaches us that the options `-la` change the output so it becomes:
*`-a, --all do not ignore entries starting with .`
*`-l use a long listing format`
To *not ignore entries starting with .* we can use two different flags, `-a` or `--all`.
The former is called short arguments and the latter long ones.
The long ones are more *verbose* so it's often easier to understand what a line *does* just by reading it.
Note that `ls` only has one way of showing the help by adding `--help` whereas the help for `htop` can be shown with both `-h` or `--help`.
This inconsistency is sadly a byproduct of the decentralized nature of Linux.
To add to this mess some programs use *single dash long form* (`-help`) but those programs are rather rare.
## Manuals
The `--help` flag is tremendously useful as it gives you an overview of how you can modify a programs behaviour, kind of like settings, but it doesn't always explain you what a program is *for*.
The is where the manuals come into play.
I see the `--help` flag as a *cheat sheet* to quickly discover options and the man pages as a reference.
We can read the manual by calling the `man` program with an argument.
This argument is the *name* of the program you want to read the manual for.
Same idea as with `which`.
```
waldek@hellodebian:~$ man ls
```
You're now in a program called `less`, which is a [pager](https://en.wikipedia.org/wiki/Terminal_pager).
You can scroll the manual with the arrows of `vi` based navigation (which we'll get into later).
If you hit `h` you'll see the builtin documentation of `less` and press `q` to quit (twice if you're in the help section).
This brings you back to your prompt.
It's worth reading the [wikipedia entry](https://en.wikipedia.org/wiki/Man_page) of the `man` command to understand it's history and operation.
The output below illustrates the *tree* of a terminal that's reading a manual.
In a `bash` shell you invoke `man ls` which in turn runs the `pager` program to display the content.
If you're intrigued by this `pstree` program go and read the manual!
```
waldek@hellodebian:~$ pstree 10845 -a
bash
└─man ls
└─pager
waldek@hellodebian:~$
```
Not every command has a manual.
For example, `cd` does not have one.
This is kind of logical because `cd` is a builtin.
To learn more about builtin commands you can use the `help` program.
On it's own it gives you all the builtin command you can invoke and with an argument it outputs the help about that specific command.
```
waldek@hellodebian:~$ help
GNU bash, version 5.1.4(1)-release (x86_64-pc-linux-gnu)
These shell commands are defined internally. Type `help' to see this list.
Type `help name' to find out more about the function `name'.
Use `info bash' to find out more about the shell in general.
Use `man -k' or `info' to find out more about commands not in this list.
A star (*) next to a name means that the command is disabled.
job_spec [&] history [-c] [-d offset] [n] or history -anrw [filename] or history -ps>
(( expression )) if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]... [ else>
. filename [arguments] jobs [-lnprs] [jobspec ...] or jobs -x command [args]
: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [>
The *easiest* to use command line editor that comes installed with most Linux distributions is called `nano`.
Below is a screenshot of an empty text file, opened up in `nano`.
![nano new file](../assets/nano_01.png)
In this window we can write anything we want.
I added two lines of text, but how do I *save and quit* this program?
At the bottom of the screen there is a *menu*.
`^X` is the *button* to exit `nano` but how do we invoke it?
Well, by pressing **CRTL-x**.
By logical consequence to search the text file we can use **CTRL-w**.
When you try to exit, dialog will pop up to ask you to save.
If you answer **Y**, you'll be prompted for a filename.
Here you can just hit **ENTER**.
![nano quit](../assets/nano_02.png)
![nano save](../assets/nano_03.png)
# Exercise
Create some text files with `nano`.
Move them around with `mv`, delete them with `rm` and copy them with `cp`.
Can you edit the `/etc/passwd` file?
### `vi` and `vim`
On of the most [popular](https://www.journaldev.com/41292/top-best-text-editors-linux) text editors is by far `vim`.
It is an *improvement* on an older editor called `vi`, vim(proved).
Personally it is my favorite editor, or even program, on Linux and I can highly advise you to become somewhat comfortable with it.
The learning curve is quite *steep* but so very much worth it!
The reason *why* it is a bit complicated at first is because `vim` is a [modal](https://www.wikidata.org/wiki/Q80592893) text editor.
The paragraph below is taken from the [wikipedia](https://en.wikipedia.org/wiki/Vi) page of `vi`.
> vi is a modal editor: it operates in either insert mode (where typed text becomes part of the document) or command mode (where keystrokes are interpreted as commands that control the edit session). For example, typing i while in command mode switches the editor to insert mode, but typing i again at this point places an "i" character in the document. From insert mode, pressing ESC switches the editor back to command mode. A perceived advantage of vi's separation of text entry and command modes is that both text editing and command operations can be performed without requiring the removal of the user's hands from the home row. As non-modal editors usually have to reserve all keys with letters and symbols for the printing of characters, any special commands for actions other than adding text to the buffer must be assigned to keys that do not produce characters, such as function keys, or combinations of modifier keys such as Ctrl, and Alt with regular keys. Vi has the property that most ordinary keys are connected to some kind of command for positioning, altering text, searching and so forth, either singly or in key combinations. Many commands can be touch typed without the use of Ctrl or Alt. Other types of editors generally require the user to move their hands from the home row when touch typing:
# Exercise
The only way to learn `vim` is to *use* it.
Luckily `vim-nox` comes with a dedicated *program* to learn it called `vimtutor`.
It's actually a text file that you open up in `vim` where you just need to **read** and **execute** what you've read.
Enjoy!
Once you have completed the `vimtutor` I urge you to have a look at one of the *many* cheat sheets [online](https://vim.rtorr.com/).
# Opening files
We can use `vim` or `nano` to open text files and write to them, but there are a few programs worth knowing that can *just* show the content of a text file.
You'll use these all the time!
Before we've seen that `head` shows the first few lines of a file.
To see the *last lines* of a file we use `tail`.
Get it, *head and tail*?
The linux world is full of little inside jokes like this.
Anyway, the two most used programs to **read** file content are `cat` and `less`.
`cat` prints the content of a file straight to the terminal, where as `less` is what we call a [pager](https://en.wikipedia.org/wiki/Terminal_pager).
If you need to search or read a long file you'll find that a pager is more practical.
Reading the `man less` page we see the following.
```
LESS(1) General Commands Manual LESS(1)
NAME
less - opposite of more
```
Again, the joke here is that `less` can actually do *more* than `more`.
# Exercise
Open up some files with `cat` and `less`.
If you need some inspiration, try a few files in the `/etc` directory.
# Finding stuff
## Finding files
### The easy way
On most distributions `locate` will not be installed by default but I advise you to install it, especially in the beginning of your linux career.
The syntax is super simple and it fetches results from a database instead of the hard drive so it's quite quick and doesn't have permission problems.
To install and use it, you should proceed as follows.
```
waldek@debian:~$ sudo apt install locate
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
locate is already the newest version (4.8.0-1).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
waldek@debian:~$ locate bin/zip
/usr/bin/zipdetails
waldek@debian:~$ locate zip | grep bin # this is a weird thing...
/usr/bin/bunzip2
/usr/bin/bzip2
/usr/bin/bzip2recover
/usr/bin/gunzip
/usr/bin/gzip
/usr/bin/streamzip
/usr/bin/zipdetails
/usr/lib/klibc/bin/gunzip
/usr/lib/klibc/bin/gzip
waldek@debian:~$
```
You'll probably be a bit confused by the `|` part of the commands above.
Don't worry, give it a few hours paragraphs and it will become fully transparent!
Next, there is a very high probability that the commands above don't immediately work for you.
This is because `locate` functions with a database which needs to be updated from time to time.
This is handled automatically by a [cron](https://en.wikipedia.org/wiki/Cron) job.
We'll go into detail on how to schedule jobs ourselves but for now just remember you can trigger a refresh of the database yourself with the following command.
```
waldek@debian:~$ locate nano.html
waldek@debian:~$ sudo updatedb
waldek@debian:~$ locate nano.html
/usr/share/doc/nano/nano.html
waldek@debian:~$
```
### The hard way
A program you'll find on most Linux distributions to search for files is `find`.
It performs a real time search a crossed the entire file system and prints out the path of the file found.
A simple example can be seen below.
```
waldek@debian:~$ find /usr -iname "nano.html"
/usr/share/doc/nano/nano.html
waldek@debian:~$
```
The real power lies in it's various command line arguments.
We can for example search for all files which are bigger than 100MB, owned by a specific user or of a certain permission.
All of these conditions can be combined to improve the specificity of your query.
There are a few exercises below so I won't give too many spoiler.
My best advise is to read `man find` and experiment.
`locate` and `find` are used to search **for** files.
To search for content **inside** files we can use an other very powerful tool called `grep`.
It is often combined with [regular expressions](https://www.gnu.org/software/grep/manual/html_node/Regular-Expressions.html), or regex, for search for patterns.
The syntax of regex's is a bit challenging but, like vim, it is very much worth the effort.
Even more, you can use regex's *inside* vim to search and replace text!
This being said, you can use `grep` without regular expressions as well and this is what we'll do first.
Most distributions will come with some dictionaries installed.
We can find the American one with the `locate` command.
The *third* one in line is the one we need, but have a look at the other files if you're intrigued.
We can search for multiple patterns with the `|` character **but** this character has a special meaning in the shell so we need to escape it with a `\`**or** we can use *extended*`grep` with either `egrep` or `grep -E`.
The exact *meaning* of `|` will come clear in a bit, but in this context it means **or**.
Both are quite related and for simplicities sake you can view regular expressions as wildcards on steroids.
A more detailed explaination can be found [here](https://unix.stackexchange.com/questions/57957/how-do-regular-expressions-differ-from-wildcards-used-to-filter-files).
The syntax for wildcards is rater simple and also goes by the name of [globbing](https://en.wikipedia.org/wiki/Globbing) of [filename expansion](https://www.gnu.org/software/bash/manual/html_node/Filename-Expansion.html).
The most important rules to remember are the following.
| character | meaning |
| --- | --- |
| ? | one character, one times |
| * | any character, any number of times |
| [a-z] | matches all lowercase letters |
| [0-9] | matches all numbers |
| [ac9] | matches a *or* c *or* 9 |
# Exercise
You can download and extract [this](../assets/a_lot_of_empty_files_1.tar.gz) to try and match some patterns with wildcards.