{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Navigating Files and Directories" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The part of the operating system responsible for managing files and directories \n", "is called the **file system**.\n", "It organizes our data into files, which hold information, and directories (also called \"folders\"), which hold files or other directories.\n", "\n", "Several commands are frequently used to create, inspect, rename, and delete files and directories.\n", "To start exploring them, we'll go to our open shell window.\n", "\n", "First let's find out where we are by running a command called `pwd`\n", "(which stands for \"print working directory\"). Directories are like *places* - at any time\n", "while we are using the shell we are in exactly one place, called\n", "our **current working directory**. Commands mostly read and write files in the \n", "current working directory, i.e. \"here\", so knowing where you are before running\n", "a command is important. `pwd` shows you where you are:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here, the computer's response is `/Users/nelle`, which is Nelle's **home directory**." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "
\n", "

Home Directory Variation

\n", "
\n", "\n", "\n", "
\n", "\n", "

The home directory path will look different on different operating systems.\n", "On Linux it may look like /home/nelle, and on Windows it will be similar to C:\\Documents and Settings\\nelle or C:\\Users\\nelle.
\n", "(Note that it may look slightly different for different versions of Windows.) In future examples, we've used Mac output as the default - Linux and Windows output may differ slightly, but should be generally similar.

\n", "\n", "
\n", "\n", "
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To understand what a \"home directory\" is,\n", "let's have a look at how the file system as a whole is organized. For the\n", "sake of this example, we'll be\n", "illustrating the filesystem on our scientist Nelle's computer. After this\n", "illustration, you'll be learning commands to explore your own filesystem,\n", "which will be constructed in a similar way, but not be exactly identical. \n", "\n", "On Nelle's computer, the filesystem looks like this:\n", "\n", "![The File System](filesystem.svg)\n", "\n", "At the top is the **root directory**\n", "that holds everything else.\n", "We refer to it using a slash character, `/`, on its own;\n", "this is the leading slash in `/Users/nelle`.\n", "\n", "Inside that directory are several other directories:\n", "`bin` (which is where some built-in programs are stored),\n", "`data` (for miscellaneous data files),\n", "`Users` (where users' personal directories are located),\n", "`tmp` (for temporary files that don't need to be stored long-term),\n", "and so on. \n", "\n", "We know that our current working directory `/Users/nelle` is stored inside `/Users`\n", "because `/Users` is the first part of its name.\n", "Similarly,\n", "we know that `/Users` is stored inside the root directory `/`\n", "because its name begins with `/`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "
\n", "

Slashes

\n", "
\n", "\n", "\n", "
\n", "\n", "

Notice that there are two meanings for the / character.\n", "When it appears at the front of a file or directory name,\n", "it refers to the root directory. When it appears inside a name,\n", "it's just a separator.

\n", "\n", "
\n", "\n", "
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Underneath `/Users`,\n", "we find one directory for each user with an account on Nelle's machine,\n", "her colleagues the Mummy and Wolfman. \n", "\n", "![Home Directories](home-directories.svg)\n", "\n", "The Mummy's files are stored in `/Users/imhotep`,\n", "Wolfman's in `/Users/larry`,\n", "and Nelle's in `/Users/nelle`. Because Nelle is the user in our\n", "examples here, this is why we get `/Users/nelle` as our home directory. \n", "Typically, when you open a new command prompt you will be in\n", "your home directory to start. \n", "\n", "Now let's learn the command that will let us see the contents of our\n", "own filesystem. We can see what's in our home directory by running `ls`,\n", "which stands for \"listing\":" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(Again, your results may be slightly different depending on your operating\n", "system and how you have customized your filesystem.)\n", "\n", "`ls` prints the names of the files and directories in the current directory. \n", "We can make its output more comprehensible by using the **flag** `-F`\n", "(also known as a **switch** or an **option**) ,\n", "which tells `ls` to add a marker to file and directory names to indicate what\n", "they are. A trailing `/` indicates that this is a directory. Depending on your\n", "settings, it might also use colors to indicate whether each entry is a file or \n", "directory.\n", "You might recall that we used `ls -F` in an earlier example." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Getting help\n", "\n", "`ls` has lots of other **flags**. There are two common ways to find out how \n", "to use a command and what flags it accepts:\n", "\n", "1. We can pass a `--help` flag to the command, such as:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2. We can read its manual with `man`, such as:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Depending on your environment you might find that only one of these works (either `man` or `--help`).**\n", "We'll describe both ways below." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### The `--help` flag\n", "\n", "Many bash commands, and programs that people have written that can be\n", "run from within bash, support a `--help` flag to display more\n", "information on how to use the command or program." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "
\n", "

Unsupported command-line options

\n", "
\n", "\n", "\n", "
\n", "\n", "

If you try to use an option (flag) that is not supported, ls and other programs\n", "will usually print an error message similar to:

\n", "
$ ls -j\n",
    "
\n", "\n", "\n", "
ls: invalid option -- 'j'\n",
    "Try 'ls --help' for more information.\n",
    "
\n", "\n", "
\n", "\n", "
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "
\n", "

The `man` command

\n", "
\n", "\n", "\n", "
\n", "\n", "

The other way to learn about ls is to type

\n", "\n", "
\n", "\n", "
\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Your output should be a list of all the files and sub-directories on your\n", "Desktop, including the `data-shell` directory you downloaded at\n", "the [setup for this lesson](). Take a look at your Desktop to confirm that\n", "your output is accurate. \n", "\n", "As you may now see, using a bash shell is strongly dependent on the idea that\n", "your files are organized in a hierarchical file system.\n", "Organizing things hierarchically in this way helps us keep track of our work:\n", "it's possible to put hundreds of files in our home directory,\n", "just as it's possible to pile hundreds of printed papers on our desk,\n", "but it's a self-defeating strategy.\n", "\n", "Now that we know the `data-shell` directory is located on our Desktop, we\n", "can do two things. \n", "\n", "First, we can look at its contents, using the same strategy as before, passing\n", "a directory name to `ls`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Second, we can actually change our location to a different directory, so\n", "we are no longer located in\n", "our home directory. \n", "\n", "The command to change locations is `cd` followed by a\n", "directory name to change our working directory.\n", "`cd` stands for \"change directory\",\n", "which is a bit misleading:\n", "the command doesn't change the directory,\n", "it changes the shell's idea of what directory we are in.\n", "\n", "Let's say we want to move to the `data` directory we saw above. We can\n", "use the following series of commands to get there:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "These commands will move us from our home directory onto our Desktop, then into\n", "the `data-shell` directory, then into the `data` directory. `cd` doesn't print anything,\n", "but if we run `pwd` after it, we can see that we are now\n", "in `/Users/nelle/Desktop/data-shell/data`.\n", "If we run `ls` without arguments now,\n", "it lists the contents of `/Users/nelle/Desktop/data-shell/data`,\n", "because that's where we now are:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We now know how to go down the directory tree, but\n", "how do we go up? We might try the following:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "But we get an error! Why is this? \n", "\n", "With our methods so far,\n", "`cd` can only see sub-directories inside your current directory. There are\n", "different ways to see directories above your current location; we'll start\n", "with the simplest. \n", "\n", "There is a shortcut in the shell to move up one directory level\n", "that looks like this:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`..` is a special directory name meaning\n", "\"the directory containing this one\",\n", "or more succinctly,\n", "the **parent** of the current directory.\n", "Sure enough,\n", "if we run `pwd` after running `cd ..`, we're back in `/Users/nelle/Desktop/data-shell`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The special directory `..` doesn't usually show up when we run `ls`. If we want\n", "to display it, we can give `ls` the `-a` flag:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`-a` stands for \"show all\";\n", "it forces `ls` to show us file and directory names that begin with `.`,\n", "such as `..` (which, if we're in `/Users/nelle`, refers to the `/Users` directory)\n", "As you can see,\n", "it also displays another special directory that's just called `.`,\n", "which means \"the current working directory\".\n", "It may seem redundant to have a name for it,\n", "but we'll see some uses for it soon.\n", "\n", "Note that in most command line tools, multiple flags can be combined \n", "with a single `-` and no spaces between the flags: `ls -F -a` is \n", "equivalent to `ls -Fa`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "
\n", "

Other Hidden Files

\n", "
\n", "\n", "\n", "
\n", "\n", "

In addition to the hidden directories .. and ., you may also see a file\n", "called .bash_profile. This file usually contains shell configuration\n", "settings. You may also see other files and directories beginning\n", "with .. These are usually files and directories that are used to configure\n", "different programs on your computer. The prefix . is used to prevent these\n", "configuration files from cluttering the terminal when a standard ls command\n", "is used.

\n", "\n", "
\n", "\n", "
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "
\n", "

Orthogonality

\n", "
\n", "\n", "\n", "
\n", "\n", "

The special names . and .. don't belong to cd;\n", "they are interpreted the same way by every program.\n", "For example,\n", "if we are in /Users/nelle/data,\n", "the command ls .. will give us a listing of /Users/nelle.\n", "When the meanings of the parts are the same no matter how they're combined,\n", "programmers say they are orthogonal:\n", "Orthogonal systems tend to be easier for people to learn\n", "because there are fewer special cases and exceptions to keep track of.

\n", "\n", "
\n", "\n", "
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "These then, are the basic commands for navigating the filesystem on your computer:\n", "`pwd`, `ls` and `cd`. Let's explore some variations on those commands. What happens\n", "if you type `cd` on its own, without giving\n", "a directory?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "How can you check what happened? `pwd` gives us the answer!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It turns out that `cd` without an argument will return you to your home directory,\n", "which is great if you've gotten lost in your own filesystem. \n", "\n", "Let's try returning to the `data` directory from before. Last time, we used\n", "three commands, but we can actually string together the list of directories\n", "to move to `data` in one step:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Check that we've moved to the right place by running `pwd` and `ls -F` \n", "\n", "If we want to move up one level from the data directory, we could use `cd ..`. But\n", "there is another way to move to any directory, regardless of your\n", "current location. \n", "\n", "So far, when specifying directory names, or even a directory path (as above),\n", "we have been using **relative paths**. When you use a relative path with a command\n", "like `ls` or `cd`, it tries to find that location from where we are,\n", "rather than from the root of the file system. \n", "\n", "However, it is possible to specify the **absolute path** to a directory by\n", "including its entire path from the root directory, which is indicated by a\n", "leading slash. The leading `/` tells the computer to follow the path from\n", "the root of the file system, so it always refers to exactly one directory,\n", "no matter where we are when we run the command.\n", "\n", "This allows us to move to our `data-shell` directory from anywhere on\n", "the filesystem (including from inside `data`). To find the absolute path\n", "we're looking for, we can use `pwd` and then extract the piece we need\n", "to move to `data-shell`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Run `pwd` and `ls -F` to ensure that we're in the directory we expect. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "
\n", "

Two More Shortcuts

\n", "
\n", "\n", "\n", "
\n", "\n", "

The shell interprets the character ~ (tilde) at the start of a path to\n", "mean \"the current user's home directory\". For example, if Nelle's home\n", "directory is /Users/nelle, then ~/data is equivalent to\n", "/Users/nelle/data. This only works if it is the first character in the\n", "path: here/there/~/elsewhere is not here/there/Users/nelle/elsewhere.

\n", "

Another shortcut is the - (dash) character. cd will translate - into\n", "the previous directory I was in, which is faster than having to remember,\n", "then type, the full path. This is a very efficient way of moving back\n", "and forth between directories. The difference between cd .. and cd - is\n", "that the former brings you up, while the latter brings you back. You can\n", "think of it as the Last Channel button on a TV remote.

\n", "\n", "
\n", "\n", "
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "
\n", "

Absolute vs Relative Paths

\n", "
\n", "\n", "\n", "
\n", "\n", "

Starting from /Users/amanda/data/,\n", "which of the following commands could Amanda use to navigate to her home directory,\n", "which is /Users/amanda?

\n", "
    \n", "
  1. cd .
  2. \n", "
  3. cd /
  4. \n", "
  5. cd /home/amanda
  6. \n", "
  7. cd ../..
  8. \n", "
  9. cd ~
  10. \n", "
  11. cd home
  12. \n", "
  13. cd ~/data/..
  14. \n", "
  15. cd
  16. \n", "
  17. cd ..
  18. \n", "
\n", "\n", "
\n", "\n", "
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "
\n", "

Solution

\n", "
\n", "\n", "\n", "
\n", "\n", "
    \n", "
  1. No: . stands for the current directory.
  2. \n", "
  3. No: / stands for the root directory.
  4. \n", "
  5. No: Amanda's home directory is /Users/amanda.
  6. \n", "
  7. No: this goes up two levels, i.e. ends in /Users.
  8. \n", "
  9. Yes: ~ stands for the user's home directory, in this case /Users/amanda.
  10. \n", "
  11. No: this would navigate into a directory home in the current directory if it exists.
  12. \n", "
  13. Yes: unnecessarily complicated, but correct.
  14. \n", "
  15. Yes: shortcut to go back to the user's home directory.
  16. \n", "
  17. Yes: goes up one level.
  18. \n", "
\n", "\n", "
\n", "\n", "
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "
\n", "

Relative Path Resolution

\n", "
\n", "\n", "\n", "
\n", "\n", "

Using the filesystem diagram below, if pwd displays /Users/thing,\n", "what will ls -F ../backup display?

\n", "
    \n", "
  1. ../backup: No such file or directory
  2. \n", "
  3. 2012-12-01 2013-01-08 2013-01-27
  4. \n", "
  5. 2012-12-01/ 2013-01-08/ 2013-01-27/
  6. \n", "
  7. original/ pnas_final/ pnas_sub/
  8. \n", "
\n", "

\"File

\n", "\n", "
\n", "\n", "
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "
\n", "

Solution

\n", "
\n", "\n", "\n", "
\n", "\n", "
    \n", "
  1. No: there is a directory backup in /Users.
  2. \n", "
  3. No: this is the content of Users/thing/backup,\n", " but with .. we asked for one level further up.
  4. \n", "
  5. No: see previous explanation.
  6. \n", "
  7. Yes: ../backup/ refers to /Users/backup/.
  8. \n", "
\n", "\n", "
\n", "\n", "
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "
\n", "

`ls` Reading Comprehension

\n", "
\n", "\n", "\n", "
\n", "\n", "

Assuming a directory structure as in the above Figure\n", "(File System for Challenge Questions), if pwd displays /Users/backup,\n", "and -r tells ls to display things in reverse order,\n", "what command will display:

\n", "
pnas_sub/ pnas_final/ original/\n",
    "
\n", "\n", "\n", "
    \n", "
  1. ls pwd
  2. \n", "
  3. ls -r -F
  4. \n", "
  5. ls -r -F /Users/backup
  6. \n", "
  7. Either #2 or #3 above, but not #1.
  8. \n", "
\n", "\n", "
\n", "\n", "
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "
\n", "

Solution

\n", "
\n", "\n", "\n", "
\n", "\n", "
    \n", "
  1. No: pwd is not the name of a directory.
  2. \n", "
  3. Yes: ls without directory argument lists files and directories\n", " in the current directory.
  4. \n", "
  5. Yes: uses the absolute path explicitly.
  6. \n", "
  7. Correct: see explanations above.
  8. \n", "
\n", "\n", "
\n", "\n", "
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Nelle's Pipeline: Organizing Files\n", "\n", "Knowing just this much about files and directories,\n", "Nelle is ready to organize the files that the protein assay machine will create.\n", "First,\n", "she creates a directory called `north-pacific-gyre`\n", "(to remind herself where the data came from).\n", "Inside that,\n", "she creates a directory called `2012-07-03`,\n", "which is the date she started processing the samples.\n", "She used to use names like `conference-paper` and `revised-results`,\n", "but she found them hard to understand after a couple of years.\n", "(The final straw was when she found herself creating\n", "a directory called `revised-revised-results-3`.)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "
\n", "

Sorting Output

\n", "
\n", "\n", "\n", "
\n", "\n", "

Nelle names her directories \"year-month-day\",\n", "with leading zeroes for months and days,\n", "because the shell displays file and directory names in alphabetical order.\n", "If she used month names,\n", "December would come before July;\n", "if she didn't use leading zeroes,\n", "November ('11') would come before July ('7'). Similarly, putting the year first\n", "means that June 2012 will come before June 2013.

\n", "\n", "
\n", "\n", "
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Each of her physical samples is labelled according to her lab's convention\n", "with a unique ten-character ID,\n", "such as \"NENE01729A\".\n", "This is what she used in her collection log\n", "to record the location, time, depth, and other characteristics of the sample,\n", "so she decides to use it as part of each data file's name.\n", "Since the assay machine's output is plain text,\n", "she will call her files `NENE01729A.txt`, `NENE01812A.txt`, and so on.\n", "All 1520 files will go into the same directory.\n", "\n", "Now in her current directory `data-shell`,\n", "Nelle can see what files she has using the command:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is a lot to type,\n", "but she can let the shell do most of the work through what is called **tab completion**.\n", "If she types:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and then presses tab (the tab key on her keyboard),\n", "the shell automatically completes the directory name for her:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If she presses tab again,\n", "Bash will add `2012-07-03/` to the command,\n", "since it's the only possible completion.\n", "Pressing tab again does nothing,\n", "since there are 19 possibilities;\n", "pressing tab twice brings up a list of all the files,\n", "and so on.\n", "This is called **tab completion**,\n", "and we will see it in many other tools as we go on." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "
\n", "

\n", "
\n", "\n", "\n", "
\n", "\n", "

Keypoints:

\n", "\n", "\n", "
\n", "\n", "
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "The material in this notebook is derived from the Software Carpentry lessons\n", "© [Software Carpentry](http://software-carpentry.org/) under the terms\n", "of the [CC-BY 4.0](https://creativecommons.org/licenses/by/4.0/) license." ] } ], "metadata": { "kernelspec": { "display_name": "Bash", "language": "bash", "name": "bash" }, "language_info": { "codemirror_mode": "shell", "file_extension": ".sh", "mimetype": "text/x-sh", "name": "bash" }, "swc_attribution": true }, "nbformat": 4, "nbformat_minor": 2 }