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
This and subsequent subsections are written to be followed as an extended exercise with explanations given as we go.
18
-
</p>
19
-
<exercisexml:id="exercise-try-cli">
14
+
<subsectionxml:id="subsec-intro_shell_commands">
15
+
<title>Introduction to Shell Commands</title>
16
+
<exercisexml:id="exercise-try-cli">
20
17
<title>Exercise: Try shell commands for navigation</title>
18
+
21
19
<p>Try each of the following commands on your own machine.</p>
22
20
</exercise>
23
-
<p>
24
-
First, follow the directions given in <xrefref="subsec-getting_terminal"/> to open a terminal window so you can use your shell. All of the shell commands will be typed in this terminal window.
25
-
</p>
26
-
<paragraphs>
27
-
<title>Introduction to Shell Commands</title>
21
+
<p>
22
+
This and subsequent subsections are written to be followed as an extended exercise with explanations given as we go.
23
+
</p>
24
+
<p>
25
+
This section is written to be followed as an extended exercise with explanations given as we go.
26
+
</p>
28
27
<p>
29
28
</p>
30
29
<p>
@@ -94,9 +93,9 @@
94
93
<p>
95
94
The reason you see something like this is because the SHELL environment variable stores the location of the shell program. This location, called the <term>file path</term>, is given in the form of the path needed to find the location. We explore how this path is described in the next section.
You should see a listing of files and directories.
158
157
</p>
158
+
</subsection>
159
159
160
-
</paragraphs>
161
-
<paragraphs>
160
+
<subsectionxml:id="subsec-shell_command_options">
162
161
<title>Shell Command Options</title>
163
162
<p>
164
163
</p>
@@ -187,10 +186,10 @@ lrwxrwxrwx 1 root root 7 Mar 24 2022 bin -> usr/bin
187
186
188
187
<p>
189
188
Thus, the <c>-l</c> flag changed how the listing is displayed.
190
-
</p>
191
-
192
-
</paragraphs>
193
-
<paragraphs>
189
+
</p>
190
+
</subsection>
191
+
192
+
<subsectionxml:id="subsec-file_permissions">
194
193
<title>File Permissions</title>
195
194
<p>
196
195
@@ -219,9 +218,9 @@ lrwxrwxrwx 1 root pearcej 7 Mar 24 2022 bin -> usr/bin
219
218
<p>
220
219
Occasionally, you need to change permissions of a file. For example, you might need to change permissions to make a file executable. Changing file permissions is done with the <c>chmod</c> command. We will explore this in a bit.
221
220
</p>
222
-
</paragraphs>
221
+
</subsection>
223
222
224
-
<paragraphs>
223
+
<subsectionxml:id="subsec-command_options">
225
224
<title>Learning More About Command Options</title>
226
225
<p>
227
226
</p>
@@ -242,8 +241,8 @@ lrwxrwxrwx 1 root pearcej 7 Mar 24 2022 bin -> usr/bin
Try it! This is an improvement because it is faster and avoids the use of a temporary file.
485
484
</p>
486
485
487
-
</paragraphs>
486
+
</subsection>
488
487
489
-
<paragraphs>
488
+
<subsectionxml:id="subsec-scripts">
490
489
<title>Scripts</title>
491
490
<p>
492
491
@@ -546,9 +545,9 @@ wc -w < temp.txt
546
545
chmod +x hellouser.sh
547
546
./hellouser.sh
548
547
</pre>
549
-
</paragraphs>
548
+
</subsection>
550
549
551
-
<paragraphs>
550
+
<subsectionxml:id="subsec-file_management">
552
551
<title>File Management</title>
553
552
<p>
554
553
</p>
@@ -614,21 +613,19 @@ rm -i newfile4.txt
614
613
<p>
615
614
And, if you respond with "n", then the removal will not happen.
616
615
</p>
617
-
</paragraphs>
618
-
<paragraphs>
616
+
</subsection>
617
+
<subsectionxml:id="subsec-shell_commands">
619
618
<title>A few time-saving shell commands</title>
620
619
<p>
621
-
622
-
</p>
623
-
<p>
624
-
The <em>up arrow</em> key retrieves the previous shell command. If you press it multiple times, it will take you back through multiple commands in your shell history. This is a useful way to repeat a command. For example, if you had a typo, you can use the up arrow, edit the command, and push enter to fix the command. Analogously, the <em>down arrow</em> will move you in the reverse direction through the shell command history. For more useful shell commands, type <c>man bash</c> for hints on how to search your shell history, re-execute commands, and much more.
625
620
</p>
626
621
<p>
622
+
The <em>up arrow</em> key retrieves the previous shell command. If you press it multiple times, it will take you back through multiple commands in your shell history. This is a useful way to repeat a command. For example, if you had a typo, you can use the up arrow, edit the command, and push enter to fix the command. Analogously, the <em>down arrow</em> will move you in the reverse direction through the shell command history. For more useful shell commands, type <c>man bash</c> for hints on how to search your shell history, re-execute commands, and much more.
623
+
</p>
627
624
<p>
628
-
An additional efficiency-enhancing feature is the <term>history</term> command. This command conveniently presents a record of previously executed shell commands, enabling users to effortlessly revisit their command history.
625
+
An additional efficiency-enhancing feature is the <term>history</term> command. This command conveniently presents a record of previously executed shell commands, enabling users to effortlessly revisit their command history.
629
626
</p>
630
627
<p>
631
-
Here's an example of what might be displayed when the <c>history</c> command is executed:
628
+
Here's an example of what might be displayed when the <c>history</c> command is executed:
632
629
</p>
633
630
<pre>
634
631
1 git init
@@ -639,9 +636,8 @@ And, if you respond with "n", then the removal will not happen.
639
636
6 history
640
637
</pre>
641
638
642
-
<p>
639
+
<p>
643
640
This paragraph is intended to alert you to some useful search features. A couple examples of very common search patterns are using wildcards for zero or more characters or for a single character. The asterisk (*) specifies zero or more characters to match. In bash the question mark (?) is used for matching exactly one single character.
644
-
</p>
645
641
<notation>
646
642
<usage><m>*</m></usage>
647
643
<description>asterisk - wildcard for zero or more characters</description>
@@ -650,116 +646,81 @@ And, if you respond with "n", then the removal will not happen.
650
646
<usage><m>?</m></usage>
651
647
<description>question mark - wildcard for single character</description>
652
648
</notation>
653
-
</p>
654
-
<p>
655
-
For example, if we type the following:
656
-
</p>
657
-
<pre>
649
+
</p> <p>
650
+
For example, if we type the following:
651
+
</p>
652
+
<pre>
658
653
rm -i newfile?.txt
659
654
</pre>
660
-
<p>
655
+
<p>
661
656
Then the question mark will match with any single character, and we will see the following prompts:
662
657
</p>
663
-
<pre>
658
+
<pre>
664
659
rm: remove regular file 'newfile2.txt'?
665
660
rm: remove regular file 'newfile4.txt'?
666
661
</pre>
667
-
<p>
662
+
<p>
668
663
If we instead type:
669
664
</p>
670
-
<pre>
665
+
<pre>
671
666
rm -i newfile*.txt
672
-
</pre>
673
-
<p>
667
+
</pre>
668
+
<p>
674
669
Then the asterisk will match with any number of characters (including zero), and we will see the following prompts:
675
670
</p>
676
-
<pre>
671
+
<pre>
677
672
rm: remove regular file 'newfile2.txt'?
678
673
rm: remove regular file 'newfile4.txt'?
679
674
rm: remove regular file 'newfile2_cp.txt'?
680
675
</pre>
681
-
<p>
682
-
As you can see, these search patterns give you a lot of power and control.
683
-
</p>
684
-
</paragraphs>
685
-
686
-
</subsection>
687
-
688
-
<conclusion>
689
-
<title>Conclusion</title>
690
676
<p>
691
-
Hopefully, you now feel a bit more comfortable using the shell. The shell commands discussed above are summarized in Appendix <xrefref="ap_shell_cheat_sheet"/>.
677
+
As you can see, these search patterns give you a lot of power and control.
692
678
</p>
693
-
</conclusion>
694
679
695
-
<exercise label = "basicShellCommands" numbered = "yes" language = "natural"adaptive="yes">
680
+
681
+
<exercisexml:id="quiz_basic_shell_commands">
682
+
<title>Quiz: Basic Shell Commands</title>
696
683
<statement>
697
-
<p>
698
-
You have a file named <term>data.txt</term> that contains the following lines:
699
-
</p>
700
-
<pre>apple
684
+
<p>
685
+
You have a file named <term>data.txt</term> that contains the following lines:
686
+
</p>
687
+
<pre>apple
701
688
orange
702
689
banana
703
690
grape
704
-
</pre>
705
-
<p>
706
-
Your task is to create a new file named <term>fruits.txt</term> and copy the contents of <term>data.txt</term> to <term>fruits.txt</term>. Next, append the word <c>kiwi</c> to <term>fruits.txt</term>. Then, display the contents of <term>fruits.txt</term> in the terminal. Next, count the number of fruits in <term>fruits.txt</term> and display the total count. Finally, overwrite the content of <term>data.txt</term> with the content of <term>fruits.txt</term> and rename <term>data.txt</term> to a new file named <c>fruits2</c>.
707
-
</p>
708
-
<p>
709
-
Here are some commands you may need to perform the task. Rearrange the correct commands in the correct order by dragging and dropping.
710
-
</p>
691
+
</pre>
692
+
<p>
693
+
Your task is to create a new file named <term>fruits.txt</term> and copy the contents of <term>data.txt</term> to <term>fruits.txt</term>. Next, append the word <c>kiwi</c> to <term>fruits.txt</term>. Then, display the contents of <term>fruits.txt</term> in the terminal. Next, count the number of fruits in <term>fruits.txt</term> and display the total count. Finally, overwrite the content of <term>data.txt</term> with the content of <term>fruits.txt</term> and rename <term>data.txt</term> to a new file named <c>fruits2</c>.
694
+
</p>
695
+
<p>
696
+
Here are some commands you may need to perform the task. Rearrange the correct commands in the correct order by dragging and dropping.
697
+
</p>
711
698
</statement>
712
699
<blocks>
713
-
<blockorder="1">
714
-
<choice correct = "yes">
715
-
<p>
716
-
<c>cat data.txt > fruits.txt</c>
717
-
</p>
718
-
</choice>
719
-
<choice>
720
-
<p>
721
-
<c>cat data.txt >> fruits.txt</c>
722
-
</p>
723
-
</choice>
724
-
</block>
725
-
<blockorder="2">
726
-
<p>
727
-
<c>echo "kiwi" >> fruits.txt</c>
728
-
</p>
729
-
</block>
730
-
<blockorder="3">
731
-
<p>
732
-
<c>cat fruits.txt</c>
733
-
</p>
734
-
</block>
735
-
<block order ="4">
736
-
<choice>
737
-
<p>
738
-
<c>wc -l < fruits.txt</c>
739
-
</p>
740
-
</choice>
741
-
<choice correct = "yes">
742
-
<p>
743
-
<c>wc -w < fruits.txt</c>
744
-
</p>
745
-
</choice>
746
-
</block>
747
-
<blockorder="5">
700
+
<blockorder="1">
701
+
<choicecorrect="yes">
748
702
<p>
749
-
<c>cp -i fruits.txt data.txt</c>
703
+
<c>cat data.txt > fruits.txt</c>
750
704
</p>
751
-
</block>
752
-
<blockorder="6">
705
+
</choice>
706
+
<choice>
753
707
<p>
754
-
<c>mv data.txt fruits2.txt</c>
708
+
<c>cat data.txt >> fruits.txt</c>
755
709
</p>
756
-
</block>
757
-
<blockcorrect="no">
758
-
<p>
759
-
<c>rm data.txt fruits2.txt</c>
760
-
</p>
761
-
</block>
710
+
</choice>
711
+
</block>
762
712
</blocks>
763
713
</exercise>
764
-
</section>
714
+
715
+
</subsection>
765
716
717
+
718
+
719
+
720
+
<conclusion>
721
+
<title>Conclusion</title>
722
+
<p>
723
+
Hopefully, you now feel a bit more comfortable using the shell. The shell commands discussed above are summarized in Appendix <xrefref="ap_shell_cheat_sheet"/>.
0 commit comments