Skip to content

Commit c244aab

Browse files
authored
Update modify.md
1 parent 247445b commit c244aab

File tree

1 file changed

+92
-64
lines changed

1 file changed

+92
-64
lines changed

docs/modify.md

Lines changed: 92 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -17,109 +17,126 @@
1717

1818
This section will show how to work with files and directories through the command line interface (CLI).
1919

20-
### Directories
20+
### Files
2121

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`.
2723

28-
!!! example "Examples: creating and removing directories"
24+
```bash
25+
touch FILE
26+
```
2927

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).
3129

32-
```bash
33-
mkdir mynewdir
34-
```
30+
!!! example "Examples"
3531

36-
Create a directory called ``cooldir`` which has a subdirectory called ``fancydir``
32+
Create a file called ``file.txt``
3733

3834
```bash
39-
mkdir -p cooldir/fancydir
35+
touch file.txt
4036
```
4137

42-
Remove the directory ``mynewdir``
38+
Remove the file ``file.txt``
4339

4440
```bash
45-
rm -r mynewdir
41+
rm file.txt
4642
```
4743

48-
### Files
44+
!!! warning
4945

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!
5147

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.
5549

56-
(The `touch` command is more often used to update the timestamps (specifically the latest access and modification times) of existing files.)
50+
### Directories
5751

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.
5957

60-
!!! warning
58+
!!! example "Examples: creating and removing directories"
6159

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``
6361

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+
```
6765

68-
Create a file called ``file.txt``
66+
Create a directory called ``cooldir`` which has a subdirectory called ``fancydir``
6967

7068
```bash
71-
touch file.txt
69+
mkdir -p cooldir/fancydir
7270
```
7371

74-
Remove the file ``file.txt``
72+
Remove the directory ``mynewdir``
7573

7674
```bash
77-
rm file.txt
75+
rm -r mynewdir
7876
```
79-
77+
8078
### Examples
8179

8280
???+ faq "Reminder"
8381

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.
9391

9492
!!! example "Creating directories, changing directories, removing directory and file, removing files by pattern"
9593

9694
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.
9795

9896
**HINT: Code-along!**
9997

98+
Create and remove directories:
99+
100100
```bash
101101
[x_rebpi@tetralith1 ~]$ mkdir mytestdir
102102
[x_rebpi@tetralith1 ~]$ cd mytestdir/
103103
[x_rebpi@tetralith1 mytestdir]$ mkdir testdir1
104104
[x_rebpi@tetralith1 mytestdir]$ mkdir testdir2
105105
[x_rebpi@tetralith1 mytestdir]$ mkdir testdir3
106+
[x_rebpi@tetralith1 mytestdir]$ ls
107+
testdir1 testdir2 testdir3
106108
[x_rebpi@tetralith1 mytestdir]$ rm -rf testdir3
109+
[x_rebpi@tetralith1 mytestdir]$ ls
110+
testdir1 testdir2
111+
```
112+
113+
Create and remove files:
114+
115+
```bash
107116
[x_rebpi@tetralith1 mytestdir]$ cd testdir1
108117
[x_rebpi@tetralith1 testdir1]$ touch file1.txt
109118
[x_rebpi@tetralith1 testdir1]$ touch file2.sh
110119
[x_rebpi@tetralith1 testdir1]$ touch file3.c
111120
[x_rebpi@tetralith1 testdir1]$ touch file4.dat
112-
[x_rebpi@tetralith1 testdir1]$ touch file5.txt
113-
[x_rebpi@tetralith1 testdir1]$ rm file5.txt
114-
[x_rebpi@tetralith1 testdir1]$
115-
[x_rebpi@tetralith1 testdir1]$ cd ..
116-
[x_rebpi@tetralith1 mytestdir]$ cd testdir2/
117-
[x_rebpi@tetralith1 testdir2]$ touch meow.txt
118-
[x_rebpi@tetralith1 testdir2]$ touch catsmeow1.txt
119-
[x_rebpi@tetralith1 testdir2]$ touch homeowners_assoc.txt
120-
[x_rebpi@tetralith1 testdir2]$ ls *meow*.txt
121-
[x_rebpi@tetralith1 testdir2]$ rm -r *meow{,1}.txt
121+
[x_rebpi@tetralith1 testdir1]$ rm file4.dat
122+
[x_rebpi@tetralith3 testdir1]$ rm -i file3.c
123+
rm: remove regular empty file 'file3.c'? y
124+
[x_rebpi@tetralith3 testdir1]$ ls
125+
file1.txt file2.sh
126+
```
127+
128+
Removing files by glob pattern (or why to always test a glob pattern with `ls` before using it with `rm`):
122129

130+
```bash
131+
[x_rebpi@tetralith1 testdir1]$ cd ../testdir2
132+
[x_rebpi@tetralith1 testdir2]$ touch meow.txt
133+
[x_rebpi@tetralith1 testdir2]$ touch catsmeow1.dat
134+
[x_rebpi@tetralith1 testdir2]$ touch homeowners_assoc.odt
135+
[x_rebpi@tetralith1 testdir2]$ ls *meow*
136+
catsmeow1.dat homeowners_assoc.odt meow.txt
137+
[x_rebpi@tetralith1 testdir2]$ rm -r *meow{,1}.??t
138+
[x_rebpi@tetralith1 testdir2]$ ls
139+
homeowners_assoc.odt
123140
```
124141

125142
!!! Note
@@ -138,10 +155,11 @@ You can remove files with ``rm``. Again, you can use the flag/option ``-i`` to p
138155

139156
This command is used to copy files or directories.
140157

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.
145163

146164
!!! warning
147165

@@ -160,43 +178,48 @@ This command is used to copy files or directories.
160178
cd exercises
161179
cd mytestdir
162180
```
181+
163182
2. Copy the file ``myfile.txt`` to the subdirectory ``testdir1``:
164183

165184
```bash
166185
cp myfile.txt testdir1
167186
```
187+
168188
3. Create a new directory called ``testdir3`` inside ``testdir1``
169189

170190
```bash
171191
cd testdir1
172192
mkdir testdir3
173193
```
194+
174195
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:
175196
a) "Go up one" and then copy:
176197
```bash
177198
cd ..
178199
cp -r testdir1/testdir3 testdir2/
179200
```
201+
180202
b) Copy will standing inside ``testdir1``
181203
```bash
182204
cp -r testdir3 ../testdir2
183205
```
206+
184207
5. If you give the full path while copying, this can be done from anywhere.
185208

186209
## mv - rename files/directories
187210

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.
189212

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.
196219

197220
!!! tip
198221

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.
200223

201224
### Exercise
202225

@@ -219,37 +242,43 @@ The command <code>mv</code> is used to rename files and directories, and to **mo
219242
touch bfile.txt
220243
touch cfile.txt
221244
```
245+
222246
2. I make the directory ``newdir`` and the subdirectory ``subdir``
223247

224248
```bash
225249
mkdir newdir
226250
cd newdir
227251
mkdir subdir
228252
```
253+
229254
3. I create a file named ``newfile.dat``
230255

231256
```bash
232257
cd subdir
233258
touch newfile.dat
234259
```
260+
235261
4. I name the file ``secondfile.txt`` and move it into ``subdir``
236262

237263
```bash
238264
cd ..
239265
touch secondfile.txt
240266
mv secondfile.txt subdir
241267
```
268+
242269
5. I rename the first directory (top-level directory) I created, calling it ``fancydir``
243270

244271
```bash
245272
cd ..
246273
mv newdir fancydir
247274
```
275+
248276
6. I remove the file ``afile.txt`` while working in the directory outside of ``fancydir`` (previously called ``newdir``)
249277

250278
```bash
251279
rm fancydir/afile.txt
252280
```
281+
253282
7. I remove the subdirectory ``subdir`` while outside the directory ``fancydir``
254283

255284
```bash
@@ -299,4 +328,3 @@ ln -s real-file-or-lib link-name
299328
- The command to rename files and directories is ``mv``
300329
- Symbolic links are pointers to another file or directory
301330
- Always test glob patterns with ``ls`` before using the same patterns with ``rm -r`` to remove files in bulk.
302-

0 commit comments

Comments
 (0)