You use the ____ character in script files to mark comments.

In this episode we will learn about creating and moving files and directories, using the

haiku.txt  LittleWomen.txt  thesis/
0 directory as an example.

Step one: see where we are and what we already have

We should still be in the

haiku.txt  LittleWomen.txt  thesis/
1 directory on the Desktop, which we can check using:

$ pwd

/Users/nelle/Desktop/shell-lesson-data

Next we’ll move to the

haiku.txt  LittleWomen.txt  thesis/
0 directory and see what it contains:

$ cd exercise-data/writing/

$ ls -F

haiku.txt  LittleWomen.txt

Create a directory

Let’s create a new directory called

haiku.txt  LittleWomen.txt  thesis/
3 using the command
haiku.txt  LittleWomen.txt  thesis/
4 [which has no output]:

$ mkdir thesis

As you might guess from its name,

haiku.txt  LittleWomen.txt  thesis/
5 means ‘make directory’. Since
haiku.txt  LittleWomen.txt  thesis/
3 is a relative path [i.e., does not have a leading slash, like
haiku.txt  LittleWomen.txt  thesis/
7], the new directory is created in the current working directory:

$ ls -F

haiku.txt  LittleWomen.txt  thesis/

Since we’ve just created the

haiku.txt  LittleWomen.txt  thesis/
3 directory, there’s nothing in it yet:

$ ls -F thesis

Note that

haiku.txt  LittleWomen.txt  thesis/
5 is not limited to creating single directories one at a time. The
$ ls -F thesis
0 option allows
haiku.txt  LittleWomen.txt  thesis/
5 to create a directory with nested subdirectories in a single operation:

$ mkdir -p ../project/data ../project/results

The

$ ls -F thesis
2 option to the
$ ls -F thesis
3 command will list all nested subdirectories within a directory. Let’s use
$ ls -F thesis
4 to recursively list the new directory hierarchy we just created in the
$ ls -F thesis
5 directory:

/Users/nelle/Desktop/shell-lesson-data
0

/Users/nelle/Desktop/shell-lesson-data
1

Two ways of doing the same thing

Using the shell to create a directory is no different than using a file explorer. If you open the current directory using your operating system’s graphical file explorer, the

haiku.txt  LittleWomen.txt  thesis/
3 directory will appear there too. While the shell and the file explorer are two different ways of interacting with the files, the files and directories themselves are the same.

Good names for files and directories

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

  1. Don’t use spaces.

    Spaces can make a name more meaningful, but since spaces are used to separate arguments on the command line it is better to avoid them in names of files and directories. You can use

    $ ls -F thesis
    
    7 or
    $ ls -F thesis
    
    8 instead [e.g.
    $ ls -F thesis
    
    9 rather than
    $ mkdir -p ../project/data ../project/results
    
    0]. To test this out, try typing
    $ mkdir -p ../project/data ../project/results
    
    1and see what directory [or directories!] are made when you check with
    $ mkdir -p ../project/data ../project/results
    
    2.

  2. Don’t begin the name with

    $ ls -F thesis
    
    7 [dash].

    Commands treat names starting with

    $ ls -F thesis
    
    7 as options.

  3. Stick with letters, numbers,

    $ mkdir -p ../project/data ../project/results
    
    5 [period or ‘full stop’],
    $ ls -F thesis
    
    7 [dash] and
    $ ls -F thesis
    
    8 [underscore].

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

If you need to refer to names of files or directories that have spaces or other special characters, you should surround the name in quotes [

$ mkdir -p ../project/data ../project/results
8].

Create a text file

Let’s change our working directory to

haiku.txt  LittleWomen.txt  thesis/
3 using
/Users/nelle/Desktop/shell-lesson-data
00, then run a text editor called Nano to create a file called
/Users/nelle/Desktop/shell-lesson-data
01:

/Users/nelle/Desktop/shell-lesson-data
2

Which Editor?

When we say, ‘

/Users/nelle/Desktop/shell-lesson-data
02 is a text editor’ we really do mean ‘text’: it can only work with plain character data, not tables, images, or any other human-friendly media. We use it in examples because it is one of the least complex text editors. However, because of this trait, it may not be powerful enough or flexible enough for the work you need to do after this workshop. On Unix systems [such as Linux and macOS], many programmers use Emacs or Vim [both of which require more time to learn], or a graphical editor such as Gedit. On Windows, you may wish to use Notepad++. Windows also has a built-in editor called
/Users/nelle/Desktop/shell-lesson-data
03 that can be run from the command line in the same way as
/Users/nelle/Desktop/shell-lesson-data
02 for the purposes of this lesson.

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

Let’s type in a few lines of text. Once we’re happy with our text, we can press Ctrl+O [press the Ctrl or Control key and, while holding it down, press the O key] to write our data to disk [we’ll be asked what file we want to save this to: press Return to accept the suggested default of

/Users/nelle/Desktop/shell-lesson-data
01].

Once our file is saved, we can use Ctrl+X to quit the editor and return to the shell.

Control, Ctrl, or ^ Key

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

  • /Users/nelle/Desktop/shell-lesson-data
    
    06
  • /Users/nelle/Desktop/shell-lesson-data
    
    07
  • /Users/nelle/Desktop/shell-lesson-data
    
    08
  • /Users/nelle/Desktop/shell-lesson-data
    
    09
  • /Users/nelle/Desktop/shell-lesson-data
    
    10
  • /Users/nelle/Desktop/shell-lesson-data
    
    11

In nano, along the bottom of the screen you’ll see

/Users/nelle/Desktop/shell-lesson-data
12. This means that you can use
/Users/nelle/Desktop/shell-lesson-data
13 to get help and
/Users/nelle/Desktop/shell-lesson-data
14 to save your file.

/Users/nelle/Desktop/shell-lesson-data
02 doesn’t leave any output on the screen after it exits, but
$ ls -F thesis
3 now shows that we have created a file called
/Users/nelle/Desktop/shell-lesson-data
01:

/Users/nelle/Desktop/shell-lesson-data
3

/Users/nelle/Desktop/shell-lesson-data
4

Creating Files a Different Way

We have seen how to create text files using the

/Users/nelle/Desktop/shell-lesson-data
02 editor. Now, try the following command:

/Users/nelle/Desktop/shell-lesson-data
5

  1. What did the

    /Users/nelle/Desktop/shell-lesson-data
    
    19 command do? When you look at your current directory using the GUI file explorer, does the file show up?

  2. Use

    /Users/nelle/Desktop/shell-lesson-data
    
    20 to inspect the files. How large is
    /Users/nelle/Desktop/shell-lesson-data
    
    21?

  3. When might you want to create a file this way?

Solution

  1. The

    /Users/nelle/Desktop/shell-lesson-data
    
    19 command generates a new file called
    /Users/nelle/Desktop/shell-lesson-data
    
    21 in your current directory. You can observe this newly generated file by typing
    $ ls -F thesis
    
    3 at the command line prompt.
    /Users/nelle/Desktop/shell-lesson-data
    
    21 can also be viewed in your GUI file explorer.

  2. When you inspect the file with

    /Users/nelle/Desktop/shell-lesson-data
    
    20, note that the size of
    /Users/nelle/Desktop/shell-lesson-data
    
    21 is 0 bytes. In other words, it contains no data. If you open
    /Users/nelle/Desktop/shell-lesson-data
    
    21 using your text editor it is blank.

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

To avoid confusion later on, we suggest removing the file you’ve just created before proceding with the rest of the episode, otherwise future outputs may vary from those given in the lesson. To do this, use the following command:

/Users/nelle/Desktop/shell-lesson-data
6

What’s In A Name?

You may have noticed that all of Nelle’s files are named ‘something dot something’, and in this part of the lesson, we always used the extension

/Users/nelle/Desktop/shell-lesson-data
29. This is just a convention: we can call a file
/Users/nelle/Desktop/shell-lesson-data
30 or almost anything else we want. However, most people use two-part names most of the time to help them [and their programs] tell different kinds of files apart. The second part of such a name is called the filename extension and indicates what type of data the file holds:
/Users/nelle/Desktop/shell-lesson-data
29 signals a plain text file,
/Users/nelle/Desktop/shell-lesson-data
32 indicates a PDF document,
/Users/nelle/Desktop/shell-lesson-data
33 is a configuration file full of parameters for some program or other,
/Users/nelle/Desktop/shell-lesson-data
34 is a PNG image, and so on.

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

Naming a PNG image of a whale as

/Users/nelle/Desktop/shell-lesson-data
35 doesn’t somehow magically turn it into a recording of whale song, though it might cause the operating system to try to open it with a music player when someone double-clicks it.

Moving files and directories

Returning to the

/Users/nelle/Desktop/shell-lesson-data
36 directory,

/Users/nelle/Desktop/shell-lesson-data
7

In our

haiku.txt  LittleWomen.txt  thesis/
3 directory we have a file
/Users/nelle/Desktop/shell-lesson-data
01 which isn’t a particularly informative name, so let’s change the file’s name using
/Users/nelle/Desktop/shell-lesson-data
39, which is short for ‘move’:

/Users/nelle/Desktop/shell-lesson-data
8

The first argument tells

/Users/nelle/Desktop/shell-lesson-data
39 what we’re ‘moving’, while the second is where it’s to go. In this case, we’re moving
/Users/nelle/Desktop/shell-lesson-data
41 to
/Users/nelle/Desktop/shell-lesson-data
42, which has the same effect as renaming the file. Sure enough,
$ ls -F thesis
3 shows us that
haiku.txt  LittleWomen.txt  thesis/
3 now contains one file called
/Users/nelle/Desktop/shell-lesson-data
45:

/Users/nelle/Desktop/shell-lesson-data
9

$ cd exercise-data/writing/
0

One must be careful when specifying the target file name, since

/Users/nelle/Desktop/shell-lesson-data
39 will silently overwrite any existing file with the same name, which could lead to data loss. An additional option,
/Users/nelle/Desktop/shell-lesson-data
47 [or
/Users/nelle/Desktop/shell-lesson-data
48], can be used to make
/Users/nelle/Desktop/shell-lesson-data
39 ask you for confirmation before overwriting.

Note that

/Users/nelle/Desktop/shell-lesson-data
39 also works on directories.

Let’s move

/Users/nelle/Desktop/shell-lesson-data
45 into the current working directory. We use
/Users/nelle/Desktop/shell-lesson-data
39 once again, but this time we’ll use just the name of a directory as the second argument to tell
/Users/nelle/Desktop/shell-lesson-data
39 that we want to keep the filename but put the file somewhere new. [This is why the command is called ‘move’.] In this case, the directory name we use is the special directory name
$ mkdir -p ../project/data ../project/results
5 that we mentioned earlier.

$ cd exercise-data/writing/
1

The effect is to move the file from the directory it was in to the current working directory.

$ ls -F thesis
3 now shows us that
haiku.txt  LittleWomen.txt  thesis/
3 is empty:

/Users/nelle/Desktop/shell-lesson-data
9

$ cd exercise-data/writing/
3

Alternatively, we can confirm the file

/Users/nelle/Desktop/shell-lesson-data
45 is no longer present in the
haiku.txt  LittleWomen.txt  thesis/
3 directory by explicitly trying to list it:

$ cd exercise-data/writing/
4

$ cd exercise-data/writing/
5

$ ls -F thesis
3 with a filename or directory as an argument only lists the requested file or directory. If the file given as the argument doesn’t exist, the shell returns an error as we saw above. We can use this to see that
/Users/nelle/Desktop/shell-lesson-data
45 is now present in our current directory:

$ cd exercise-data/writing/
6

$ cd exercise-data/writing/
0

Moving Files to a new folder

After running the following commands, Jamie realizes that she put the files

/Users/nelle/Desktop/shell-lesson-data
61 and
/Users/nelle/Desktop/shell-lesson-data
62 into the wrong folder. The files should have been placed in the
/Users/nelle/Desktop/shell-lesson-data
63 folder.

$ cd exercise-data/writing/
8

Fill in the blanks to move these files to the

/Users/nelle/Desktop/shell-lesson-data
64 folder [i.e. the one she forgot to put them in]

$ cd exercise-data/writing/
9

Solution

$ ls -F
0

Recall that

/Users/nelle/Desktop/shell-lesson-data
65 refers to the parent directory [i.e. one above the current directory] and that
$ mkdir -p ../project/data ../project/results
5 refers to the current directory.

Copying files and directories

The

/Users/nelle/Desktop/shell-lesson-data
67 command works very much like
/Users/nelle/Desktop/shell-lesson-data
39, except it copies a file instead of moving it. We can check that it did the right thing using
$ ls -F thesis
3 with two paths as arguments — like most Unix commands,
$ ls -F thesis
3 can be given multiple paths at once:

$ ls -F
1

$ ls -F
2

We can also copy a directory and all its contents by using the recursive option

/Users/nelle/Desktop/shell-lesson-data
71, e.g. to back up a directory:

$ ls -F
3

We can check the result by listing the contents of both the

haiku.txt  LittleWomen.txt  thesis/
3 and
/Users/nelle/Desktop/shell-lesson-data
73 directory:

$ ls -F
4

$ ls -F
5

Renaming Files

Suppose that you created a plain-text file in your current directory to contain a list of the statistical tests you will need to do to analyze your data, and named it:

/Users/nelle/Desktop/shell-lesson-data
74

After creating and saving this file you realize you misspelled the filename! You want to correct the mistake, which of the following commands could you use to do so?

  1. /Users/nelle/Desktop/shell-lesson-data
    
    75
  2. /Users/nelle/Desktop/shell-lesson-data
    
    76
  3. /Users/nelle/Desktop/shell-lesson-data
    
    77
  4. /Users/nelle/Desktop/shell-lesson-data
    
    78

Solution

  1. No. While this would create a file with the correct name, the incorrectly named file still exists in the directory and would need to be deleted.
  2. Yes, this would work to rename the file.
  3. No, the period[.] indicates where to move the file, but does not provide a new file name; identical file names cannot be created.
  4. No, the period[.] indicates where to copy the file, but does not provide a new file name; identical file names cannot be created.

Moving and Copying

What is the output of the closing

$ ls -F thesis
3 command in the sequence shown below?

$ pwd

$ ls -F
7

/Users/nelle/Desktop/shell-lesson-data
3

$ ls -F
9

haiku.txt  LittleWomen.txt
0

  1. /Users/nelle/Desktop/shell-lesson-data
    
    80
  2. /Users/nelle/Desktop/shell-lesson-data
    
    81
  3. /Users/nelle/Desktop/shell-lesson-data
    
    82
  4. /Users/nelle/Desktop/shell-lesson-data
    
    83

Solution

We start in the

/Users/nelle/Desktop/shell-lesson-data
84 directory, and create a new folder called
/Users/nelle/Desktop/shell-lesson-data
81. The second line moves [
/Users/nelle/Desktop/shell-lesson-data
39] the file
/Users/nelle/Desktop/shell-lesson-data
87 to the new folder [
/Users/nelle/Desktop/shell-lesson-data
81]. The third line makes a copy of the file we just moved. The tricky part here is where the file was copied to. Recall that
/Users/nelle/Desktop/shell-lesson-data
65 means ‘go up a level’, so the copied file is now in
/Users/nelle/Desktop/shell-lesson-data
90. Notice that
/Users/nelle/Desktop/shell-lesson-data
65 is interpreted with respect to the current working directory, not with respect to the location of the file being copied. So, the only thing that will show using ls [in
/Users/nelle/Desktop/shell-lesson-data
84] is the recombined folder.

  1. No, see explanation above.
    /Users/nelle/Desktop/shell-lesson-data
    
    83 is located at
    /Users/nelle/Desktop/shell-lesson-data
    
    90
  2. Yes
  3. No, see explanation above.
    /Users/nelle/Desktop/shell-lesson-data
    
    87 is located at
    /Users/nelle/Desktop/shell-lesson-data
    
    96
  4. No, see explanation above.
    /Users/nelle/Desktop/shell-lesson-data
    
    83 is located at
    /Users/nelle/Desktop/shell-lesson-data
    
    90

Removing files and directories

Returning to the

/Users/nelle/Desktop/shell-lesson-data
36 directory, let’s tidy up this directory by removing the
/Users/nelle/Desktop/shell-lesson-data
45 file we created. The Unix command we’ll use for this is
$ cd exercise-data/writing/
01 [short for ‘remove’]:

haiku.txt  LittleWomen.txt
1

We can confirm the file has gone using

$ ls -F thesis
3:

$ cd exercise-data/writing/
6

haiku.txt  LittleWomen.txt
3

Deleting Is Forever

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

Using
$ cd exercise-data/writing/
01 Safely

What happens when we execute

$ cd exercise-data/writing/
04? Why would we want this protection when using
$ cd exercise-data/writing/
01?

Solution

haiku.txt  LittleWomen.txt
4

The

$ cd exercise-data/writing/
06 option will prompt before [every] removal [use Y to confirm deletion or N to keep the file]. The Unix shell doesn’t have a trash bin, so all the files removed will disappear forever. By using the
$ cd exercise-data/writing/
06 option, we have the chance to check that we are deleting only the files that we want to remove.

If we try to remove the

haiku.txt  LittleWomen.txt  thesis/
3 directory using
$ cd exercise-data/writing/
09, we get an error message:

haiku.txt  LittleWomen.txt
5

haiku.txt  LittleWomen.txt
6

This happens because

$ cd exercise-data/writing/
01 by default only works on files, not directories.

$ cd exercise-data/writing/
01 can remove a directory and all its contents if we use the recursive option
/Users/nelle/Desktop/shell-lesson-data
71, and it will do so without any confirmation prompts:

haiku.txt  LittleWomen.txt
7

Given that there is no way to retrieve files deleted using the shell,

$ cd exercise-data/writing/
13 should be used with great caution [you might consider adding the interactive option
$ cd exercise-data/writing/
14].

Operations with multiple files and directories

Oftentimes one needs to copy or move several files at once. This can be done by providing a list of individual filenames, or specifying a naming pattern using wildcards.

Copy with Multiple Filenames

For this exercise, you can test the commands in the

$ cd exercise-data/writing/
15 directory.

In the example below, what does

/Users/nelle/Desktop/shell-lesson-data
67 do when given several filenames and a directory name?

haiku.txt  LittleWomen.txt
8

In the example below, what does

/Users/nelle/Desktop/shell-lesson-data
67 do when given three or more file names?

haiku.txt  LittleWomen.txt
9

$ mkdir thesis
0

$ mkdir thesis
1

Solution

If given more than one file name followed by a directory name [i.e. the destination directory must be the last argument],

/Users/nelle/Desktop/shell-lesson-data
67 copies the files to the named directory.

If given three file names,

/Users/nelle/Desktop/shell-lesson-data
67 throws an error such as the one below, because it is expecting a directory name as the last argument.

$ mkdir thesis
2

Using wildcards for accessing multiple files at once

Wildcards

$ cd exercise-data/writing/
20 is a wildcard, which matches zero or more characters. Let’s consider the
$ cd exercise-data/writing/
21 directory:
$ cd exercise-data/writing/
22 matches
$ cd exercise-data/writing/
23,
$ cd exercise-data/writing/
24, and every file that ends with ‘.pdb’. On the other hand,
$ cd exercise-data/writing/
25 only matches
$ cd exercise-data/writing/
26 and
$ cd exercise-data/writing/
24, because the ‘p’ at the front only matches filenames that begin with the letter ‘p’.

$ cd exercise-data/writing/
28 is also a wildcard, but it matches exactly one character. So
$ cd exercise-data/writing/
29 would match
$ cd exercise-data/writing/
30 whereas
$ cd exercise-data/writing/
31 matches both
$ cd exercise-data/writing/
23, and
$ cd exercise-data/writing/
30.

Wildcards can be used in combination with each other e.g.

$ cd exercise-data/writing/
34 matches three characters followed by
$ cd exercise-data/writing/
35, giving
$ cd exercise-data/writing/
36.

When the shell sees a wildcard, it expands the wildcard to create a list of matching filenames before running the command that was asked for. As an exception, if a wildcard expression does not match any file, Bash will pass the expression as an argument to the command as it is. For example, typing

$ cd exercise-data/writing/
37 in the
$ cd exercise-data/writing/
38 directory [which contains only files with names ending with
$ cd exercise-data/writing/
39] results in an error message that there is no file called
$ cd exercise-data/writing/
40. However, generally commands like
$ cd exercise-data/writing/
41 and
$ ls -F thesis
3 see the lists of file names matching these expressions, but not the wildcards themselves. It is the shell, not the other programs, that deals with expanding wildcards.

List filenames matching a pattern

When run in the

$ cd exercise-data/writing/
38 directory, which
$ ls -F thesis
3 command[s] will produce this output?

$ cd exercise-data/writing/
45

  1. $ cd exercise-data/writing/
    
    46
  2. $ cd exercise-data/writing/
    
    47
  3. $ cd exercise-data/writing/
    
    48
  4. $ cd exercise-data/writing/
    
    49

Solution

The solution is

$ cd exercise-data/writing/
50

$ cd exercise-data/writing/
51 shows all files whose names contain zero or more characters [
$ cd exercise-data/writing/
20] followed by the letter
$ cd exercise-data/writing/
53, then zero or more characters [
$ cd exercise-data/writing/
20] followed by
$ cd exercise-data/writing/
35. This gives
$ cd exercise-data/writing/
56.

$ cd exercise-data/writing/
57 shows all files whose names start with zero or more characters [
$ cd exercise-data/writing/
20] followed by the letter
$ cd exercise-data/writing/
53, then a single character [
$ cd exercise-data/writing/
28], then
$ cd exercise-data/writing/
61 followed by zero or more characters [
$ cd exercise-data/writing/
20]. This will give us
$ cd exercise-data/writing/
63 and
$ cd exercise-data/writing/
26 but doesn’t match anything which ends in
$ cd exercise-data/writing/
65.

$ cd exercise-data/writing/
50 fixes the problems of option 2 by matching two characters [
$ cd exercise-data/writing/
67] between
$ cd exercise-data/writing/
53 and
$ cd exercise-data/writing/
69. This is the solution.

$ cd exercise-data/writing/
70 only shows files starting with
$ cd exercise-data/writing/
71.

More on Wildcards

Sam has a directory containing calibration data, datasets, and descriptions of the datasets:

$ mkdir thesis
3

Before heading off to another field trip, she wants to back up her data and send some datasets to her colleague Bob. Sam uses the following commands to get the job done:

$ mkdir thesis
4

Help Sam by filling in the blanks.

The resulting directory structure should look like this

$ mkdir thesis
5

Solution

$ mkdir thesis
6

Organizing Directories and Files

Jamie is working on a project and she sees that her files aren’t very well organized:

$ ls -F

$ mkdir thesis
8

The

$ cd exercise-data/writing/
72 and
/Users/nelle/Desktop/shell-lesson-data
61 files contain output from her data analysis. What command[s] covered in this lesson does she need to run so that the commands below will produce the output shown?

$ ls -F

$ ls -F
0

$ ls -F
1

$ ls -F
2

Solution

$ ls -F
3

Jamie needs to move her files

$ cd exercise-data/writing/
72 and
/Users/nelle/Desktop/shell-lesson-data
61 to the
$ cd exercise-data/writing/
76 directory. The shell will expand *.dat to match all .dat files in the current directory. The
/Users/nelle/Desktop/shell-lesson-data
39 command then moves the list of .dat files to the ‘analyzed’ directory.

Reproduce a folder structure

You’re starting a new experiment and would like to duplicate the directory structure from your previous experiment so you can add new data.

Assume that the previous experiment is in a folder called

$ cd exercise-data/writing/
78, which contains a
$ cd exercise-data/writing/
79 folder that in turn contains folders named
/Users/nelle/Desktop/shell-lesson-data
63 and
$ cd exercise-data/writing/
81 that contain data files. The goal is to copy the folder structure of the
$ cd exercise-data/writing/
78 folder into a folder called
$ cd exercise-data/writing/
83 so that your final directory structure looks like this:

$ ls -F
4

Which of the following set of commands would achieve this objective? What would the other commands do?

$ ls -F
5

$ ls -F
6

$ ls -F
7

$ ls -F
8

$ ls -F
9

Solution

The first two sets of commands achieve this objective. The first set uses relative paths to create the top-level directory before the subdirectories.

The third set of commands will give an error because the default behavior of

haiku.txt  LittleWomen.txt  thesis/
5 won’t create a subdirectory of a non-existent directory: the intermediate level folders must be created first.

The fourth set of commands achieve this objective. Remember, the

$ ls -F thesis
0 option, followed by a path of one or more directories, will cause
haiku.txt  LittleWomen.txt  thesis/
5 to create any intermediate subdirectories as required.

The final set of commands generates the ‘raw’ and ‘processed’ directories at the same level as the ‘data’ directory.

Key Points

  • $ cd exercise-data/writing/
    
    87 copies a file.

  • $ cd exercise-data/writing/
    
    88 creates a new directory.

  • $ cd exercise-data/writing/
    
    89 moves [renames] a file or directory.

  • $ cd exercise-data/writing/
    
    90 removes [deletes] a file.

  • $ cd exercise-data/writing/
    
    20 matches zero or more characters in a filename, so
    $ cd exercise-data/writing/
    
    92 matches all files ending in
    /Users/nelle/Desktop/shell-lesson-data
    
    29.

  • $ cd exercise-data/writing/
    
    28 matches any single character in a filename, so
    $ cd exercise-data/writing/
    
    95 matches
    $ cd exercise-data/writing/
    
    96 but not
    $ cd exercise-data/writing/
    
    97.

  • Use of the Control key may be described in many ways, including

    /Users/nelle/Desktop/shell-lesson-data
    
    08,
    /Users/nelle/Desktop/shell-lesson-data
    
    06, and
    /Users/nelle/Desktop/shell-lesson-data
    
    10.

  • The shell does not have a trash bin: once something is deleted, it’s really gone.

  • Most files’ names are

    $ ls -F
    
    01. The extension isn’t required, and doesn’t guarantee anything, but is normally used to indicate the type of data in the file.

    Which of the following shells was the first UNIX command processor?

    The first Unix shell was the Thompson shell, sh, written by Ken Thompson at Bell Labs and distributed with Versions 1 through 6 of Unix, from 1971 to 1975.

    When using WC you Cannot specify all three options in the command line at the same time?

    When using wc, you cannot specify all three options [-l, -w and -c] in the command line at the same time. The pipe operator can connect several commands on the same command line.

Chủ Đề