You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We now know how to explore directories and files, but how do we create new ones?
139
+
140
+
Go ahead and create a new directory called `my_scripts` using the command `mkdir my_scripts`.
141
+
142
+
```
143
+
$ cd
144
+
$ mkdir my_scripts
145
+
```
146
+
147
+
As you might (or might not) guess from its name, `mkdir` means *make directory*.
148
+
Since `my_scripts` is a relative path (i.e., doesn't have a leading slash),
149
+
the new directory is made below the current working directory:
150
+
151
+
```
152
+
$ ls -F
153
+
```
154
+
155
+
However, there's nothing in it yet:
156
+
157
+
```
158
+
$ cd my_scripts
159
+
$ ls -F
160
+
```
161
+
162
+
To create an empty file, you can use the command `touch`.
163
+
164
+
```
165
+
$ touch draft.txt
166
+
$ ls -F
167
+
```
168
+
169
+
At this point, you want to make a backup of this *very* important empty file. You can use `cp` to make copies of files or directories
137
170
138
-
mkdirnano (or Leafpad)mvcprmrmdir
171
+
```
172
+
$ cp draft.txt draft2.txt
173
+
$ ls -F
174
+
```
175
+
176
+
You can see that the command `cp` took two filenames. The first one was the existing file, the second was the new file.
177
+
178
+
If you decide that *draft2.txt* is not the best name for a backup file, you can change a name using the `mv` command which moves the file.
179
+
180
+
```
181
+
$ mv draft2.txt draft_backup.txt
182
+
$ ls -F
183
+
```
184
+
185
+
As *move* implies, it can be used to actually move the file someplace else. For example, if we do not want the backup file in this directory, we can move it out.
186
+
187
+
```
188
+
$ mv draft_backup.txt ../draft_backup.txt
189
+
$ ls -F
190
+
```
191
+
192
+
Based on what you know from `cp`, can you see how `mv` works? Where is the draft_backup.txt file now? Can you find it?
193
+
194
+
```
195
+
$ cd ..
196
+
$ ls -F
197
+
```
198
+
199
+
Okay, now that we have made a mess with empty files and directories we do not actually need, let’s clean up.
200
+
201
+
***Notice!*** Deleting Is Forever
202
+
203
+
```
204
+
$ rm draft_backup.txt
205
+
$ ls -F
206
+
```
207
+
208
+
`rm` is short for "remove”. It works on files, but not directories. For directories, you can use `rmdir`.
209
+
210
+
For safety purposes, `rmdir` only works on empty directories. Because we left a file in the folder *my_scripts*, the following command will not work.
211
+
212
+
```
213
+
$ rmdir my_scripts
214
+
```
215
+
216
+
To override this, you can use `rm -r`. Please be careful with this command, and only use it on directories that you are certain have nothing you wish to keep.
217
+
218
+
```
219
+
$ rm -r my_scripts
220
+
$ ls -F
221
+
```
222
+
223
+
The `-r` in this case indicates that the command is *recursive*, which means it will delete all files and subdirectories and their files and subdirectories and so on and so on. As you can imagine, this could be a lot of files.
139
224
140
225
#### Challenges
141
226
142
-
1. Don’t erase your filesystem!
227
+
1. Don’t erase your entire filesystem!
143
228
144
229
#### What you learned
145
230
146
231
1. Making a new directory with `mkdir`
147
-
2. Creating a file
232
+
2. Creating an empty file with `touch`
148
233
3. Changing the name and/or location of a file or directory with `mv`
149
234
4. Duplicating a file or directory with `cp`
150
235
5. Removing a file or directory with `rm` or `rmdir` respectively
0 commit comments