-
Notifications
You must be signed in to change notification settings - Fork 0
/
02-create.html
268 lines (266 loc) · 20.9 KB
/
02-create.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="generator" content="pandoc">
<title>Software Carpentry: The Unix Shell</title>
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css" href="css/bootstrap/bootstrap.css" />
<link rel="stylesheet" type="text/css" href="css/bootstrap/bootstrap-theme.css" />
<link rel="stylesheet" type="text/css" href="css/swc.css" />
<link rel="alternate" type="application/rss+xml" title="Software Carpentry Blog" href="http://software-carpentry.org/feed.xml"/>
<meta charset="UTF-8" />
<!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body class="lesson">
<div class="container card">
<div class="banner">
<a href="http://software-carpentry.org" title="Software Carpentry">
<img alt="Software Carpentry banner" src="img/software-carpentry-banner.png" />
</a>
</div>
<article>
<div class="row">
<div class="col-md-10 col-md-offset-1">
<a href="index.html"><h1 class="title">The Unix Shell</h1></a>
<h2 class="subtitle">Working With Files and Directories</h2>
<section class="objectives panel panel-warning">
<div class="panel-heading">
<h2 id="learning-objectives"><span class="glyphicon glyphicon-certificate"></span>Learning Objectives</h2>
</div>
<div class="panel-body">
<ul>
<li>Create a directory hierarchy that matches a given diagram.</li>
<li>Create files in that hierarchy using an editor or by copying and renaming existing files.</li>
<li>Display the contents of a directory using the command line.</li>
<li>Delete specified files and/or directories.</li>
</ul>
</div>
</section>
<p>We now know how to explore files and directories, but how do we create them in the first place? Let’s go back to our <code>data-shell</code> directory on the Desktop and use <code>ls -F</code> to see what it contains:</p>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">pwd</span></code></pre></div>
<pre class="output"><code>/Users/nelle/Desktop/data-shell</code></pre>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">ls</span> -F</code></pre></div>
<pre class="output"><code>creatures/ molecules/ pizza.cfg
data/ north-pacific-gyre/ solar.pdf
Desktop/ notes.txt writing/</code></pre>
<p>Let’s create a new directory called <code>thesis</code> using the command <code>mkdir thesis</code> (which has no output):</p>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">mkdir</span> thesis</code></pre></div>
<p>As you might (or might not) guess from its name, <code>mkdir</code> means “make directory”. Since <code>thesis</code> is a relative path (i.e., doesn’t have a leading slash), the new directory is created in the current working directory:</p>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">ls</span> -F</code></pre></div>
<pre class="output"><code>creatures/ north-pacific-gyre/ thesis/
data/ notes.txt writing/
Desktop/ pizza.cfg
molecules/ solar.pdf</code></pre>
<p>However, there’s nothing in it yet:</p>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">ls</span> -F thesis</code></pre></div>
<p>Let’s change our working directory to <code>thesis</code> using <code>cd</code>, then run a text editor called Nano to create a file called <code>draft.txt</code>:</p>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">cd</span> thesis
$ <span class="kw">nano</span> draft.txt</code></pre></div>
<aside class="callout panel panel-info">
<div class="panel-heading">
<h2 id="which-editor"><span class="glyphicon glyphicon-pushpin"></span>Which Editor?</h2>
</div>
<div class="panel-body">
<p>When we say, “<code>nano</code> 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 almost anyone can drive it anywhere without training, but please use something more powerful for real work. On Unix systems (such as Linux and Mac OS X), many programmers use <a href="http://www.gnu.org/software/emacs/">Emacs</a> or <a href="http://www.vim.org/">Vim</a> (both of which are completely unintuitive, even by Unix standards), or a graphical editor such as <a href="http://projects.gnome.org/gedit/">Gedit</a>. On Windows, you may wish to use <a href="http://notepad-plus-plus.org/">Notepad++</a>. Windows also has a built-in editor called <code>notepad</code> that can be run from the command line in the same way as <code>nano</code> for the purposes of this lesson.</p>
<p>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…”</p>
</div>
</aside>
<p>Let’s type in a few lines of text. Once we’re happy with our text, we can press <code>Ctrl-O</code> (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 <code>draft.txt</code>).</p>
<div class="figure">
<img src="fig/nano-screenshot.png" alt="Nano in action" />
<p class="caption">Nano in action</p>
</div>
<p>Once our file is saved, we can use <code>Ctrl-X</code> to quit the editor and return to the shell.</p>
<aside class="callout panel panel-info">
<div class="panel-heading">
<h2 id="control-ctrl-or-key"><span class="glyphicon glyphicon-pushpin"></span>Control, ctrl, or ^ key</h2>
</div>
<div class="panel-body">
<p>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:</p>
<ul>
<li><code>Control-X</code></li>
<li><code>Control+X</code></li>
<li><code>Ctrl-X</code></li>
<li><code>Ctrl+X</code></li>
<li><code>^X</code></li>
</ul>
<p>In nano, along the bottom of the screen you’ll see <code>^G Get Help ^O WriteOut</code>. This means that you can use <code>Control-G</code> to get help and <code>Control-O</code> to save your file.</p>
</div>
</aside>
<p><code>nano</code> doesn’t leave any output on the screen after it exits, but <code>ls</code> now shows that we have created a file called <code>draft.txt</code>:</p>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">ls</span></code></pre></div>
<pre class="output"><code>draft.txt</code></pre>
<p>Let’s tidy up by running <code>rm draft.txt</code>:</p>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">rm</span> draft.txt</code></pre></div>
<p>This command removes files (<code>rm</code> is short for “remove”). If we run <code>ls</code> again, its output is empty once more, which tells us that our file is gone:</p>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">ls</span></code></pre></div>
<aside class="callout panel panel-info">
<div class="panel-heading">
<h2 id="deleting-is-forever"><span class="glyphicon glyphicon-pushpin"></span>Deleting Is Forever</h2>
</div>
<div class="panel-body">
<p>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 unhooked 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.</p>
</div>
</aside>
<p>Let’s re-create that file and then move up one directory to <code>/Users/nelle</code> using <code>cd ..</code>:</p>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">pwd</span></code></pre></div>
<pre class="output"><code>/Users/nelle/thesis</code></pre>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">nano</span> draft.txt
$ <span class="kw">ls</span></code></pre></div>
<pre class="output"><code>draft.txt</code></pre>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">cd</span> ..</code></pre></div>
<p>If we try to remove the entire <code>thesis</code> directory using <code>rm thesis</code>, we get an error message:</p>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">rm</span> thesis</code></pre></div>
<pre class="error"><code>rm: cannot remove `thesis': Is a directory</code></pre>
<p>This happens because <code>rm</code> by default only works on files, not directories.</p>
<p>To really get rid of <code>thesis</code> we must also delete the file <code>draft.txt</code>. We can do this with the <a href="https://en.wikipedia.org/wiki/Recursion">recursive</a> option for <code>rm</code>:</p>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">rm</span> -r thesis</code></pre></div>
<aside class="callout panel panel-info">
<div class="panel-heading">
<h2 id="with-great-power-comes-great-responsibility"><span class="glyphicon glyphicon-pushpin"></span>With Great Power Comes Great Responsibility</h2>
</div>
<div class="panel-body">
<p>Removing the files in a directory recursively can be very dangerous operation. If we’re concerned about what we might be deleting we can add the “interactive” flag <code>-i</code> to <code>rm</code> which will ask us for confirmation before each step</p>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">rm</span> -r -i thesis
<span class="kw">rm</span>: descend into directory ‘thesis’? y
<span class="kw">rm</span>: remove regular file ‘thesis/draft.txt’? y
<span class="kw">rm</span>: remove directory ‘thesis’? y</code></pre></div>
<p>This removes everything in the directory, then the directory itself, asking at each step for you to confirm the deletion.</p>
</div>
</aside>
<p>Let’s create that directory and file one more time. (Note that this time we’re running <code>nano</code> with the path <code>thesis/draft.txt</code>, rather than going into the <code>thesis</code> directory and running <code>nano</code> on <code>draft.txt</code> there.)</p>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">pwd</span></code></pre></div>
<pre class="output"><code>/Users/nelle</code></pre>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">mkdir</span> thesis</code></pre></div>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">nano</span> thesis/draft.txt
$ <span class="kw">ls</span> thesis</code></pre></div>
<pre class="output"><code>draft.txt</code></pre>
<p><code>draft.txt</code> isn’t a particularly informative name, so let’s change the file’s name using <code>mv</code>, which is short for “move”:</p>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">mv</span> thesis/draft.txt thesis/quotes.txt</code></pre></div>
<p>The first parameter tells <code>mv</code> what we’re “moving”, while the second is where it’s to go. In this case, we’re moving <code>thesis/draft.txt</code> to <code>thesis/quotes.txt</code>, which has the same effect as renaming the file. Sure enough, <code>ls</code> shows us that <code>thesis</code> now contains one file called <code>quotes.txt</code>:</p>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">ls</span> thesis</code></pre></div>
<pre class="output"><code>quotes.txt</code></pre>
<p>One has to be careful when specifying the target file name, since <code>mv</code> will silently overwrite any existing file with the same name, which could lead to data loss. An additional flag, <code>mv -i</code> (or <code>mv --interactive</code>), can be used to make <code>mv</code> ask you for confirmation before overwriting.</p>
<p>Just for the sake of inconsistency, <code>mv</code> also works on directories — there is no separate <code>mvdir</code> command.</p>
<p>Let’s move <code>quotes.txt</code> into the current working directory. We use <code>mv</code> once again, but this time we’ll just use the name of a directory as the second parameter to tell <code>mv</code> 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 <code>.</code> that we mentioned earlier.</p>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">mv</span> thesis/quotes.txt .</code></pre></div>
<p>The effect is to move the file from the directory it was in to the current working directory. <code>ls</code> now shows us that <code>thesis</code> is empty:</p>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">ls</span> thesis</code></pre></div>
<p>Further, <code>ls</code> with a filename or directory name as a parameter only lists that file or directory. We can use this to see that <code>quotes.txt</code> is still in our current directory:</p>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">ls</span> quotes.txt</code></pre></div>
<pre class="output"><code>quotes.txt</code></pre>
<p>The <code>cp</code> command works very much like <code>mv</code>, except it copies a file instead of moving it. We can check that it did the right thing using <code>ls</code> with two paths as parameters — like most Unix commands, <code>ls</code> can be given thousands of paths at once:</p>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">cp</span> quotes.txt thesis/quotations.txt
$ <span class="kw">ls</span> quotes.txt thesis/quotations.txt</code></pre></div>
<pre class="output"><code>quotes.txt thesis/quotations.txt</code></pre>
<p>To prove that we made a copy, let’s delete the <code>quotes.txt</code> file in the current directory and then run that same <code>ls</code> again.</p>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">rm</span> quotes.txt
$ <span class="kw">ls</span> quotes.txt thesis/quotations.txt</code></pre></div>
<pre class="error"><code>ls: cannot access quotes.txt: No such file or directory
thesis/quotations.txt</code></pre>
<p>This time it tells us that it can’t find <code>quotes.txt</code> in the current directory, but it does find the copy in <code>thesis</code> that we didn’t delete.</p>
<aside class="callout panel panel-info">
<div class="panel-heading">
<h2 id="whats-in-a-name"><span class="glyphicon glyphicon-pushpin"></span>What’s In A Name?</h2>
</div>
<div class="panel-body">
<p>You may have noticed that all of Nelle’s files’ names are “something dot something”, and in this part of the lesson, we always used the extension <code>.txt</code>. This is just a convention: we can call a file <code>mythesis</code> 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 <strong>filename extension</strong>, and indicates what type of data the file holds: <code>.txt</code> signals a plain text file, <code>.pdf</code> indicates a PDF document, <code>.cfg</code> is a configuration file full of parameters for some program or other, <code>.png</code> is a PNG image, and so on.</p>
<p>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.</p>
<p>Naming a PNG image of a whale as <code>whale.mp3</code> doesn’t somehow magically turn it into a recording of whalesong, though it <em>might</em> cause the operating system to try to open it with a music player when someone double-clicks it.</p>
</div>
</aside>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="renaming-files"><span class="glyphicon glyphicon-pencil"></span>Renaming files</h2>
</div>
<div class="panel-body">
<p>Suppose that you created a <code>.txt</code> 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: <code>statstics.txt</code></p>
<p>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?</p>
<ol style="list-style-type: decimal">
<li><code>cp statstics.txt statistics.txt</code></li>
<li><code>mv statstics.txt statistics.txt</code></li>
<li><code>mv statstics.txt .</code></li>
<li><code>cp statstics.txt .</code></li>
</ol>
</div>
</section>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="moving-and-copying"><span class="glyphicon glyphicon-pencil"></span>Moving and Copying</h2>
</div>
<div class="panel-body">
<p>What is the output of the closing <code>ls</code> command in the sequence shown below?</p>
<pre><code>$ pwd
/Users/jamie/data
$ ls
proteins.dat
$ mkdir recombine
$ mv proteins.dat recombine
$ cp recombine/proteins.dat ../proteins-saved.dat
$ ls</code></pre>
<ol style="list-style-type: decimal">
<li><code>proteins-saved.dat recombine</code></li>
<li><code>recombine</code></li>
<li><code>proteins.dat recombine</code></li>
<li><code>proteins-saved.dat</code></li>
</ol>
</div>
</section>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="organizing-directories-and-files"><span class="glyphicon glyphicon-pencil"></span>Organizing Directories and Files</h2>
</div>
<div class="panel-body">
<p>Jamie is working on a project and she sees that her files aren’t very well organized:</p>
<pre><code>$ ls -F
analyzed/ fructose.dat raw/ sucrose.dat</code></pre>
<p>The <code>fructose.dat</code> and <code>sucrose.dat</code> 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?</p>
<pre><code>$ ls -F
analyzed/ raw/
$ ls analyzed
fructose.dat sucrose.dat</code></pre>
</div>
</section>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="copy-with-multiple-filenames"><span class="glyphicon glyphicon-pencil"></span>Copy with Multiple Filenames</h2>
</div>
<div class="panel-body">
<p>What does <code>cp</code> do when given several filenames and a directory name, as in:</p>
<pre><code>$ mkdir backup
$ cp thesis/citations.txt thesis/quotations.txt backup</code></pre>
<p>What does <code>cp</code> do when given three or more filenames, as in:</p>
<pre><code>$ ls -F
intro.txt methods.txt survey.txt
$ cp intro.txt methods.txt survey.txt</code></pre>
</div>
</section>
</div>
</div>
</article>
<div class="footer">
<a class="label swc-blue-bg" href="http://software-carpentry.org">Software Carpentry</a>
<a class="label swc-blue-bg" href="https://github.com/swcarpentry/shell-novice">Source</a>
<a class="label swc-blue-bg" href="mailto:[email protected]">Contact</a>
<a class="label swc-blue-bg" href="LICENSE.html">License</a>
</div>
</div>
<!-- Javascript placed at the end of the document so the pages load faster -->
<script src="http://software-carpentry.org/v5/js/jquery-1.9.1.min.js"></script>
<script src="css/bootstrap/bootstrap-js/bootstrap.js"></script>
<script src='https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'></script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-37305346-2', 'auto');
ga('send', 'pageview');
</script>
</body>
</html>