{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Unix Essentials \u2014 Bash Practice Notebook\n\n", "This notebook contains 50 short exercises grouped in 5 series.\n", "Each exercise has a hidden solution for self-assessment." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Series 1 \u2013 Path Manipulation\n\nFocus: `pwd`, `cd`, `mkdir`, `.`, `..`, relative and absolute paths." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 1.1 \u2013 Show current directory" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\npwd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 1.2 \u2013 Create a directory named `work`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\nmkdir work" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 1.3 \u2013 Change to the `work` directory" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\ncd work" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 1.4 \u2013 Create two subdirectories `data` and `logs`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\nmkdir data logs" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 1.5 \u2013 Move back to the parent directory" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\ncd .." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 1.6 \u2013 Create nested directories `project/src` in one command" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\nmkdir -p project/src" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 1.7 \u2013 Display the absolute path" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\npwd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 1.8 \u2013 List all including hidden ones" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\nls -a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 1.9 \u2013 Create directory using absolute path `/tmp/test1`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\nmkdir /tmp/test1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 1.10 \u2013 Remove directory `/tmp/test1`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\nrmdir /tmp/test1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Series 2 \u2013 File Manipulation\n\nFocus: `rm`, `rm -rf`, `rmdir`, `cp`, `mv`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 2.1 \u2013 Create an empty file `a.txt`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\ntouch a.txt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 2.2 \u2013 Copy `a.txt` to `b.txt`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\ncp a.txt b.txt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 2.3 \u2013 Rename `b.txt` to `c.txt`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\nmv b.txt c.txt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 2.4 \u2013 Move `c.txt` to `/tmp/`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\nmv c.txt /tmp/" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 2.5 \u2013 Create a folder `tempdir`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\nmkdir tempdir" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 2.6 \u2013 Create files `x.txt`, `y.txt` in it" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\ntouch tempdir/x.txt tempdir/y.txt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 2.7 \u2013 Remove `y.txt`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\nrm tempdir/y.txt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 2.8 \u2013 Remove non-empty folder `tempdir` recursively" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\nrm -rf tempdir" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 2.9 \u2013 Create and remove empty folder `delete_me`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\nmkdir delete_me && rmdir delete_me" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 2.10 \u2013 Create file with text using echo" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\necho 'hello world' > greeting.txt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Series 3 \u2013 Redirections and Pipes\n\nFocus: Redirect input/output, combine with pipes." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 3.1 \u2013 Save `ls` output to file" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\nls > list.txt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 3.2 \u2013 Append date to `list.txt`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\ndate >> list.txt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 3.3 \u2013 View first 5 lines of `list.txt`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\nhead -5 list.txt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 3.4 \u2013 Count lines using pipe" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\ncat list.txt | wc -l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 3.5 \u2013 Redirect errors to `errors.txt`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\nls /fake 2> errors.txt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 3.6 \u2013 Redirect both output and errors" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\nls /fake > all.txt 2>&1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 3.7 \u2013 Use pipe to sort `list.txt`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\ncat list.txt | sort" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 3.8 \u2013 Combine commands with `| grep`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\nls /etc | grep conf" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 3.9 \u2013 Use input redirection `<` to count words" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\nwc -w < list.txt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 3.10 \u2013 Chain three commands" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\ncat list.txt | sort | uniq" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Series 4 \u2013 Viewing Results\n\nFocus: `head`, `tail`, `cat`, `grep`, `less`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 4.1 \u2013 Display first 10 lines of `/etc/passwd`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\nhead /etc/passwd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 4.2 \u2013 Show last 5 lines of `/etc/passwd`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\ntail -5 /etc/passwd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 4.3 \u2013 View complete file `/etc/hostname`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\ncat /etc/hostname" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 4.4 \u2013 Search for 'root' in `/etc/passwd`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\ngrep root /etc/passwd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 4.5 \u2013 Count matches of 'bash' in `/etc/passwd`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\ngrep -c bash /etc/passwd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 4.6 \u2013 Show line numbers with `grep`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\ngrep -n bash /etc/passwd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 4.7 \u2013 Invert match to exclude 'bash'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\ngrep -v bash /etc/passwd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 4.8 \u2013 Use `less` to browse `/etc/services`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\nless /etc/services" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 4.9 \u2013 Concatenate multiple small files" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\ncat a.txt greeting.txt > merged.txt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 4.10 \u2013 Show sorted unique words from `list.txt`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\ncat list.txt | tr ' ' '\\n' | sort | uniq" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Series 5 \u2013 Process Management\n\nFocus: `ps`, `kill`, `ctrl-c`, `ctrl-z`, `top`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 5.1 \u2013 List processes" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\nps" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 5.2 \u2013 Show detailed process tree" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\nps aux" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 5.3 \u2013 Find your shell PID" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\necho $$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 5.4 \u2013 Launch sleep in background" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\nsleep 60 &" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 5.5 \u2013 List background jobs" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\njobs" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 5.6 \u2013 Bring job to foreground" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\nfg" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 5.7 \u2013 Stop a process by PID" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\nkill " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 5.8 \u2013 Force kill a process" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\nkill -9 " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 5.9 \u2013 Display dynamic processes" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\ntop" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 5.10 \u2013 Exit `top` with `q` key" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# your work here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true, "outputs_hidden": true } }, "outputs": [], "source": [ "# Solution\n# press q in top" ] } ], "metadata": { "kernelspec": { "display_name": "Bash", "language": "bash", "name": "bash" }, "language_info": { "name": "bash" } }, "nbformat": 4, "nbformat_minor": 5 }