<h1class="courselink">Learning Linux Command Line</h1>
<h2class="lecturer">LinkedIn Learning : Scott Simpson</h2>
<h2class="episodetitle">Command-Line Basics</h2>
</div>
<article>
<h2class="sectiontitle">What is the Command Line?</h2>
<p>The basic structure of a command is:</p>
<preclass="inset">
Command Option(s) Argument(s)</pre>
<p>A command is always required but both options and arguments are optional.</p>
<p>Options are usually a single letter but often have a longer equivalent. For instance</p>
<preclass="inset">$ls -a</pre>
<p>will list all files in a directory, but so will</p>
<preclass="inset">$ls --all</pre>
<p>Note the double hyphen when using the longer version. Actually, in this example, is you execute the command (with a single hyphen)</p>
<preclass="inset">$ls -all</pre>
<p>this may appear to work, however, it is interpreting the a in all as the short version of the option and the l as the shortened version of the option for a long directory listing (and is seeming to simply ignore the second l). So, in other words, this is equivalent to typing</p>
<preclass="inset">$ls -al</pre>
<p>To see all of the options available for a particular command you can view its manual page using the command man followed by the command you want to investigate so for the ls command this would be</p>
<preclass="inset">$man ls</pre>
<p>It maty be worth noting that if you use the short versions of the options, you can group them together but if you use the longer versions, each option must be separated by a space (which is why the long version of the option all was interpreted as two separate options, a followed by l).</p>
<p>The arguments modify the command by specifying what we want it to operate on. For instance, let’s say we want to display the contents of the folder /var/lib without navigating to that folder, we can do that with the command</p>
<preclass="inset">$ls -al /var/lib</pre>
<h2class="sectiontitle">Helpful Keyboard Shortcuts in the Terminal</h2>
<p>Tab completion, when you are typing something out at the prompt such as a directory or file name, you can type part of the name and press tab to auto-complete it, provided the full name can be inferred unambiguously.</p>
<figure>
<imgsrc=" images\figure1.png "alt="Figure 1 - a typical directory listing for a home directory">
<figcaption>Figure 1 - a typical directory listing for a home directory</figcaption>
</figure>
<p>For instance, the contents of a typical home directory might be something like that shown in figure 1.</p>
<p>Now, let’s say we want to navigate to the Desktop directory, we can do this by typing</p>
<preclass="inset">$cd De</pre>
<p>followed by the tab key. This will autocomplete the command giving us</p>
<preclass="inset">$cd Desktop</pre>
<p>and we can then complete the operation by pressing return. This is possible because Desktop is
the only directory starting with De. If we were to type</p>
<preclass="inset">$Do</pre>
<p>followed by tab, we would see that nothing happens because Linux is unable to determine whether we want to change to the Documents folder or the Downloads folder However, if we press tab a second time, we will see that Linux lists all possible options and then repeats the command so that we can complete it with one of the available directory names. This is shown in figure 2.</p>
<figure>
<imgsrc=" images\figure2.png"alt="Figure 2 - an attempt to use tab autocomplete when there is ambiguity over the directory name selected">
<figcaption> Figure 2 - an attempt to use tab autocomplete when there is ambiguity over the directory name selected </figcaption>
</figure>
<p>You can also use tab autocomplete if you can’t quite remember the name of a command. For instance, if you type a followed by a tab, nothing happens. But if you press tab again, you will see a list of commands that start with an a.</p>
<p>If you then press space followed by tab twice, you will see a list of possible arguments for the command a. Of course, a is not a genuine command so we are really seeing everything in the directory which Linux is presenting as possible arguments for the command. Since it doesn’t recognize the command, it cannot make a good decision about possible arguments and this is
presumably why it shows us everything!</p>
<p>If we need to edit a command in the terminal, we can use Control + A to move to the beginning of the line or control + E to move to the end of the line (note that in documentation, such shortcuts can be shown with a caret character representing the control character so control + A would be shown as
<preclass="inset">^A</pre>
<p>and similarly,</p>
<preclass="inset">^E</pre>
<p>represents control + E). A more complete list of shortcuts which was copied from <ahref="https://www.howtogeek.com/howto/ubuntu/keyboard-shortcuts-for-bash-command-shell-for-ubuntu-debian-suse-redhat-linux-etc/">https://www.howtogeek.com/howto/ubuntu/keyboard-shortcuts-for-bash-command-shell-for-ubuntu-debian-suse-redhat-linux-etc/</a> is provided in appendix A.</p>
<h2class="sectiontitle">Finding Help for Commands</h2>
<p>I mentioned the use of man with a command name previously to display a help file (actually, man is short for manual so we might call this a manual page) for a command such as ls. This actually displays the help info using a tool called less and when we are viewing this, we can press q to quit or h for help. In this case, help means help in navigating through a page displayed in less so it would be used for info on how to navigate around one of these manual (man) pages.</p>
<p>Another way to find help for a specific command is to type the name of the command followed by –help. There is also a help command so you could try typing help followed by the name of the command although in many cases, this will simply refer you to the appropriate man page.</p>
<p>So it is easy to help when you know the name of the command. However, if you don’t you can use the command apropos followed by some text relating to the command and this will search for something appropriate. For instance, let’s say we want to find out what commands are used for printing, we can type</p>
<preclass="inset">$apropos print</pre>
<p>or</p>
<preclass="inset">$apropos cups</pre>
<p>Both of which will list commands relevant to printing (cups being the Linux print server).</p>