class: center, middle, inverse # Hands on working with **bash** --- class: middle ## About shells - `shell` = command line interface to computer. - `bash` = one of the most used shells - other shells: `zsh`, `ksh`, `sh`, ... --- class: middle ## First steps First simple command: ```bash $ pwd /home/schmittu ``` The `$` is called **prompt** and can look different, e.g ```bash [schmittu@slab1vrt ~]$ ``` Here the prompt contains the user before the `@`, followed by the "hostname" and `~` indicates the home folder. The output after `pwd` is the *current working directory* which on Linux systems usually is `/home/
`. --- class: middle ## Paths Text like `/home/schmittu` describing the location of a file or folder is called a *path*. - The first `/` relates to the root of the file system and then folders separated by `/` the last entry is a file or folder. - Such a path starting with `/` is also called an *absolute path*. - A path not starting with `/` is called an *relative path* which is relative the current working directory - `.` and `..` are also paths, the first is a shortcut for the current working directory, the `..` refers to the parent of the current working directory. --- class: middle ## Changing the current working directory `cd` = "change directory" ```bash $ cd /tmp $ pwd /tmp ``` ```bash $ cd . $ pwd /tmp ``` ```bash $ cd .. $ pwd / ``` A simple `cd` changes to the home folder ```bash $ cd $ pwd /home/schmittu ``` --- class: middle ## Listing files ls = "list files" ```bash $ cd workshop $ ls folder_01 folder_02 ``` `-l` gives long output ```bash $ ls -l total 0 drwx------ 2 schmittu domainusers 0 Jun 4 21:06 folder_01 drwx------ 2 schmittu domainusers 0 Jun 4 21:06 folder_02 ``` ```bash $ ls -l folder_01 total 16 -rwx------ 1 schmittu domainusers 6 Jun 4 21:06 hi.txt -rwx------ 1 schmittu domainusers 6 Jun 4 21:06 ho.txt ``` --- class: middle ## Listing files (continued) ```bash drwx------ 2 schmittu domainusers 0 Jun 4 21:06 folder_01 ``` 1. The `d` in the first column refers to "directory", for file you see a `-`. 2. The following 3 groups of 3 characters are file or folder permissions. More about this on the next slide. 3. The `2` is a so called link count. Then the owner of the file and the group of the file are named. The following `0` refers to the size and the timestamp describes the latest change. --- class: middle: ## About file permissions ```bash -rw-r--r-- 2 schmittu domainusers 0 Jun 8 21:04 instructions.txt drwxrx---- 2 schmittu domainusers 0 Jun 4 21:06 folder_01 ^^^^^^^^^ FILE PERMISSIONS ``` This is how to interpret file permissions by positions: - The first three characters refer to the owner of the file (here `schmittu`), - The next three characters refer to the group the user belongs to (here `domainusers`), - The last three characters refer to all other users. And this are the meanings of the characters: - `r`: allowed to read. - `w`: allowed to write. - `x`: allowed to run as a programm / allowed to `cd` if it is a folder. - '-': not allowed. --- class: middle: ## About file permissions, continued. ```bash -rw-r--r-- 2 schmittu domainusers 0 Jun 8 21:04 instructions.txt drwxrx---- 2 schmittu domainusers 0 Jun 4 21:06 folder_01 ^^^^^^^^^ FILE PERMISSIONS ``` So in the example above: - `instructions.txt` is a file which is readable by all users on the system and can be written by the owner. - `folder_01` is a directory. The owner and group members are allowed to `cd` to the folder and to list the content. --- class: middle ## globbing The character `*` refers to an arbitrary string (text) of length 0 or greater. ```bash ls fol*1 hi.txt ho.txt ``` `?` represents one single arbitrary character: ```bash ls -l ?older_?2 total 8 -rwx------ 1 schmittu domainusers 40 Jun 4 21:06 today.txt ``` Both `*` and / or `?` can occur multiple times ```bash ls -l */*.txt -rwx------ 1 schmittu domainusers 6 Jun 4 21:06 folder_01/hi.txt -rwx------ 1 schmittu domainusers 6 Jun 4 21:06 folder_01/ho.txt -rwx------ 1 schmittu domainusers 40 Jun 4 21:06 folder_02/today.txt ``` --- class: middle ## Moving / renaming The `mv` command allows to move and / or rename files and folders ```bash $ mv folder_01/hi.txt . $ ls -l total 8 drwx------ 2 schmittu domainusers 0 Jun 4 21:18 folder_01 drwx------ 2 schmittu domainusers 0 Jun 4 21:06 folder_02 -rwx------ 1 schmittu domainusers 6 Jun 4 21:06 hi.txt ``` ```bash $ mv hi.txt greet.txt $ ls -l total 8 drwx------ 2 schmittu domainusers 0 Jun 4 21:18 folder_01 drwx------ 2 schmittu domainusers 0 Jun 4 21:06 folder_02 -rwx------ 1 schmittu domainusers 6 Jun 4 21:06 greet.txt ``` --- class: middle ## Copying / removing ```bash $ cp greet.txt backup.txt $ ls -l total 8 drwx------ 2 schmittu domainusers 0 Jun 4 21:18 folder_01 drwx------ 2 schmittu domainusers 0 Jun 4 21:06 folder_02 -rwx------ 1 schmittu domainusers 6 Jun 4 21:06 backup.txt -rwx------ 1 schmittu domainusers 6 Jun 4 21:06 greet.txt ``` ```bash $ rm greet.txt $ ls -l total 8 drwx------ 2 schmittu domainusers 0 Jun 4 21:18 folder_01 drwx------ 2 schmittu domainusers 0 Jun 4 21:06 folder_02 -rwx------ 1 schmittu domainusers 6 Jun 4 21:06 backup.txt ``` ```bash $ mv backup.txt greet.txt $ ls -l total 8 drwx------ 2 schmittu domainusers 0 Jun 4 21:18 folder_01 drwx------ 2 schmittu domainusers 0 Jun 4 21:06 folder_02 -rwx------ 1 schmittu domainusers 6 Jun 4 21:06 greet.txt ``` --- class: middle ## Redirecting output The `>` can be used to redirect output to a file: ```bash $ ls -l * > out.txt $ ls -l total 16 drwx------ 2 schmittu domainusers 0 Jun 4 21:18 folder_01 drwx------ 2 schmittu domainusers 0 Jun 4 21:06 folder_02 -rwx------ 1 schmittu domainusers 6 Jun 4 21:22 greet.txt -rwx------ 1 schmittu domainusers 215 Jun 4 21:23 out.txt ``` --- class: middle ## Showing content of a file ```bash $ cat out.txt -rwx------ 1 schmittu domainusers 6 Jun 4 21:22 greet.txt folder_01: total 8 -rwx------ 1 schmittu domainusers 6 Jun 4 21:06 ho.txt folder_02: total 8 -rwx------ 1 schmittu domainusers 40 Jun 4 21:06 today.txt ``` --- class: middle ## Writing to a file ```bash $ echo hi hi ``` `>>` appends to the file: ```bash $ echo hi > new.txt $ echo ho >> new.txt $ cat new.txt hi ho ``` --- class: middle - Show TAB completion and navigation with cursor keys! - Show use of cursor keys - Show use of nano text editor