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
Copy file name to clipboardExpand all lines: docs/modify.md
+92-64Lines changed: 92 additions & 64 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,109 +17,126 @@
17
17
18
18
This section will show how to work with files and directories through the command line interface (CLI).
19
19
20
-
### Directories
20
+
### Files
21
21
22
-
-**mkdir DIR**: Creates a directory DIR
23
-
-**mkdir -p DIR/SUBDIR**: creates a directory DIR with the subdirectory SUBDIR
24
-
-**rm -r DIR**: Removes a directory DIR. The flag "-r" means recursively
25
-
- You can also add "-f". This means ignore nonexistent files and arguments, and never prompt.
26
-
- You can add the option "-i". This means it will prompt before every removal.
22
+
To create files, you would normally use an editor (``nano``, ``vim``, ``emacs``, etc.), but you can also create an empty file with the command `touch`.
27
23
28
-
!!! example "Examples: creating and removing directories"
24
+
```bash
25
+
touch FILE
26
+
```
29
27
30
-
Create a directory called ``mynewdir``
28
+
You can remove files with ``rm``. You can use the flag/option ``-i`` to prompt before removing a file. Be aware that files removed with ``rm`` are deleted *permanently*---they generally cannot be restored (people have gotten lucky with system backup snapshots, but do not assume that those will be available).
31
29
32
-
```bash
33
-
mkdir mynewdir
34
-
```
30
+
!!! example "Examples"
35
31
36
-
Create a directory called ``cooldir`` which has a subdirectory called ``fancydir``
32
+
Create a file called ``file.txt``
37
33
38
34
```bash
39
-
mkdir -p cooldir/fancydir
35
+
touch file.txt
40
36
```
41
37
42
-
Remove the directory ``mynewdir``
38
+
Remove the file ``file.txt``
43
39
44
40
```bash
45
-
rm -r mynewdir
41
+
rm file.txt
46
42
```
47
43
48
-
### Files
44
+
!!! warning
49
45
50
-
To create files, you would normally use an editor (``nano``, ``vim``, ``emacs``, etc.), but it is also possible to create an empty file with the command ``touch``.
46
+
If you do not add the flag/option "-i" the file will be deleted without prompting. Be careful!
51
47
52
-
```bash
53
-
touch FILE
54
-
```
48
+
Be **extra** careful using `rm -rf` with glob patterns (see [Wild Cards under The File System](../filesystem/#wild__cards) )! It is strongly recommended that you always test a pattern with `ls` and check that the output is what you expect before using `rm -rf` on that pattern.
55
49
56
-
(The `touch` command is more often used to update the timestamps (specifically the latest access and modification times) of existing files.)
50
+
### Directories
57
51
58
-
You can remove files with ``rm``. Again, you can use the flag/option ``-i`` to prompt before removing a file. Be aware that files removed with ``rm`` are deleted *permanently*---they generally cannot be restored (people have gotten lucky with system backup snapshots, but do not assume that those will be available).
52
+
-**`mkdir DIR`**: Creates a directory DIR
53
+
-**`mkdir -p DIR/SUBDIR`**: creates a directory DIR with the subdirectory SUBDIR
54
+
-**`rm -r DIR`**: Removes a directory DIR. The flag `-r` means recursively.
55
+
- You can also add `-f` (meaning force). This means ignore nonexistent files and arguments, and never ask before deleting the target.
56
+
- Again, you can add the option `-i`. This means it will prompt before every removal.
59
57
60
-
!!! warning
58
+
!!! example "Examples: creating and removing directories"
61
59
62
-
If you do not add the flag/option "-i" the file will be deleted without prompting. Be careful!
60
+
Create a directory called ``mynewdir``
63
61
64
-
Be **extra** careful using `rm -rf` with glob patterns (see [Wild Cards under The File System](../filesystem/#wild__cards) )! It is strongly recommended that you always test a pattern with `ls` and check that the output is what you expect before using `rm -rf` on that pattern.
65
-
66
-
!!! example "Examples"
62
+
```bash
63
+
mkdir mynewdir
64
+
```
67
65
68
-
Create a file called ``file.txt``
66
+
Create a directory called ``cooldir`` which has a subdirectory called ``fancydir``
69
67
70
68
```bash
71
-
touch file.txt
69
+
mkdir -p cooldir/fancydir
72
70
```
73
71
74
-
Remove the file ``file.txt``
72
+
Remove the directory ``mynewdir``
75
73
76
74
```bash
77
-
rm file.txt
75
+
rm -r mynewdir
78
76
```
79
-
77
+
80
78
### Examples
81
79
82
80
???+ faq "Reminder"
83
81
84
-
- **mkdir DIR**: Create a directory DIR
85
-
- **rm -rf DIR**: Remove a directory DIR. The flag "-r" means recursively and "-f" means do so without asking for each file and subdirectory. Useful, but dangerous. Be careful!
86
-
- **cd**: Go to your home directory ($HOME)
87
-
- **cd DIR**: Change directory to DIR
88
-
- **cd ..**: Change directory to the parent directory of the current directory
89
-
- **cd -**: go back to the previous working directory
90
-
- **touch FILE**: create an empty file with the name FILE
91
-
- **rm FILE**: remove the file with the name FILE
92
-
- The command `pwd` tells you the current directory path.
82
+
- **`mkdir DIR`**: Create a directory DIR
83
+
- **`rm -rf DIR`**: Remove a directory DIR. The flag "-r" means recursively and "-f" means do so without asking for each file and subdirectory. Useful, but dangerous. Be careful!
84
+
- **`cd`**: Go to your home directory ($HOME)
85
+
- **`cd DIR`**: Change directory to DIR
86
+
- **`cd ..`**: Change directory to the parent directory of the current directory
87
+
- **`cd -`**: Go back to the previous working directory
88
+
- **`touch FILE`**: Create an empty file with the name FILE or update the timestamps of an existing file named FILE
89
+
- **`rm FILE`**: Remove the file with the name FILE
90
+
- **`pwd`**: print the current working directory path in full.
93
91
94
92
!!! example "Creating directories, changing directories, removing directory and file, removing files by pattern"
95
93
96
94
This example sequence will demonstrate some of the things we just learned, as well as the command `cd` and glob patterns from the previous section.
@@ -138,10 +155,11 @@ You can remove files with ``rm``. Again, you can use the flag/option ``-i`` to p
138
155
139
156
This command is used to copy files or directories.
140
157
141
-
-**cp myfile.txt DIR/**: copy the file "myfile.txt" to the directory DIR
142
-
-**cp DIR1/ DIR2/**: copy the directory DIR1 into the directory DIR2 (Note: overwrites existing files with same name)
143
-
-**cp -R DIR1/ DIR2/**: copy the directory DIR1 and all subdirectories into the directory DIR2.
144
-
-**cp -i file.txt DIR/**: Interactive. It will ask before overwriting if there is a file with the same name.
158
+
-**`cp myfile.txt myfile2.txt`**: make a copy of "myfile.txt" named "myfile2.txt"
159
+
-**`cp myfile.txt DIR/`**: copy the file "myfile.txt" into the directory DIR
160
+
-**`cp DIR1/ DIR2/`**: copy the directory DIR1 into the directory DIR2 (Note: only works if DIR1 contains no subdirectories)
161
+
-**`cp -r DIR1/ DIR2/`**: copy the directory DIR1 and all subdirectories into the directory DIR2.
162
+
-**`cp -i file.txt DIR/`**: Interactive. It will ask before overwriting if there is a file with the same name.
145
163
146
164
!!! warning
147
165
@@ -160,43 +178,48 @@ This command is used to copy files or directories.
160
178
cd exercises
161
179
cd mytestdir
162
180
```
181
+
163
182
2. Copy the file ``myfile.txt`` to the subdirectory ``testdir1``:
164
183
165
184
```bash
166
185
cp myfile.txt testdir1
167
186
```
187
+
168
188
3. Create a new directory called ``testdir3`` inside ``testdir1``
169
189
170
190
```bash
171
191
cd testdir1
172
192
mkdir testdir3
173
193
```
194
+
174
195
4. Copy the new subdirectory ``testdir3`` to the directory ``testdir2``. Remember, "testdir2" is located outside "testdir1" and at the same "level". This can be done in more than one way. Remember you need the option ``-r`` (for recursive) when copying directories:
175
196
a) "Go up one" and then copy:
176
197
```bash
177
198
cd ..
178
199
cp -r testdir1/testdir3 testdir2/
179
200
```
201
+
180
202
b) Copy will standing inside ``testdir1``
181
203
```bash
182
204
cp -r testdir3 ../testdir2
183
205
```
206
+
184
207
5. If you give the full path while copying, this can be done from anywhere.
185
208
186
209
## mv - rename files/directories
187
210
188
-
The command <code>mv</code> is used to rename files and directories, and to **move** a file or directory to another location.
211
+
The command `mv` is used to rename files and directories, and to **move** a file or directory to another location.
189
212
190
-
-**mv file1.txt file2.txt**: renames <code>file1.txt</code> to <code>file2.txt</code>
191
-
-**mv DIR1/ DIR2/**: renames directory <code>DIR1</code> to directory <code>DIR2/</code>
192
-
-**mv file1.txt DIR1/**: moves the file <code>file1.txt</code> into the directory <code>DIR1/</code>
193
-
-**mv DIR1 DIR2/**: (note lack of forward slash after <code>DIR1</code>) moves directory <code>DIR1</code> into directory <code>DIR2/</code>.
194
-
-**mv -i file1.txt file2.txt**: interactive. Asks before overwriting if there is already a file with the destination name.
195
-
-**mv -i DIR1/ DIR2/**: interactive. Asks before overwriting, if there is already a directory with that name.
213
+
-**`mv file1.txt file2.txt`**: renames `file1.txt` to `file2.txt`
214
+
-**`mv DIR1/ DIR2/`**: renames directory `DIR1` to directory `DIR2/`
215
+
-**`mv file1.txt DIR1/`**: moves the file `file1.txt` into the directory `DIR1/`
216
+
-**`mv DIR1 DIR2/`**: (note lack of forward slash after `DIR1`) moves directory `DIR1` into directory `DIR2/`.
217
+
-**`mv -i file1.txt file2.txt`**: interactive. Asks before overwriting if there is already a file with the destination name.
218
+
-**`mv -i DIR1/ DIR2/`**: interactive. Asks before overwriting, if there is already a directory with that name.
196
219
197
220
!!! tip
198
221
199
-
<code>mv</code> complains if there is already a file/directory with the new name. You can force the renaming with "-f" at the cost of the disappearence of the file that previously held the name.
222
+
`mv` complains if there is already a file/directory with the new name. You can force the renaming with "-f" at the cost of the disappearence of the file that previously held the name.
200
223
201
224
### Exercise
202
225
@@ -219,37 +242,43 @@ The command <code>mv</code> is used to rename files and directories, and to **mo
219
242
touch bfile.txt
220
243
touch cfile.txt
221
244
```
245
+
222
246
2. I make the directory ``newdir`` and the subdirectory ``subdir``
223
247
224
248
```bash
225
249
mkdir newdir
226
250
cd newdir
227
251
mkdir subdir
228
252
```
253
+
229
254
3. I create a file named ``newfile.dat``
230
255
231
256
```bash
232
257
cd subdir
233
258
touch newfile.dat
234
259
```
260
+
235
261
4. I name the file ``secondfile.txt`` and move it into ``subdir``
236
262
237
263
```bash
238
264
cd ..
239
265
touch secondfile.txt
240
266
mv secondfile.txt subdir
241
267
```
268
+
242
269
5. I rename the first directory (top-level directory) I created, calling it ``fancydir``
243
270
244
271
```bash
245
272
cd ..
246
273
mv newdir fancydir
247
274
```
275
+
248
276
6. I remove the file ``afile.txt`` while working in the directory outside of ``fancydir`` (previously called ``newdir``)
249
277
250
278
```bash
251
279
rm fancydir/afile.txt
252
280
```
281
+
253
282
7. I remove the subdirectory ``subdir`` while outside the directory ``fancydir``
0 commit comments