{ "cells": [ { "cell_type": "markdown", "id": "still-rehabilitation", "metadata": {}, "source": [ "# Navigate through files and directories" ] }, { "cell_type": "markdown", "id": "honey-proof", "metadata": {}, "source": [ "First let us find out where we are by running a command called `pwd`:" ] }, { "cell_type": "code", "execution_count": 1, "id": "revolutionary-bulletin", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/Users/veit/cusy/trn/Python4DataScience/docs/workspace/ipython/unix-shell\n" ] } ], "source": [ "!pwd" ] }, { "cell_type": "markdown", "id": "rotary-married", "metadata": {}, "source": [ "Here, the response is the iPython chapter of the Jupyter tutorial in my home directory `/Users/veit`.\n", "\n", "On Windows the home directory will look like `C:\\Documents and Settings\\veit` or `C:\\Users\\veit` and on Linux like `/home/veit`." ] }, { "cell_type": "markdown", "id": "cleared-china", "metadata": {}, "source": [ "To see the contents of our directory, we can use `ls`:" ] }, { "cell_type": "code", "execution_count": 2, "id": "perfect-courtesy", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "create-delete.ipynb index.rst\n", "create-delete.ipynb.license pipes-filters.ipynb\n", "file-system.ipynb pipes-filters.ipynb.license\n", "file-system.ipynb.license shell-variables.ipynb\n", "grep-find.ipynb shell-variables.ipynb.license\n", "grep-find.ipynb.license\n" ] } ], "source": [ "!ls" ] }, { "cell_type": "markdown", "id": "efficient-arkansas", "metadata": {}, "source": [ "* a trailing `/` indicates a directory\n", "* `@` indicates a link\n", "* `*` indicates an executable\n", "\n", "Depending on your default options, the shell might also use colors to indicate whether an entry is a file or a directory." ] }, { "cell_type": "markdown", "id": "polish-thursday", "metadata": {}, "source": [ "## `ls` options and arguments" ] }, { "cell_type": "code", "execution_count": 3, "id": "major-spell", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "debugging.ipynb\n", "debugging.ipynb.license\n", "display.ipynb\n", "display.ipynb.license\n", "examples.ipynb\n", "examples.ipynb.license\n", "extensions.rst\n", "importing.ipynb\n", "importing.ipynb.license\n", "index.rst\n", "magics.ipynb\n", "magics.ipynb.license\n", "\u001b[34mmypackage\u001b[m\u001b[m/\n", "myscript.py\n", "shell.ipynb\n", "shell.ipynb.license\n", "start.rst\n", "\u001b[31mtab-completion-for-anything.png\u001b[m\u001b[m*\n", "tab-completion-for-anything.png.license\n", "\u001b[31mtab-completion-for-modules.png\u001b[m\u001b[m*\n", "tab-completion-for-modules.png.license\n", "\u001b[31mtab-completion-for-objects.png\u001b[m\u001b[m*\n", "tab-completion-for-objects.png.license\n", "\u001b[34munix-shell\u001b[m\u001b[m/\n" ] } ], "source": [ "!ls -F ../" ] }, { "cell_type": "markdown", "id": "numeric-arena", "metadata": {}, "source": [ "`ls` is the command, with the option `-F` and the argument `../`. \n", "\n", "* Options either start with a single dash (`-`) or two dashes (`--`), and they change the behavior of a command.\n", "* Arguments tell the command what to operate on.\n", "* Options and arguments are sometimes also referred as parameters.\n", "* Each part is separated by spaces.\n", "* Also, capitalisation is important, for example \n", "\n", " * `ls -s` will display the size of files and directories alongside the names,\n", " * while `ls -S` will sort the files and directories by size." ] }, { "cell_type": "code", "execution_count": 4, "id": "imperial-reflection", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "total 224\n", "24 create-delete.ipynb 8 index.rst\n", " 8 create-delete.ipynb.license 16 pipes-filters.ipynb\n", "96 file-system.ipynb 8 pipes-filters.ipynb.license\n", " 8 file-system.ipynb.license 16 shell-variables.ipynb\n", "24 grep-find.ipynb 8 shell-variables.ipynb.license\n", " 8 grep-find.ipynb.license\n" ] } ], "source": [ "!ls -s" ] }, { "cell_type": "code", "execution_count": 5, "id": "neutral-invite", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "file-system.ipynb create-delete.ipynb.license\n", "grep-find.ipynb file-system.ipynb.license\n", "create-delete.ipynb grep-find.ipynb.license\n", "pipes-filters.ipynb pipes-filters.ipynb.license\n", "shell-variables.ipynb shell-variables.ipynb.license\n", "index.rst\n" ] } ], "source": [ "!ls -S" ] }, { "cell_type": "markdown", "id": "cognitive-award", "metadata": {}, "source": [ "### Show all options and arguments\n", "\n", "`ls` comes with a lot of other useful options. Using `man` you can print out the built-in manual page for the desired UNIX/Linux-command:" ] }, { "cell_type": "code", "execution_count": 6, "id": "artificial-aspect", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "LS(1) General Commands Manual LS(1)\n", "\n", "NAME\n", " ls – list directory contents\n", "\n", "SYNOPSIS\n", " ls [-@ABCFGHILOPRSTUWabcdefghiklmnopqrstuvwxy1%,] [--color=when]\n", " [-D format] [file ...]\n", "\n", "DESCRIPTION\n", " For each operand that names a file of a type other than directory, ls\n", " displays its name as well as any requested, associated information. For\n", " each operand that names a file of type directory, ls displays the names\n", " of files contained within that directory, as well as any requested,\n", " associated information.\n", "\n", " If no operands are given, the contents of the current directory are\n", " displayed. If more than one operand is given, non-directory operands are\n", " displayed first; directory and non-directory operands are sorted\n", " separately and in lexicographical order.\n", "\n", " …" ] } ], "source": [ "!man ls" ] }, { "cell_type": "markdown", "id": "manufactured-momentum", "metadata": {}, "source": [ "### Illegal options\n", "\n", "If you try to use an option that isn’t supported, the commands will usually print an error message, for example for:" ] }, { "cell_type": "code", "execution_count": 7, "id": "fuzzy-secondary", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ls: invalid option -- z\n", "usage: ls [-@ABCFGHILOPRSTUWXabcdefghiklmnopqrstuvwxy1%,] [--color=when] [-D format] [file ...]\n" ] } ], "source": [ "!ls -z" ] }, { "cell_type": "markdown", "id": "unavailable-giving", "metadata": {}, "source": [ "### Hidden Files\n", "\n", "With the `-a` option you can display all files:" ] }, { "cell_type": "code", "execution_count": 8, "id": "intellectual-sense", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[34m.\u001b[m\u001b[m grep-find.ipynb\n", "\u001b[34m..\u001b[m\u001b[m grep-find.ipynb.license\n", "\u001b[34m.ipynb_checkpoints\u001b[m\u001b[m index.rst\n", "create-delete.ipynb pipes-filters.ipynb\n", "create-delete.ipynb.license pipes-filters.ipynb.license\n", "file-system.ipynb shell-variables.ipynb\n", "file-system.ipynb.license shell-variables.ipynb.license\n" ] } ], "source": [ "!ls -a" ] }, { "cell_type": "markdown", "id": "cultural-ghana", "metadata": {}, "source": [ "In addition to the hidden directories `..` and `.`, you may also see a directory called `.ipynb_checkpoints`. This file usually contains snapshots of the Jupyter notebooks." ] }, { "cell_type": "markdown", "id": "latin-exclusive", "metadata": {}, "source": [ "## Show directory treeThe command `tree` lists contents of directories in a tree-like format." ] }, { "cell_type": "code", "execution_count": 9, "id": "middle-stomach", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[01;34m.\u001b[0m\n", "├── \u001b[00mcreate-delete.ipynb\u001b[0m\n", "├── \u001b[00mcreate-delete.ipynb.license\u001b[0m\n", "├── \u001b[00mfile-system.ipynb\u001b[0m\n", "├── \u001b[00mfile-system.ipynb.license\u001b[0m\n", "├── \u001b[00mgrep-find.ipynb\u001b[0m\n", "├── \u001b[00mgrep-find.ipynb.license\u001b[0m\n", "├── \u001b[00mindex.rst\u001b[0m\n", "├── \u001b[00mpipes-filters.ipynb\u001b[0m\n", "├── \u001b[00mpipes-filters.ipynb.license\u001b[0m\n", "├── \u001b[00mshell-variables.ipynb\u001b[0m\n", "└── \u001b[00mshell-variables.ipynb.license\u001b[0m\n", "\n", "1 directory, 11 files\n" ] } ], "source": [ "!tree" ] }, { "cell_type": "markdown", "id": "divided-leone", "metadata": {}, "source": [ "## Change directory\n", "\n", "At first it may seem irritating to some that they cannot use `!cd` to change to another directory." ] }, { "cell_type": "code", "execution_count": 10, "id": "moved-neutral", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/Users/veit/cusy/trn/Python4DataScience/docs/workspace/ipython/unix-shell\n" ] } ], "source": [ "!pwd" ] }, { "cell_type": "code", "execution_count": 11, "id": "crude-toronto", "metadata": {}, "outputs": [], "source": [ "!cd .." ] }, { "cell_type": "code", "execution_count": 12, "id": "detailed-canyon", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/Users/veit/cusy/trn/Python4DataScience/docs/workspace/ipython/unix-shell\n" ] } ], "source": [ "!pwd" ] }, { "cell_type": "markdown", "id": "textile-eugene", "metadata": {}, "source": [ "The reason for this is that Jupyter uses a temporary subshell. If you want to change to another directory permanently, you have to use the [magic command](../magics.ipynb) `%cd`. However, this also requires the Python package `pickleshare`." ] }, { "cell_type": "code", "execution_count": 13, "id": "liable-afghanistan", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/Users/veit/cusy/trn/Python4DataScience/docs/workspace/ipython\n" ] } ], "source": [ "%cd .." ] }, { "cell_type": "code", "execution_count": 14, "id": "written-camping", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/Users/veit/cusy/trn/Python4DataScience/docs/workspace/ipython\n" ] } ], "source": [ "!pwd" ] }, { "cell_type": "markdown", "id": "divine-garbage", "metadata": {}, "source": [ "With the `%automagic` function, these can also be used without the preceding `%` character:" ] }, { "cell_type": "code", "execution_count": 16, "id": "lonely-illness", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Automagic is ON, % prefix IS NOT needed for line magics.\n" ] } ], "source": [ "%automagic" ] }, { "cell_type": "code", "execution_count": 17, "id": "mobile-cologne", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/Users/veit/cusy/trn/Python4DataScience/docs/workspace\n" ] } ], "source": [ "cd .." ] }, { "cell_type": "markdown", "id": "ed4e7d06", "metadata": {}, "source": [ "### Absolute and relative Paths" ] }, { "cell_type": "code", "execution_count": 18, "id": "younger-pulse", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/Users/veit/cusy/trn/Python4DataScience/docs/workspace\n" ] } ], "source": [ "cd ." ] }, { "cell_type": "code", "execution_count": 19, "id": "black-thursday", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/Users/veit/cusy/trn/Python4DataScience\n" ] } ], "source": [ "cd ../.." ] }, { "cell_type": "code", "execution_count": 20, "id": "protective-plastic", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/Users/veit/cusy/trn\n" ] } ], "source": [ "cd .." ] }, { "cell_type": "code", "execution_count": 21, "id": "expired-enough", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/\n" ] } ], "source": [ "cd /" ] }, { "cell_type": "code", "execution_count": 22, "id": "tough-anchor", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/Users/veit\n" ] } ], "source": [ "cd" ] }, { "cell_type": "code", "execution_count": 23, "id": "positive-orchestra", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/Users/veit\n" ] } ], "source": [ "cd ~" ] }, { "cell_type": "code", "execution_count": 24, "id": "aggressive-hardware", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/Users/veit\n" ] } ], "source": [ "cd /Users/veit" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.11 Kernel", "language": "python", "name": "python311" }, "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.11.10" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autoclose": false, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 5 }