{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Commands, shells, processes, files.\n",
    "\n",
    "## Overview\n",
    "\n",
    "Today, we'll first talk about the course organization. <br>\n",
    "Then we'll discuss the fundamentals of the Linux operating system: kernel, processes, file system, user accounts, and basic commands."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Course organization\n",
    "\n",
    "- Virtual Desktop (VD) access\n",
    "- Canvas\n",
    "- Slack channel\n",
    "- Course website\n",
    "- Lectures\n",
    "- Practical exercises\n",
    "- Midterm and Final exams\n",
    "\n",
    "<hr>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Course agenda\n",
    "\n",
    "- Lectures and practical recitations on the virtual computers remotely.\n",
    "- Midterm exam (on June 22 2026).\n",
    "- Final exam (on July 15 2026).\n",
    "\n",
    "<hr>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Course topics\n",
    "\n",
    "- Command line interface (CLI): Linux system commands, file systems, processes, services.\n",
    "- Operating system Virtualization with KVM and virtual machine management.\n",
    "- Package management (apt, dnf, rpm, dpkg).\n",
    "- Networking (TCP/IP basics, troubleshooting, configuration, services).\n",
    "- Network File System (NFS).\n",
    "- Shell scripting, awk, grep, sed, regular expressions.\n",
    "- Service startup and shutdown, at and cron scheduling.\n",
    "- Python: conda environment, jupyter notebook, various modules, computing with python.\n",
    "- Application compilation and libraries.\n",
    "- GPU computing: CUDA, neural networks, PyTorch.\n",
    "- Security topics: password, brute force attacks, iptables, port scanning.\n",
    "<hr>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## A typical Server/Desktop/Laptop (Intel Core i7) hardware\n",
    "\n",
    "- From the file system, OS and applications are loaded into the RAM.\n",
    "- The OS (kernel) controls the hardware and processes. \n",
    "- On the diagram:\n",
    "  - CPU \n",
    "  - RAM\n",
    "  - PCIe - Peripheral Component Interconnect Express, PCIe 3.0 here. Mordern PCIe 4.0 and 5.0 are on newer systems.\n",
    "  - PCH - Platform Controller Hub. Connects PCIe, USB, and SATA.\n",
    "  - DMI - Direct Memory Interface \n",
    "  - LPC - Low Pin Count; a simple interface to slower I/O devices \n",
    "  - Peripheral devices connected to PCIe, SATA, USB interfaces\n",
    "![title](img/Chipset_CPU.jpg)\n",
    "- computer architecture\n",
    "***\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Purpose of operating systems\n",
    "\n",
    "\n",
    "[Operating system concepts](https://en.wikipedia.org/wiki/Operating_system)\n",
    "\n",
    "Common features\n",
    "\n",
    "- [Device drivers](https://en.wikipedia.org/wiki/Device_driver)\n",
    "- [Interrupts](https://en.wikipedia.org/wiki/Interrupt)\n",
    "- [Process management (computing)](https://en.wikipedia.org/wiki/Process_management_(computing)) \n",
    "- [Memory management](https://en.wikipedia.org/wiki/Memory_management) \n",
    "- [Security](https://en.wikipedia.org/wiki/Computer_security)\n",
    "- [File system](https://en.wikipedia.org/wiki/File_system)\n",
    "- [I/O](https://en.wikipedia.org/wiki/Input/output)\n",
    "- [Networking](https://en.wikipedia.org/wiki/Computer_network) \n",
    "\n",
    "<br><br>\n",
    "\n",
    "![title](img/Operating_system_placement.png)\n",
    "- Operating system in the computing stack\n",
    "***"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Linux kernel and applications\n",
    "\n",
    "In general, Linux implies rather just a Kernel than a whole OS.\n",
    "\n",
    "Linux OS distributions include:\n",
    "generic or customized Kernel and\n",
    "generic or customized GNU software (libraries, shells, and applications).\n",
    "\n",
    "Source codes of the Kernel, shells and GNU applications are freely available to anyone.\n",
    "\n",
    "Traditional tools for Linux administration and development:\n",
    "   C - programming language\n",
    "   Shell scripting,\n",
    "   Perl,\n",
    "   Python\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "![Linux kernel](img/linux_kerne_app.jpg)\n",
    "- Linux kernel, applications, and hardware\n",
    "***"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Login to the virtual desktop (Exercise)\n",
    "\n",
    "\n",
    "![Connect to your VD in Ravada cluster](img/Ravada_connection_SSH.png)\n",
    "- Follow the HOWTO for VD connection in Canvas\n",
    "\n",
    "***"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Login to a Linux terminal\n",
    "![Login to Linux System](img/user_login.jpg)\n",
    "- Login procedure\n",
    "***"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## User accounts\n",
    "\n",
    "\n",
    "In order to login to a system a user has to authenticate with his/her credentials: user name and password.\n",
    "\n",
    "Authentication types:\n",
    "\n",
    "- local (password/shadow/group) and\n",
    "- Domain based (NIS, Kerberos, LDAP).\n",
    "\n",
    "For local authentication, a user should have an account on the system.\n",
    "\n",
    "\n",
    "<style> \n",
    "table td, table th, table tr {text-align:left !important;}\n",
    "</style>\n",
    "\n",
    "|Account File         | Entry\n",
    "|:-|:-\n",
    "| /etc/passwd | mike:x:1001:1001:Michael Whites:/home/mike:/bin/bash\n",
    "| /etc/shadow | mike:$1$zXCV7fz8ii84grbZhj:14087:0:99999:7:::\n",
    "| /etc/group  | mike:x:1001:mike    \n",
    "| /etc/group  | admin:x:112:hostadm,mike\n",
    "\n",
    "Accounts can be created only by a superuser (root).\n",
    "<br><br>\n",
    "Commands to create user accounts:\n",
    "<br><br><tt> adduser</tt> (interactive command)\n",
    "<br><br><tt>useradd </tt>(can be included in a script).\n",
    "\n",
    "***"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "###  Exercise\n",
    "Create a new user account by following the instructor.\n",
    "\n",
    "Become root: <br>\n",
    "\n",
    "```bash\n",
    "sudo -s\n",
    "```\n",
    "\n",
    "Create a new user account, mike: <br>\n",
    "\n",
    "```bash\n",
    "adduser mike\n",
    "```\n",
    "\n",
    "Verify the account existence: <br>\n",
    "\n",
    "```bash\n",
    "id mike\n",
    "```\n",
    "\n",
    "Change password of the user: <br>\n",
    "\n",
    "```bash\n",
    "passwd mike\n",
    "```\n",
    "\n",
    "***"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Linux shells\n",
    "\n",
    "When you login to a Linux system, you get a command shell.\n",
    "\n",
    "Shells are listed in `/etc/shells`. Default Linux shell: `bash`\n",
    "\n",
    "System commands, scripts and applications run in the shell - they become child processes of the shell.\n",
    "\n",
    "Besides commands, the shell is using variables.\n",
    "\n",
    "You define a shell variable with assignment command, ```=```, for example:\n",
    "```bash\n",
    "svar='file_name.txt'\n",
    "```\n",
    "The variable is referenced in the shell with ```$``` prefix, for example:\n",
    "```bash\n",
    "echo $svar\n",
    "```\n",
    "\n",
    "Shell variables are local to the shell.\n",
    "\n",
    "A shell variable becomes an environment variable after executing command export on it: <br>\n",
    "\n",
    "```bash\n",
    "svar='VAR_1'  #initialize shell variable\n",
    "export svar #becomes environment variable\n",
    "env | grep svar\n",
    "```\n",
    "\n",
    "Environment variables are inherited by the child shells and processes.\n",
    "\n",
    "Commands executed in the shell should be either built-in shell commands, or addressed with the full path, or located in the `PATH` environment variable. <br>\n",
    "\n",
    "```bash\n",
    "echo $PATH\n",
    "```\n",
    "\n",
    "***"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### Exercise\n",
    "\n",
    "Create a new shell variable, svar: <br>\n",
    "\n",
    "```bash\n",
    "svar='VAR_1'\n",
    "echo $svar\n",
    "```\n",
    "\n",
    "Start a new child shell and see if svar is defined there:<br>\n",
    "\n",
    "```bash\n",
    "bash\n",
    "echo $svar\n",
    "```\n",
    "\n",
    "Exit from the child shell, export the variable, and see if it is defined in a child shell:<br>\n",
    "\n",
    "```bash\n",
    "exit\n",
    "export svar\n",
    "bash\n",
    "echo $svar\n",
    "```\n",
    "\n",
    "***"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Data storage hierarchy\n",
    "\n",
    "- Data is stored in files.\n",
    "- Files are created in a file system.\n",
    "- A file system rezides on a volume.\n",
    "- A volume is created on drives.\n",
    "\n",
    "![How data is stored](img/Device_and_file_system.png)\n",
    "\n",
    "- A file system can also be created on the whole drive or a slice of the drive (partition).\n",
    "- To show up as a directory (folder), a file system needs to be mounted.\n",
    "***"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## HDD and SSD hardware\n",
    "::::{grid} 2\n",
    ":::{grid-item-card} Mechanical hard drive\n",
    "```{image} img/hd_partitions.jpg\n",
    ":alt: fishy\n",
    ":class: bg-primary mb-1\n",
    ":width: 200px\n",
    ":align: center\n",
    "```\n",
    ":::\n",
    ":::{grid-item-card}  Elements of a file system\n",
    "- The cylinders define partition boundaries. \n",
    "- The sectors define data blocks.\n",
    "- A file system is created within a disk partition.\n",
    "- Files are created within a file system.\n",
    "- HDD endurance is about 5 years.\n",
    ":::\n",
    "::::\n",
    "\n",
    "\n",
    ":::{card}\n",
    "\n",
    "NAND cell technology for SSD, SD, NVMe, Flash drive.\n",
    "^^^\n",
    "```{image} img/NAND-Cell-Read-Programmed-and-Erased-fd945b79.png\n",
    ":alt: fishy\n",
    ":class: bg-primary mb-1\n",
    ":width: 600px\n",
    ":align: center\n",
    "```\n",
    "\n",
    "---\n",
    "NAND cells are combined into strings, pages, blocks, planes, and banks.\n",
    "```{image} img/Basic-SSD-Diagram-6b8c8849.png\n",
    ":alt: fishy\n",
    ":class: bg-primary mb-1\n",
    ":width: 600px\n",
    ":align: center\n",
    "```\n",
    ":::\n",
    "\n",
    "\n",
    "| SSD Type: | SLC (Single-Level Cell) (1 bit/cell) | MLC (2 bits/cell) | TLC (3 bits) | QLC (4 bits) \n",
    "|:-     | :-        | :-        |  :-  | :-\n",
    "|Endurance | 100k cycles | 10k cycles | 3K cycles | 1K cycles\n",
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## HDD vs SSD\n",
    "\n",
    "### Typical HDD and SSD on a market in 2025\n",
    "\n",
    "|Type     | Capacity | Price | Throughput | Latency  | IOPS |\n",
    "|:------: | :---:     | :---:  |:----------: | :-----  | :------- |\n",
    "| SATA HD | 20 TB    | $500  | 120 MB/s   | 5 ms    | 100      |\n",
    "| SATA SSD | 4 TB    | $500  | 500 MB/s   | 0.05 ms | 100,000  |\n",
    "| NVMe SSD | 4 TB    | $500  | 7,000 MB/s | 0.02 ms | 500,000  |"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Files and Directories\n",
    "\n",
    "::::{grid} 2\n",
    ":::{grid-item-card}\n",
    "```{image} img/directory_entry.jpg\n",
    ":alt: fishy\n",
    ":class: bg-primary mb-1\n",
    ":width: 200px\n",
    ":align: center\n",
    ":::\n",
    ":::{grid-item-card} INODES contain information about files\n",
    "\n",
    "- a pointer to the file data blocks\n",
    "- link count\n",
    "- the owner\n",
    "- the group\n",
    "- mode\n",
    "- size\n",
    "- last access time\n",
    "- last modification time\n",
    "- last update time (attributes)\n",
    ":::\n",
    "::::\n",
    "\n",
    "::::{grid} 2\n",
    ":::{grid-item-card} Type of files\n",
    "- Regular or ordinary files\n",
    "- Directories\n",
    "- Symbolic links\n",
    "- Device files\n",
    "- Directory and Inodes\n",
    ":::\n",
    ":::{grid-item-card} Files are stored by blocks\n",
    "Default block size of ext4 file system is 4K:\n",
    "```bash\n",
    "stat -f /home\n",
    ":::\n",
    "::::"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## File Permissions and Ownerships\n",
    "\n",
    "### Permissions\n",
    "|Octal\t| Binary\t| Permission |\tMeaning\n",
    "|:-     | :-        | :-:        |  :-\n",
    "|0\t|000\t|none\t|All turned off\n",
    "|1\t|001\t|--x\t|Execute\n",
    "|2\t|010\t|-w-\t|Write\n",
    "|3\t|011\t|-wx\t|Write, execute\n",
    "|4\t|100\t|r--\t|Read\n",
    "|5\t|101\t|r-x\t|Read, execute\n",
    "|6\t|110\t|rw-\t|Read, write\n",
    "|7\t|111\t|rwx\t|Read, write, execute\n",
    "\n",
    "### File Ownerships: USER (u), GROUP (g), OTHERS (o)\n",
    "\n",
    "Changing permissions\n",
    "Permissions are applied for USER, GROUP and OTHERS (rwx rwx rwx)\n",
    "\n",
    "```bash\n",
    "chmod 660 testf.txt\n",
    "ls -l  testf.txt\n",
    "```\n",
    "\n",
    "```\n",
    "-rw-rw----    1 mike   staff          0 Jan 18 10:26 testf.txt\n",
    "```\n",
    "\n",
    "umask sets default permission for files and directories.\n",
    "For example,\n",
    "\n",
    "```bash\n",
    "umask 022\n",
    "```\n",
    "\n",
    "|perm/umask| directory | file\n",
    "|:-         | :-     | :-\n",
    "|permission|777 \t|666 \n",
    "|umask |-022 \t|-022 \n",
    "|permission|755 \t|644 \n",
    "\n",
    "***"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Assigning permissions and ownerships on files (Exercise)\n",
    "\n",
    "\n",
    "Create a new directory `EX1` and step into it:\n",
    "\n",
    "```bash\n",
    "mkdir EX1\n",
    "cd EX1\n",
    "```\n",
    "\n",
    "Check `umask` and create a new file, `mf1.txt`. See the file attributes with command `ls`.\n",
    "\n",
    "```bash\n",
    "umask\n",
    "touch mf1.txt\n",
    "ls -l mf1.txt\n",
    "```\n",
    "\n",
    "Change `umask` and create anothe file, `mf2.txt`\n",
    "\n",
    "```bash\n",
    "umask 022\n",
    "touch mf2.txt\n",
    "ls -l mf2.txt\n",
    "```\n",
    "\n",
    "Create another file, <TT>mf3.exe</TT> and make it executable:\n",
    "\n",
    "```bash\n",
    "touch mf3.exe\n",
    "chmod 755 mf3.exe\n",
    "ls -l mf3.exe\n",
    "```\n",
    "\n",
    "Try runing files `mf2.txt` and `mf3.exe`, and see which one is runnable:\n",
    "\n",
    "```bash\n",
    "./mf2.txt\n",
    "./mf3.exe\n",
    "```\n",
    "\n",
    "As you can see only the executable file can run.\n",
    "<br>\n",
    "Changing the user ownership on file `mf1.txt`:\n",
    "\n",
    "```bash\n",
    "sudo chown mike mf1.txt\n",
    "ls -l mf1.txt\n",
    "```\n",
    "\n",
    "Note, you need to be the root user when changing the file ownership.</li>\n",
    "<br>\n",
    "\n",
    "Changing the group ownership on file `mf2.txt`:\n",
    "\n",
    "```bash\n",
    "sudo chown :mike mf2.txt\n",
    "ls -l mf2.txt\n",
    "```\n",
    "\n",
    "Changing both the user and group ownership on file `mf3.exe`:\n",
    "\n",
    "```bash\n",
    "sudo chown mike:mike mf3.exe\n",
    "ls -l mf*\n",
    "```\n",
    "\n",
    "***"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "scrolled": true,
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Sticky bit (Exercise)\n",
    "\n",
    "Sticky bit on a directory protects files in the directory from been modified/removed by non their owners.\n",
    "\n",
    "For example `/tmp` directory on a Unix/Linux system has a sticky bit set\n",
    "\n",
    "\n",
    "```bash\n",
    "ls -ld /tmp\n",
    "```\n",
    "\n",
    "```    \n",
    "drwxrwxrwt 4 root root 4096 2018-07-25 16:29 /tmp\n",
    "```\n",
    "\n",
    "\n",
    "\n",
    "Open a new terminal and become root:\n",
    "\n",
    "```bash\n",
    "sudo -s\n",
    "```\n",
    "\n",
    "Create a new directory, `temp`, and give it a world writable permissions\n",
    "\n",
    "```bash\n",
    "mkdir temp\n",
    "chmod 777 temp\n",
    "```\n",
    "\n",
    "In the directory, user root creates a new empty file:\n",
    "\n",
    "```bash\n",
    "cd temp\n",
    "touch ex3\n",
    "```\n",
    "\n",
    "In the other terminal,try removing this file as user hostadm:\n",
    "\n",
    "```bash\n",
    "cd temp\n",
    "rm ex3\n",
    "```\n",
    "\n",
    "The file has been deleted. \n",
    "<br>\n",
    "\n",
    "Change the permission on `temp` director in the root user terminal: \n",
    "\n",
    "```bash\n",
    "chmod 1777 temp\n",
    "```\n",
    "\n",
    "Then repeat the steps above with creating and removing file `ex3` in directory `temp`\n",
    "\n",
    "***\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Processes\n",
    "\n",
    "Program running on a system is a process. Linux is a multi-processing (multi-tasking) system.\n",
    "\n",
    "Processes are created by forking from a parent process, for example an application is forked from a user shell.\n",
    "\n",
    "Process types: \n",
    "\n",
    "- User Processes.   A user process is one that is initiated by a regular user account and runs in user space.\n",
    "\n",
    "- Daemon Processes.  A daemon process is an application that is designed to run in the background, usually related to a service.\n",
    "\n",
    "- Kernel Threads.   Kernel processes execute only in kernel space.\n",
    "\n",
    "\n",
    "Privilege levels. Kernel space and user space:\n",
    "\n",
    "\n",
    "<img src=\"img/2560px-Priv_rings.svg.png\" width=\"400\"></img>\n",
    "- Process privilege levels. The kernel threads run in Ring 0, the User and Daemon processes run in Ring 3."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Process states\n",
    "\n",
    "<a href=https://en.wikipedia.org/wiki/Process_state>When created, a process goes through various states:</a>\n",
    "\n",
    "<img src=\"img/1920px-Process_states.svg.png\" width=400></img>\n",
    "- Evolution of the process state"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "***\n",
    "## Process termination and renice command\n",
    "\n",
    "* Program running on a system is a process. Linux is a multi-processing\n",
    "  (multi-tasking) system.\n",
    " \n",
    "* Attributes: Lifetime, PID, PPID, UID, GID, env variables, CWD.\n",
    " \n",
    "* Process with PID=1 is `init`\n",
    "    \n",
    "* Monitoring: `ps` (for full listing, use `ps -ef` or `ps -aux`), `top`, `pstree`\n",
    "    \n",
    "* Signal a running processes with PID=1009:\n",
    "\n",
    "    ```bash\n",
    "    kill -HUP 1009    #Hang up; re-read config files\n",
    "    kill -1   1009\n",
    "    kill -9  1009     #Kill; stop unconditionally\n",
    "    kill -KILL 1009\n",
    "    kill -15   1009    #Terminate gracefully\n",
    "    kill -TERM 1009\n",
    "    kill -TSTP 1009   #Suspend; can be continued\n",
    "    kill -18   1009\n",
    "    ```\n",
    "    \n",
    "* Process can be started with lower and higher priority through `nice`\n",
    "(range: -20 highest, +19 lowest)\n",
    "\n",
    "    ```bash\n",
    "    nice -10 matlab\n",
    "    nice --10 matlab\n",
    "    ```\n",
    "\n",
    "* Process can be re-niced at run time:\n",
    "\n",
    "    ```bash\n",
    "    renice 5 1009\n",
    "    renice -5 1009\n",
    "    ```\n",
    "    \n",
    "    \n",
    "    \n",
    "## Exercise with process termination\n",
    "\n",
    "    \n",
    "When administrating a Linux system, it is often needed to terminate processes that consume a lot of resources (CPU and/or RAM), and slow down everything else.\n",
    "<br>\n",
    "    \n",
    "* Download a python script, and make it executable:\n",
    "\n",
    "    ```bash\n",
    "    wget http://capone.rutgers.edu/coursefiles/inf_loop.py\n",
    "    chmod 755 inf_loop.py\n",
    "    ```\n",
    "    \n",
    "\n",
    "* Start `inf_loop.py` scrip on a \"background\":\n",
    "\n",
    "    ```bash\n",
    "    ./inf_loop.py &\n",
    "    ```\n",
    "\n",
    "* Find out the <b>PID</b> of the process:\n",
    "\n",
    "    ```bash\n",
    "    pgrep inf_loop.py\n",
    "    ```\n",
    "\n",
    "    The output shows the <b>PID</b> of the process, for exmple 4198.\n",
    "    <br>\n",
    "    Terminate process 4198:\n",
    "\n",
    "    ```bash\n",
    "    kill -15 4198\n",
    "    ```\n",
    "\n",
    "    An alternative way to kill an application  is by using `pkill` command:\n",
    "\n",
    "    ```bash\n",
    "    pkill -15 inf_loop.py\n",
    "    ```\n",
    "\n",
    "    If option `-15` above doesn't work, try `-9`\n",
    "\n",
    "    \n",
    "* <a href=\"https://linuxjourney.com/lesson/monitor-processes-ps-command\" target=\"resource_window\">Reference: Understanding Linux processes</a></li>\n",
    "\n",
    "***"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Process data streams\n",
    "\n",
    "* Most of the Linux system processes have three data streams with file descriptors \"0\", \"1\", and \"3\":\n",
    "    \n",
    "    standard input \"0\" (stdin)\n",
    "\n",
    "    standard output \"1\" (stdo)\n",
    "\n",
    "    standard error \"2\" (stderr)\n",
    "\n",
    "\n",
    "* The standard output and standard error are directed to the screen of your monitor; the standard input is read from a keyboard.\n",
    "\n",
    "    It is possible to redirect the standard output and error into files, for example\n",
    "    ```bash\n",
    "    ps -ef 1 > stdo.out\n",
    "    ```\n",
    "\n",
    "    or\n",
    "\n",
    "    ```bash\n",
    "    ps -ef > stdo.out\n",
    "    ```\n",
    "\n",
    "    To redirect both stdo and stderr to the same file:\n",
    "\n",
    "    `command > output.txt 2>&1`\n",
    "\n",
    "    For example, \n",
    "    ```bash\n",
    "    ps -ef > output.txt 2>&1\n",
    "    ```\n",
    "\n",
    "    If you need to discard the stdo stream, you can re-direct it to `/dev/null`:\n",
    "    ```bash\n",
    "    ps -ef 1>/dev/null\n",
    "    ```\n",
    "\n",
    "    <br>\n",
    "    It is also possible to re-direct the standart output of one process into\n",
    "     the standard input of the other using pipes \"|\":\n",
    "     \n",
    "    ```bash\n",
    "    ps -ef | less\n",
    "    ```\n",
    "    \n",
    "    ***"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "##  Background processes (exercise)\n",
    "\n",
    "<br>\n",
    "Processes with Input/Output detached from the terminal are called background processes.\n",
    "<br>\n",
    "\n",
    "\n",
    "In a terminal window, run script <tt>inf_loop.py</tt>:\n",
    "\n",
    "```bash\n",
    "inf_loop.py\n",
    "```\n",
    "\n",
    "<br>\n",
    "Suspend the interactive process and move it to the background:\n",
    "\n",
    "```bash\n",
    "# Ctrl-Z\n",
    "jobs\n",
    "bg %1\n",
    "```\n",
    "\n",
    "Now the shell can be used for running the other commands and applications.\n",
    "Move the background job (with its ID) back to the foreground and terminate\n",
    "ing_loop.py process:\n",
    "\n",
    "```bash\n",
    "fg %1\n",
    "# Ctrl-C\n",
    "```\n",
    "\n",
    "***"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Commands for directory manipulation (exercise)\n",
    "\n",
    "When you login into the system, you automatically get into your home directory\n",
    "\n",
    "#1. Determine the directory you are in:\n",
    "```bash \n",
    "pwd\n",
    "```\n",
    "#2. Go to some other directory, for example, /etc:      \n",
    "```bash\n",
    "cd /etc\n",
    "```\n",
    "#3. Go to your home directory:\n",
    "```bash\n",
    "cd $HOME\n",
    "```\n",
    "      or\n",
    "```bash \n",
    "cd ~\n",
    "```\n",
    "#4. Go back-and-forth between directories and check where you are:\n",
    "```bash\n",
    "cd /etc\n",
    "pwd\n",
    "cd -\n",
    "pwd\n",
    "cd -\n",
    "pwd\n",
    "```\n",
    "#5. Create a new directory above your home directory:\n",
    "```bash\n",
    "mkdir Newdir\n",
    "```\n",
    "#6. Go to the new directory and check where you are:\n",
    "```bash\n",
    "cd Newdir\n",
    "pwd\n",
    "```\n",
    "#7. Go one step back to the parent directory and check where you are:\n",
    "```bash\n",
    "cd ..\n",
    "pwd\n",
    "```\n",
    "#8. Remove the new directory:\n",
    "```bash\n",
    "rmdir Newdir\n",
    "```\n",
    "#9. Create a new directory above your home directory with subdirectories. Try the following:\n",
    "```bash\n",
    "mkdir Newdir/one/two/three\n",
    "```\n",
    "Does it work? Try the following:\n",
    "```bash\n",
    "mkdir -p Newdir/one/two/three\n",
    "```\n",
    "#10. Create a few files in Newdir/one:\n",
    "```bash\n",
    "cd Newdir/one; touch f1.t; touch f2.t; touch f3.t\n",
    "```\n",
    "#11. Go back into the original directory and try to delete Newdir:\n",
    "```bash\n",
    "cd -\n",
    "rmdir Newdir\n",
    "```\n",
    "Does it work ? Try the following:\n",
    "```bash\n",
    "rm -R Newdir\n",
    "```\n",
    "#12. Repeat step #9;\n",
    "Copy the directory with its tree into another directory:\n",
    "```bash\n",
    "cp -dpR Newdir Newdir-1\n",
    "```\n",
    "same as\n",
    "```bash\n",
    "cp -a Newdir Newdir-1\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Commands to read text file content (exercise)\n",
    "\n",
    "#13. Show content of file /etc/hosts\n",
    "```bash\n",
    "cat /etc/hosts\n",
    "tac /etc/hosts\n",
    "less /etc/hosts\n",
    "```\n",
    "#14. Show the first and last 10 lines of a file\n",
    "```bash\n",
    "head -n 10 /etc/nsswitch.conf\n",
    "tail -n 10 /etc/nsswitch.conf\n",
    "```\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Various important commands (exercise)\n",
    "\n",
    "#15. Show date on the system clock\n",
    "```bash\n",
    "date\n",
    "```\n",
    "#16. File system usage\n",
    "```bash\n",
    "df -h\n",
    "```\n",
    "#17. Disk usage\n",
    "```bash\n",
    "du -h /home\n",
    "du -s /home\n",
    "du --max-depth=1 /var\n",
    "du -x -h /\n",
    "```\n",
    "#18. Display data\n",
    "```bash\n",
    "echo $[10*3+2]\n",
    "echo '$[10*3+2]'\n",
    "echo \"$[10*3+2]\"\n",
    "```\n",
    "#19. Determine file type\n",
    "```bash\n",
    "file /bin/ls\n",
    "file /etc/hosts\n",
    "```\n",
    "#20. Who is logined to the system\n",
    "```bash\n",
    "who\n",
    "```\n",
    "#21. The owner of the current shell\n",
    "```bash\n",
    "whoami\n",
    "```\n",
    "#22. Getting information about a file or command, for example netstat\n",
    "```bash\n",
    "whatis netstat\n",
    "apropos netstat\n",
    "man netstat\n",
    "info netstat\n",
    "```\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "#23. Compressing/uncompressing files with gzip, and bzip2:\n",
    "```bash\n",
    "cd ~\n",
    "cp /etc/hosts  hosts.txt\n",
    "gzip hosts.txt\n",
    "ls -l\n",
    "less hosts.txt.gz\n",
    "more hosts.txt.gz\n",
    "zcat hosts.txt.gz\n",
    "gunzip hosts.txt.gz\n",
    "bzip2 hosts.txt\n",
    "bzip2 -d hosts.txt.bz2\n",
    "```\n",
    "#24. Searching for files by using command find\n",
    "```bash\n",
    "cd /tmp\n",
    "mkdir newfiles; cd newfiles\n",
    "touch fff.txt\n",
    "cd ~\n",
    "find / -name fff.txt\n",
    "cd /tmp\n",
    "find . -name fff.txt\n",
    "find . -name '*ff*'\n",
    "find /tmp -name fff.txt -exec rm '{}' ';'\n",
    "find /var -size +1000k\n",
    "find /var -size +1000000c\n",
    "find /home -user hostadm\n",
    "find /var -mtime -5\n",
    "find /var -maxdepth 2 -mtime -20\n",
    "```\n",
    "#25. Searching for system files\n",
    "```bash\n",
    "which dpkg\n",
    "whereis dpkg\n",
    "locate dpkg\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "#26. Using tar-gzip to archive a directory.\n",
    "Create a new directory with subdirectories above your home directory, then tar and gzip it. For example,\n",
    "```bash\n",
    "mkdir -p Newdir/one/two/three\n",
    "cd Newdir/one/two\n",
    "cp /etc/hosts .\n",
    "cd three\n",
    "cp /etc/passwd .\n",
    "cd ~\n",
    "tar -cvf Newdir.tar Newdir\n",
    "gzip Newdir.tar\n",
    "ls -l Newdir.tar.gz\n",
    "rm -R Newdir\n",
    "cp Newdir.tar.gz /tmp\n",
    "cd /tmp\n",
    "gunzip Newdir.tar.gz\n",
    "tar -xvf Newdir.tar\n",
    "```\n",
    "#27. To tar and gzip file with one command, try the following:\n",
    "```bash\n",
    "tar -zcvf Newdir.tgz Newdir\n",
    "rm -R Newdir\n",
    "ls -l Newdir.tgz\n",
    "tar -zxvf Newdir.tgz\n",
    "```\n",
    "#28. Archive/restore from input file list by cpio command:\n",
    "```bash\n",
    "cd /etc\n",
    "find . | cpio -ov  > /tmp/etc.cpio\n",
    "mkdir /tmp/RESTORE\n",
    "cd /tmp/RESTORE\n",
    "cpio -iv < ../etc.cpio\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "#29. Download files from a website:\n",
    "```bash\n",
    "wget https://people.sc.fsu.edu/~jburkardt/c_src/laplace_mpi/laplace_mpi.c\n",
    "```\n",
    "   Similarly\n",
    "```bash\n",
    "curl -O https://people.sc.fsu.edu/~jburkardt/c_src/laplace_mpi/laplace_mpi.c\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "#30. Command history in bash shell:\n",
    "```bash\n",
    "history\n",
    "```\n",
    "Find a command in the history by the regular expression in its name:\n",
    "```bash\n",
    "ctrl + R\n",
    "```\n",
    "then type the string contained in the command.\n",
    "\n",
    "\n",
    "#31. Command history file .bash_history is updated when a user exits the shell.\n",
    "```bash\n",
    "tail .bash_history\n",
    "```\n",
    "The size or file .bash_history is defined by env variable HISTSIZE\n",
    "```bash\n",
    "echo $HISTSIZE\n",
    "```\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "#32. Commands to read hardware information.\n",
    "CPU info:\n",
    "```bash\n",
    "lscpu\n",
    "```\n",
    "PCI device info:\n",
    "```bash\n",
    "lspci\n",
    "```\n",
    "\n",
    "Install package `lshw`:\n",
    "```bash\n",
    "sudo apt install lshw\n",
    "```\n",
    "\n",
    "Memory info:\n",
    "```bash\n",
    "lshw -class memory\n",
    "```\n",
    "Storage controller:\n",
    "```bash\n",
    "lshw -class storage\n",
    "```\n",
    "Disks:\n",
    "```bash\n",
    "lshw -class disk\n",
    "```\n",
    "Network interfaces:\n",
    "```bash\n",
    "lshw -class network \n",
    "```\n",
    "Video card and display info:\n",
    "```bash\n",
    "lshw -class video \n",
    "```"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
