{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Working with Files and Directories" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We now know how to explore files and directories,\n", "but how do we create them in the first place?\n", "Let's go back to our `data-shell` directory on the Desktop\n", "and use `ls -F` to see what it contains:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's create a new directory called `thesis` using the command `mkdir thesis` (which has no output):" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As you might guess from its name, `mkdir` means \"make directory\". Since `thesis`\n", "is a relative path (i.e., doesn't have a leading slash), the new directory is\n", "created in the current working directory:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "
\n", "

Two ways of doing the same thing

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

Using the shell to create a directory is no different than using a file explorer.\n", "If you open the current directory using your operating system's graphical file explorer,\n", "the thesis directory will appear there too.\n", "While they are two different ways of interacting with the files,\n", "the files and directories themselves are the same.

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

Good names for files and directories

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

Complicated names of files and directories can make your life painful\n", "when working on the command line. Here we provide a few useful\n", "tips for the names of your files.

\n", "
    \n", "
  1. Don't use whitespaces.
  2. \n", "
\n", "

Whitespaces can make a name more meaningful\n", " but since whitespace is used to break arguments on the command line\n", " it is better to avoid them in names of files and directories.\n", " You can use - or _ instead of whitespace.

\n", "
    \n", "
  1. Don't begin the name with - (dash).
  2. \n", "
\n", "

Commands treat names starting with - as options.

\n", "
    \n", "
  1. Stick with letters, numbers, . (period or 'full stop'), - (dash) and _ (underscore).
  2. \n", "
\n", "

Many other characters have special meanings on the command line.\n", " We will learn about some of these during this lesson.\n", " There are special characters that can cause your command to not work as\n", " expected and can even result in data loss.

\n", "

If you need to refer to names of files or directories that have whitespace\n", "or another non-alphanumeric character, you should surround the name in quotes (\"\").

\n", "\n", "
\n", "\n", "
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Since we've just created the `thesis` directory, there's nothing in it yet:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's change our working directory to `thesis` using `cd`,\n", "then run a text editor called Nano to create a file called `draft.txt`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "
\n", "

Which Editor?

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

When we say, \"nano is a text editor,\" we really do mean \"text\": it can\n", "only work with plain character data, not tables, images, or any other\n", "human-friendly media. We use it in examples because it is one of the \n", "least complex text editors. However, because of this trait, it may \n", "not be powerful enough or flexible enough for the work you need to do\n", "after this workshop. On Unix systems (such as Linux and Mac OS X),\n", "many programmers use Emacs or\n", "Vim (both of which require more time to learn), \n", "or a graphical editor such as\n", "Gedit. On Windows, you may wish to\n", "use Notepad++. Windows also has a built-in\n", "editor called notepad that can be run from the command line in the same\n", "way as nano for the purposes of this lesson.

\n", "

No matter what editor you use, you will need to know where it searches\n", "for and saves files. If you start it from the shell, it will (probably)\n", "use your current working directory as its default location. If you use\n", "your computer's start menu, it may want to save files in your desktop or\n", "documents directory instead. You can change this by navigating to\n", "another directory the first time you \"Save As...\"

\n", "\n", "
\n", "\n", "
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's type in a few lines of text.\n", "Once we're happy with our text, we can press `Ctrl-O` (press the Ctrl or Control key and, while\n", "holding it down, press the O key) to write our data to disk\n", "(we'll be asked what file we want to save this to:\n", "press Return to accept the suggested default of `draft.txt`).\n", "![Nano in Action](nano-screenshot.png)\n", "\n", "Once our file is saved, we can use `Ctrl-X` to quit the editor and\n", "return to the shell.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "
\n", "

Control, Ctrl, or ^ Key

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

The Control key is also called the \"Ctrl\" key. There are various ways\n", "in which using the Control key may be described. For example, you may\n", "see an instruction to press the Control key and, while holding it down,\n", "press the X key, described as any of:

\n", "\n", "

In nano, along the bottom of the screen you'll see ^G Get Help ^O WriteOut.\n", "This means that you can use Control-G to get help and Control-O to save your\n", "file.

\n", "\n", "
\n", "\n", "
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`nano` doesn't leave any output on the screen after it exits,\n", "but `ls` now shows that we have created a file called `draft.txt`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "
\n", "

Creating Files a Different Way

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

We have seen how to create text files using the nano editor.\n", "Now, try the following command in your home directory:

\n", "
$ cd                  # go to your home directory\n",
    "$ touch my_file.txt\n",
    "
\n", "\n", "\n", "
    \n", "
  1. \n", "

    What did the touch command do?\n", " When you look at your home directory using the GUI file explorer,\n", " does the file show up?

    \n", "
  2. \n", "
  3. \n", "

    Use ls -l to inspect the files. How large is my_file.txt?

    \n", "
  4. \n", "
  5. \n", "

    When might you want to create a file this way?

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

Solution

\n", "
\n", "\n", "\n", "
\n", "\n", "
    \n", "
  1. The touch command generates a new file called 'my_file.txt' in\n", " your home directory. If you are in your home directory, you\n", " can observe this newly generated file by typing 'ls' at the \n", " command line prompt. 'my_file.txt' can also be viewed in your\n", " GUI file explorer.
  2. \n", "
  3. \n", "

    When you inspect the file with 'ls -l', note that the size of\n", " 'my_file.txt' is 0kb. In other words, it contains no data.\n", " If you open 'my_file.txt' using your text editor it is blank.

    \n", "
  4. \n", "
  5. \n", "

    Some programs do not generate output files themselves, but\n", " instead require that empty files have already been generated.\n", " When the program is run, it searches for an existing file to\n", " populate with its output. The touch command allows you to\n", " efficiently generate a blank text file to be used by such\n", " programs.

    \n", "
  6. \n", "
\n", "\n", "
\n", "\n", "
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Returning to the `data-shell` directory,\n", "let's tidy up the `thesis` directory by removing the draft we created:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This command removes files (`rm` is short for \"remove\"). If we run `ls` again,\n", "its output is empty once more, which tells us that our file is gone:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "
\n", "

Deleting Is Forever

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

The Unix shell doesn't have a trash bin that we can recover deleted\n", "files from (though most graphical interfaces to Unix do). Instead,\n", "when we delete files, they are unhooked from the file system so that\n", "their storage space on disk can be recycled. Tools for finding and\n", "recovering deleted files do exist, but there's no guarantee they'll\n", "work in any particular situation, since the computer may recycle the\n", "file's disk space right away.

\n", "\n", "
\n", "\n", "
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's re-create that file\n", "and then move up one directory to `/Users/nelle/Desktop/data-shell` using `cd ..`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we try to remove the entire `thesis` directory using `rm thesis`,\n", "we get an error message:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This happens because `rm` by default only works on files, not directories.\n", "\n", "To really get rid of `thesis` we must also delete the file `draft.txt`.\n", "We can do this with the [recursive](https://en.wikipedia.org/wiki/Recursion) option for `rm`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "
\n", "

Using `rm` Safely

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

What happens when we type rm -i thesis/quotations.txt?\n", "Why would we want this protection when using rm?

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

Solution

\n", "
\n", "\n", "\n", "
\n", "\n", "
$ rm: remove regular file 'thesis/quotations.txt'?\n",
    "
\n", "\n", "\n", "

The -i option will prompt before every removal. \n", "The Unix shell doesn't have a trash bin, so all the files removed will disappear forever. \n", "By using the -i flag, we have the chance to check that we are deleting only the files that we want to remove.

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

With Great Power Comes Great Responsibility

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

Removing the files in a directory recursively can be a very dangerous\n", "operation. If we're concerned about what we might be deleting we can\n", "add the \"interactive\" flag -i to rm which will ask us for confirmation\n", "before each step

\n", "
$ rm -r -i thesis\n",
    "rm: descend into directory ‘thesis’? y\n",
    "rm: remove regular file ‘thesis/draft.txt’? y\n",
    "rm: remove directory ‘thesis’? y\n",
    "
\n", "\n", "\n", "

This removes everything in the directory, then the directory itself, asking\n", "at each step for you to confirm the deletion.

\n", "\n", "
\n", "\n", "
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's create that directory and file one more time.\n", "(Note that this time we're running `nano` with the path `thesis/draft.txt`,\n", "rather than going into the `thesis` directory and running `nano` on `draft.txt` there.)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`draft.txt` isn't a particularly informative name,\n", "so let's change the file's name using `mv`,\n", "which is short for \"move\":" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The first argument tells `mv` what we're \"moving\",\n", "while the second is where it's to go.\n", "In this case,\n", "we're moving `thesis/draft.txt` to `thesis/quotes.txt`,\n", "which has the same effect as renaming the file.\n", "Sure enough,\n", "`ls` shows us that `thesis` now contains one file called `quotes.txt`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The effect is to move the file from the directory it was in to the current working directory.\n", "`ls` now shows us that `thesis` is empty:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One has to be careful when specifying the target file name, since `mv` will\n", "silently overwrite any existing file with the same name, which could\n", "lead to data loss. An additional flag, `mv -i` (or `mv --interactive`),\n", "can be used to make `mv` ask you for confirmation before overwriting.\n", "\n", "Just for the sake of consistency,\n", "`mv` also works on directories\n", "\n", "Let's move `quotes.txt` into the current working directory.\n", "We use `mv` once again,\n", "but this time we'll just use the name of a directory as the second argument\n", "to tell `mv` that we want to keep the filename,\n", "but put the file somewhere new.\n", "(This is why the command is called \"move\".)\n", "In this case,\n", "the directory name we use is the special directory name `.` that we mentioned earlier." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Further,\n", "`ls` with a filename or directory name as an argument only lists that file or directory.\n", "We can use this to see that `quotes.txt` is still in our current directory:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "
\n", "

Moving to the Current Folder

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

After running the following commands,\n", "Jamie realizes that she put the files sucrose.dat and maltose.dat into the wrong folder:

\n", "
$ ls -F\n",
    " analyzed/ raw/\n",
    "$ ls -F analyzed\n",
    "fructose.dat glucose.dat maltose.dat sucrose.dat\n",
    "$ cd raw/\n",
    "
\n", "\n", "\n", "

Fill in the blanks to move these files to the current folder\n", "(i.e., the one she is currently in):

\n", "
$ mv ___/sucrose.dat  ___/maltose.dat ___\n",
    "
\n", "\n", "
\n", "\n", "
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "
\n", "

Solution

\n", "
\n", "\n", "\n", "
\n", "\n", "
$ mv ../analyzed/sucrose.dat ../analyzed/maltose.dat .\n",
    "
\n", "\n", "\n", "

Recall that .. refers to the parent directory (i.e. one above the current directory)\n", "and that . refers to the current directory.

\n", "\n", "
\n", "\n", "
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `cp` command works very much like `mv`,\n", "except it copies a file instead of moving it.\n", "We can check that it did the right thing using `ls`\n", "with two paths as arguments --- like most Unix commands,\n", "`ls` can be given multiple paths at once:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To prove that we made a copy,\n", "let's delete the `quotes.txt` file in the current directory\n", "and then run that same `ls` again." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This time it tells us that it can't find `quotes.txt` in the current directory,\n", "but it does find the copy in `thesis` that we didn't delete." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "
\n", "

What's In A Name?

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

You may have noticed that all of Nelle's files' names are \"something dot\n", "something\", and in this part of the lesson, we always used the extension\n", ".txt. This is just a convention: we can call a file mythesis or\n", "almost anything else we want. However, most people use two-part names\n", "most of the time to help them (and their programs) tell different kinds\n", "of files apart. The second part of such a name is called the\n", "filename extension, and indicates\n", "what type of data the file holds: .txt signals a plain text file, .pdf\n", "indicates a PDF document, .cfg is a configuration file full of parameters\n", "for some program or other, .png is a PNG image, and so on.

\n", "

This is just a convention, albeit an important one. Files contain\n", "bytes: it's up to us and our programs to interpret those bytes\n", "according to the rules for plain text files, PDF documents, configuration\n", "files, images, and so on.

\n", "

Naming a PNG image of a whale as whale.mp3 doesn't somehow\n", "magically turn it into a recording of whalesong, though it might\n", "cause the operating system to try to open it with a music player\n", "when someone double-clicks it.

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

Renaming Files

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

Suppose that you created a .txt file in your current directory to contain a list of the\n", "statistical tests you will need to do to analyze your data, and named it: statstics.txt\n", "After creating and saving this file you realize you misspelled the filename! You want to\n", "correct the mistake, which of the following commands could you use to do so?

\n", "
    \n", "
  1. cp statstics.txt statistics.txt
  2. \n", "
  3. mv statstics.txt statistics.txt
  4. \n", "
  5. mv statstics.txt .
  6. \n", "
  7. cp statstics.txt .
  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. While this would create a file with the correct name, the incorrectly named file still exists in the directory\n", "and would need to be deleted.
  2. \n", "
  3. Yes, this would work to rename the file.
  4. \n", "
  5. No, the period(.) indicates where to move the file, but does not provide a new file name; identical file names\n", "cannot be created.
  6. \n", "
  7. No, the period(.) indicates where to copy the file, but does not provide a new file name; identical file names\n", "cannot be created.
  8. \n", "
\n", "\n", "
\n", "\n", "
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "
\n", "

Moving and Copying

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

What is the output of the closing ls command in the sequence shown below?

\n", "
$ pwd\n",
    "
\n", "\n", "\n", "
/Users/jamie/data\n",
    "
\n", "\n", "\n", "
$ ls\n",
    "
\n", "\n", "\n", "
proteins.dat\n",
    "
\n", "\n", "\n", "
$ mkdir recombine\n",
    "$ mv proteins.dat recombine/\n",
    "$ cp recombine/proteins.dat ../proteins-saved.dat\n",
    "$ ls\n",
    "
\n", "\n", "\n", "
    \n", "
  1. proteins-saved.dat recombine
  2. \n", "
  3. recombine
  4. \n", "
  5. proteins.dat recombine
  6. \n", "
  7. proteins-saved.dat
  8. \n", "
\n", "\n", "
\n", "\n", "
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "
\n", "

Solution

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

We start in the /Users/jamie/data directory, and create a new folder called recombine.\n", "The second line moves (mv) the file proteins.dat to the new folder (recombine).\n", "The third line makes a copy of the file we just moved. The tricky part here is where the file was\n", "copied to. Recall that .. means \"go up a level\", so the copied file is now in /Users/jamie.\n", "Notice that .. is interpreted with respect to the current working\n", "directory, not with respect to the location of the file being copied.\n", "So, the only thing that will show using ls (in /Users/jamie/data) is the recombine folder.

\n", "
    \n", "
  1. No, see explanation above. proteins-saved.dat is located at /Users/jamie
  2. \n", "
  3. Yes
  4. \n", "
  5. No, see explanation above. proteins.dat is located at /Users/jamie/data/recombine
  6. \n", "
  7. No, see explanation above. proteins-saved.dat is located at /Users/jamie
  8. \n", "
\n", "\n", "
\n", "\n", "
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "
\n", "

Organizing Directories and Files

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

Jamie is working on a project and she sees that her files aren't very well\n", "organized:

\n", "
$ ls -F\n",
    "
\n", "\n", "\n", "
analyzed/  fructose.dat    raw/   sucrose.dat\n",
    "
\n", "\n", "\n", "

The fructose.dat and sucrose.dat files contain output from her data\n", "analysis. What command(s) covered in this lesson does she need to run so that the commands below will\n", "produce the output shown?

\n", "
$ ls -F\n",
    "
\n", "\n", "\n", "
analyzed/   raw/\n",
    "
\n", "\n", "\n", "
$ ls analyzed\n",
    "
\n", "\n", "\n", "
fructose.dat    sucrose.dat\n",
    "
\n", "\n", "
\n", "\n", "
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "
\n", "

Solution

\n", "
\n", "\n", "\n", "
\n", "\n", "
mv *.dat analyzed\n",
    "
\n", "\n", "\n", "

Jamie needs to move her files fructose.dat and sucrose.dat to the analyzed directory.\n", "The shell will expand *.dat to match all .dat files in the current directory.\n", "The mv command then moves the list of .dat files to the \"analyzed\" directory.

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

Copy with Multiple Filenames

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

For this exercise, you can test the commands in the data-shell/data directory.

\n", "

In the example below, what does cp do when given several filenames and a directory name?

\n", "
$ mkdir backup\n",
    "$ cp amino-acids.txt animals.txt backup/\n",
    "
\n", "\n", "\n", "

In the example below, what does cp do when given three or more file names?

\n", "
$ ls -F\n",
    "
\n", "\n", "\n", "
amino-acids.txt  animals.txt  backup/  elements/  morse.txt  pdb/  planets.txt  salmon.txt  sunspot.txt\n",
    "
\n", "\n", "\n", "
$ cp amino-acids.txt animals.txt morse.txt \n",
    "
\n", "\n", "
\n", "\n", "
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "
\n", "

Solution

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

If given more than one file name followed by a directory name (i.e. the destination directory must \n", "be the last argument), cp copies the files to the named directory.

\n", "

If given three file names, cp throws an error because it is expecting a directory\n", "name as the last argument.

\n", "
cp: target morse.txt is not a directory\n",
    "
\n", "\n", "
\n", "\n", "
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "
\n", "

Copy a folder structure but not the files

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

You're starting a new experiment, and would like to duplicate the file\n", "structure from your previous experiment without the data files so you can\n", "add new data.

\n", "

Assume that the file structure is in a folder called '2016-05-18-data',\n", "which contains a data folder that in turn contains folders named raw and\n", "processed that contain data files. The goal is to copy the file structure\n", "of the 2016-05-18-data folder into a folder called 2016-05-20-data and\n", "remove the data files from the directory you just created.

\n", "

Which of the following set of commands would achieve this objective?\n", "What would the other commands do?

\n", "
$ cp -r 2016-05-18-data/ 2016-05-20-data/\n",
    "$ rm 2016-05-20-data/raw/*\n",
    "$ rm 2016-05-20-data/processed/*\n",
    "
\n", "\n", "\n", "
$ rm 2016-05-20-data/raw/*\n",
    "$ rm 2016-05-20-data/processed/*\n",
    "$ cp -r 2016-05-18-data/ 2016-5-20-data/\n",
    "
\n", "\n", "\n", "
$ cp -r 2016-05-18-data/ 2016-05-20-data/\n",
    "$ rm -r -i 2016-05-20-data/\n",
    "
\n", "\n", "
\n", "\n", "
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "
\n", "

Solution

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

The first set of commands achieves this objective.\n", "First we have a recursive copy of a data folder.\n", "Then two rm commands which remove all files in the specified directories.\n", "The shell expands the '*' wild card to match all files and subdirectories.

\n", "

The second set of commands have the wrong order: \n", "attempting to delete files which haven't yet been copied,\n", "followed by the recursive copy command which would copy them.

\n", "

The third set of commands would achieve the objective, but in a time-consuming way:\n", "the first command copies the directory recursively, but the second command deletes\n", "interactively, prompting for confirmation for each file and directory.

\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" }, "name": "03-working-with-files-and-directories.ipynb", "swc_attribution": true }, "nbformat": 4, "nbformat_minor": 2 }