Files
OBIJupyterHub/jupyterhub_volumes/course/unix.ipynb
2025-11-16 14:55:30 +01:00

898 lines
15 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "30438c30",
"metadata": {},
"source": [
"# Unix Essentials — Bash Practice Notebook\n",
"\n",
"This notebook contains 50 small Bash exercises grouped by topic.\n",
"Each exercise includes a hidden solution that can be revealed in Jupyter."
]
},
{
"cell_type": "markdown",
"id": "39d711f9",
"metadata": {},
"source": [
"## Series 1 — Paths and Directories"
]
},
{
"cell_type": "markdown",
"id": "6fb7335e",
"metadata": {},
"source": [
"### Exercise 1.1 — Show your current working directory"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2b8575f3",
"metadata": {},
"outputs": [],
"source": [
"# your work here\n",
"pwd"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "21f63bb0",
"metadata": {
"jupyter": {
"outputs_hidden": true,
"source_hidden": true
}
},
"outputs": [],
"source": [
"# Solution\n",
"pwd"
]
},
{
"cell_type": "markdown",
"id": "61a624bf",
"metadata": {},
"source": [
"### Exercise 1.2 — Create a new directory named `testdir`"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4a4df387",
"metadata": {},
"outputs": [],
"source": [
"# your work here\n",
"mkdir testdir"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2b5cfbca",
"metadata": {
"jupyter": {
"outputs_hidden": true,
"source_hidden": true
}
},
"outputs": [],
"source": [
"# Solution\n",
"mkdir testdir"
]
},
{
"cell_type": "markdown",
"id": "2dccbb61",
"metadata": {},
"source": [
"### Exercise 1.3 — Move into `testdir` using a relative path"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fb73ca3c",
"metadata": {},
"outputs": [],
"source": [
"# your work here\n",
"cd testdir"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ba670a20",
"metadata": {
"jupyter": {
"outputs_hidden": true,
"source_hidden": true
}
},
"outputs": [],
"source": [
"# Solution\n",
"cd testdir"
]
},
{
"cell_type": "markdown",
"id": "4f7e5817",
"metadata": {},
"source": [
"### Exercise 1.4 — Create two subdirectories `a` and `b` in one command"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "30be8f44",
"metadata": {},
"outputs": [],
"source": [
"# your work here\n",
"mkdir a b"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "caf92714",
"metadata": {
"jupyter": {
"outputs_hidden": true,
"source_hidden": true
}
},
"outputs": [],
"source": [
"# Solution\n",
"mkdir a b"
]
},
{
"cell_type": "markdown",
"id": "edec9041",
"metadata": {},
"source": [
"### Exercise 1.5 — Return to the parent directory using `..`"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "32a0f1df",
"metadata": {},
"outputs": [],
"source": [
"# your work here\n",
"cd .."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2a0aa92f",
"metadata": {
"jupyter": {
"outputs_hidden": true,
"source_hidden": true
}
},
"outputs": [],
"source": [
"# Solution\n",
"cd .."
]
},
{
"cell_type": "markdown",
"id": "31917159",
"metadata": {},
"source": [
"### Exercise 1.6 — Create nested directories `one/two/three` in a single command"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c0a313c3",
"metadata": {},
"outputs": [],
"source": [
"# your work here\n",
"mkdir -p one/two/three"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4af63dce",
"metadata": {
"jupyter": {
"outputs_hidden": true,
"source_hidden": true
}
},
"outputs": [],
"source": [
"# Solution\n",
"mkdir -p one/two/three"
]
},
{
"cell_type": "markdown",
"id": "24ee949e",
"metadata": {},
"source": [
"### Exercise 1.7 — List the absolute path of the current directory"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8258fff7",
"metadata": {},
"outputs": [],
"source": [
"# your work here\n",
"pwd"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a6a062b2",
"metadata": {
"jupyter": {
"outputs_hidden": true,
"source_hidden": true
}
},
"outputs": [],
"source": [
"# Solution\n",
"pwd"
]
},
{
"cell_type": "markdown",
"id": "6d724d3e",
"metadata": {},
"source": [
"### Exercise 1.8 — Create a directory using an absolute path"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5f497f7c",
"metadata": {},
"outputs": [],
"source": [
"# your work here\n",
"mkdir /tmp/mydir"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1d1d4e27",
"metadata": {
"jupyter": {
"outputs_hidden": true,
"source_hidden": true
}
},
"outputs": [],
"source": [
"# Solution\n",
"mkdir /tmp/mydir"
]
},
{
"cell_type": "markdown",
"id": "0a4cbe5b",
"metadata": {},
"source": [
"### Exercise 1.9 — Display both `.` and `..` directories with `ls -a`"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7d962a9f",
"metadata": {},
"outputs": [],
"source": [
"# your work here\n",
"ls -a"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "91994de2",
"metadata": {
"jupyter": {
"outputs_hidden": true,
"source_hidden": true
}
},
"outputs": [],
"source": [
"# Solution\n",
"ls -a"
]
},
{
"cell_type": "markdown",
"id": "e147ff34",
"metadata": {},
"source": [
"### Exercise 1.10 — Remove the directory `/tmp/mydir`"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dcae28e3",
"metadata": {},
"outputs": [],
"source": [
"# your work here\n",
"rmdir /tmp/mydir"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1a000f14",
"metadata": {
"jupyter": {
"outputs_hidden": true,
"source_hidden": true
}
},
"outputs": [],
"source": [
"# Solution\n",
"rmdir /tmp/mydir"
]
},
{
"cell_type": "markdown",
"id": "578548f7",
"metadata": {},
"source": [
"## Series 2 — File Manipulation"
]
},
{
"cell_type": "markdown",
"id": "813140d5",
"metadata": {},
"source": [
"### Exercise 2.1 — Create an empty file named `notes.txt`"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "08385872",
"metadata": {},
"outputs": [],
"source": [
"touch notes.txt"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a474ad37",
"metadata": {
"jupyter": {
"outputs_hidden": true,
"source_hidden": true
}
},
"outputs": [],
"source": [
"# Solution\n",
"touch notes.txt"
]
},
{
"cell_type": "markdown",
"id": "6e918f3d",
"metadata": {},
"source": [
"### Exercise 2.2 — Copy `notes.txt` to `backup.txt`"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "faf85985",
"metadata": {},
"outputs": [],
"source": [
"cp notes.txt backup.txt"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4d3e54d5",
"metadata": {
"jupyter": {
"outputs_hidden": true,
"source_hidden": true
}
},
"outputs": [],
"source": [
"# Solution\n",
"cp notes.txt backup.txt"
]
},
{
"cell_type": "markdown",
"id": "2c924f2e",
"metadata": {},
"source": [
"### Exercise 2.3 — Rename `backup.txt` to `archive.txt`"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2d4adeb4",
"metadata": {},
"outputs": [],
"source": [
"mv backup.txt archive.txt"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2c62e70f",
"metadata": {
"jupyter": {
"outputs_hidden": true,
"source_hidden": true
}
},
"outputs": [],
"source": [
"# Solution\n",
"mv backup.txt archive.txt"
]
},
{
"cell_type": "markdown",
"id": "c9e525a7",
"metadata": {},
"source": [
"### Exercise 2.4 — Create 3 files and remove one with `rm`"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3ef53a3a",
"metadata": {},
"outputs": [],
"source": [
"touch a.txt b.txt c.txt\n",
"rm b.txt"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "55e07074",
"metadata": {
"jupyter": {
"outputs_hidden": true,
"source_hidden": true
}
},
"outputs": [],
"source": [
"# Solution\n",
"touch a.txt b.txt c.txt\n",
"rm b.txt"
]
},
{
"cell_type": "markdown",
"id": "b5a406b4",
"metadata": {},
"source": [
"### Exercise 2.5 — Remove a directory recursively"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2c819a49",
"metadata": {},
"outputs": [],
"source": [
"mkdir -p tmpdir/sub\n",
"rm -rf tmpdir"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b958a668",
"metadata": {
"jupyter": {
"outputs_hidden": true,
"source_hidden": true
}
},
"outputs": [],
"source": [
"# Solution\n",
"mkdir -p tmpdir/sub\n",
"rm -rf tmpdir"
]
},
{
"cell_type": "markdown",
"id": "9aa4996e",
"metadata": {},
"source": [
"## Series 3 — Redirections and Pipes"
]
},
{
"cell_type": "markdown",
"id": "ada90797",
"metadata": {},
"source": [
"### Exercise 3.1 — Save the output of `ls` to a file `list.txt`"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "25afa75b",
"metadata": {},
"outputs": [],
"source": [
"ls > list.txt"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "80ca4767",
"metadata": {
"jupyter": {
"outputs_hidden": true,
"source_hidden": true
}
},
"outputs": [],
"source": [
"# Solution\n",
"ls > list.txt"
]
},
{
"cell_type": "markdown",
"id": "61c3bff6",
"metadata": {},
"source": [
"### Exercise 3.2 — Append the date to `list.txt`"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b7849f15",
"metadata": {},
"outputs": [],
"source": [
"date >> list.txt"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "83b08fe6",
"metadata": {
"jupyter": {
"outputs_hidden": true,
"source_hidden": true
}
},
"outputs": [],
"source": [
"# Solution\n",
"date >> list.txt"
]
},
{
"cell_type": "markdown",
"id": "600edcaa",
"metadata": {},
"source": [
"### Exercise 3.3 — Count lines of `list.txt` with a pipe"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "37e859f4",
"metadata": {},
"outputs": [],
"source": [
"cat list.txt | wc -l"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d1429a1c",
"metadata": {
"jupyter": {
"outputs_hidden": true,
"source_hidden": true
}
},
"outputs": [],
"source": [
"# Solution\n",
"cat list.txt | wc -l"
]
},
{
"cell_type": "markdown",
"id": "a901cf34",
"metadata": {},
"source": [
"### Exercise 3.4 — Redirect error messages to a file"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f2c1d530",
"metadata": {},
"outputs": [],
"source": [
"ls /fakepath 2> errors.txt"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "66c68bc4",
"metadata": {
"jupyter": {
"outputs_hidden": true,
"source_hidden": true
}
},
"outputs": [],
"source": [
"# Solution\n",
"ls /fakepath 2> errors.txt"
]
},
{
"cell_type": "markdown",
"id": "72e5eca4",
"metadata": {},
"source": [
"## Series 4 — Viewing Results"
]
},
{
"cell_type": "markdown",
"id": "dd7cbf2e",
"metadata": {},
"source": [
"### Exercise 4.1 — Display the first 5 lines of `/etc/passwd`"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "195c0f25",
"metadata": {},
"outputs": [],
"source": [
"head -5 /etc/passwd"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "72dc0297",
"metadata": {
"jupyter": {
"outputs_hidden": true,
"source_hidden": true
}
},
"outputs": [],
"source": [
"# Solution\n",
"head -5 /etc/passwd"
]
},
{
"cell_type": "markdown",
"id": "227ebdf8",
"metadata": {},
"source": [
"### Exercise 4.2 — Show the last 3 lines of `/etc/passwd`"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3a829074",
"metadata": {},
"outputs": [],
"source": [
"tail -3 /etc/passwd"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e66f1752",
"metadata": {
"jupyter": {
"outputs_hidden": true,
"source_hidden": true
}
},
"outputs": [],
"source": [
"# Solution\n",
"tail -3 /etc/passwd"
]
},
{
"cell_type": "markdown",
"id": "826cbb41",
"metadata": {},
"source": [
"### Exercise 4.3 — Use `grep` to show lines containing 'root'"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "df9a877b",
"metadata": {},
"outputs": [],
"source": [
"grep root /etc/passwd"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5ed43d3d",
"metadata": {
"jupyter": {
"outputs_hidden": true,
"source_hidden": true
}
},
"outputs": [],
"source": [
"# Solution\n",
"grep root /etc/passwd"
]
},
{
"cell_type": "markdown",
"id": "a6c2f0a8",
"metadata": {},
"source": [
"## Series 5 — Process Management"
]
},
{
"cell_type": "markdown",
"id": "333ab7c1",
"metadata": {},
"source": [
"### Exercise 5.1 — List current processes"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "41dc9dfb",
"metadata": {},
"outputs": [],
"source": [
"ps"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c0f28455",
"metadata": {
"jupyter": {
"outputs_hidden": true,
"source_hidden": true
}
},
"outputs": [],
"source": [
"# Solution\n",
"ps"
]
},
{
"cell_type": "markdown",
"id": "c2d37e8f",
"metadata": {},
"source": [
"### Exercise 5.2 — Display dynamic process list with `top`"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7931c9aa",
"metadata": {},
"outputs": [],
"source": [
"top"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "947570cf",
"metadata": {
"jupyter": {
"outputs_hidden": true,
"source_hidden": true
}
},
"outputs": [],
"source": [
"# Solution\n",
"top"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Bash",
"language": "bash",
"name": "bash"
},
"language_info": {
"name": "bash"
}
},
"nbformat": 4,
"nbformat_minor": 5
}