Unix Command Basics new Developers should know
Tags: ubuntu, linux
Published: 2020-02-16 17:23:19

<!-- wp:paragraph -->
<p>At my first internship for an IT firm, I was very confused when asked to work with the terminal (or "shell"). It seemed very intimidating and the sure mass of different commands I was seeing people using while googling for answers to problems was very overwhelming. In this tutorial, I will break down some of the fundamental commands that you should know, so that you can have a much easier time than I did! Also, in case you didn't know: you can use unix shell command if you are working with a machine running Linux or Mac!</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Why learn how to use the Terminal</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Most common use for the terminal is for server administration on remote machines. This is important for developers who are running their applications on remote servers that need to be setup, maintained, etc. Another use is various development applications and tools that do not have their own GUI, so the command line is used instead (most commonly NPM and Git). Finally, it can also be much faster to work from the shell once you are comfortable with it (which will take time but is ultimately very much worth it). These aren't the only reasons, but hopefully they are good enough to keep you motivated in learning this stuff!</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Breaking down a Unix Command</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>When we open a terminal and write something inside, our computer will try to look through a list of installed applications or commands and will attempt to initiate a sequence of processes based on that. Most commands require some level of user input, on Linux these come in the form of Options (also referred to as Flags) and Arguments. Options/flags are going to allow us to customize how the command is executed, while arguments are going to give the command vital input. We usually use options interchangeably, whereas arguments can sometimes be invalid or missing, causing the command to throw up an error. Let's take a look at a simple example, don't worry if you are not familiar with the command:</p>
<!-- /wp:paragraph -->

<!-- wp:html -->
<pre><code>ls -l /bin</code></pre>
<!-- /wp:html -->

<!-- wp:paragraph -->
<p>In this case, our command is <em>ls</em>, which lists files in a directory - try executing it and note what you are seeing. Options follow after a hyphen, in this case the option is a single <em>l</em>, which stands for "long" and makes sure that all files are listed in the long file format. Try removing the option and see how the output changes. The amount of available options is often overwhelming, so let's look at a <a href="https://www.rapidtables.com/code/linux/ls.html">site that has all the options for ls</a>. We can see that we can also combine options, like allowing hidden files to be visible by appending the "a" character to our options. Finally, we have the argument, which in this case is just a single directory path (the directory we want to view).</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Manpages</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Pretty much every command has a "manpage", which stands for "Manual Page". These are designed to give you a detailed explanation of what exactly the command does and all of the flags. Let's use it on the previous command:</p>
<!-- /wp:paragraph -->

<!-- wp:html -->
<pre><code>man ls</code></pre>
<!-- /wp:html -->

<!-- wp:paragraph -->
<p>Entering the previous command, we can see that our terminal is now listing all the different options for the ls command, along with useful descriptions on what each does. You can scroll around or press "q" to quit the reading view once you are done.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Navigating the Filesystem via the Terminal</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>I am now going to give you a short introduction to how we can navigate the file system with our terminal. This is important, since most work on a server will require you to move around and inspect directories or files. Our terminal always has an absolute location, which tells us where we are located right now in the file system. We can view this location with the command <em>pwd</em> (short for "print working directory"). Type <em>pwd</em> into your terminal and you will see the directory you are currently in!</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>For moving around the most common command is <em>cd</em>, short for "current directory", which allows us to move from our current directory to another. For example, in my "Home" folder (the default directory when opening a new terminal), I have a folder called "Documents". If I enter the "cd" command and pass Documents as the arguments, you will see that your have been moved to this directory. You can also do absolute paths, by starting off the argument path with a dash (i.e. <em>cd /bin</em> will take you to the bin folder, no matter where you are in the file system). Here is a quick overview, you can see the terminal directory on the next to where I entered the commands:</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":538,"sizeSlug":"large"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><img src="https://wp.jdit.eu/wp-content/uploads/2020/02/cd-example.png" alt="" class="wp-image-538"/></figure></div>
<!-- /wp:image -->

<!-- wp:heading {"level":3} -->
<h3>Basic File manipulation</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>In unix, you can create files with the <em>touch</em> command, which takes the filename as an argument. Using the command <em>cat</em>, we can pass a filepath as the argument and have the contents of that file displayed in our console. The <em>rm</em> command is used to delete the file that is passed as the argument. Remember: it is always relative to the location of your current terminal. The <em>cp</em> command is used to copy files. You can move files with the <em>mv</em> command, the first argument is the relative path to the file or directory you are trying to move, the second is the target directory. You can also use <em>mv</em> to rename files. For this, you need to add the original file as the first argument and have the second argument be that same file only with the name adjusted at the end (e.g. <em>mv helloworld.txt hello.txt</em> will rename a file helloworld.txt to hello.txt). All files within a directory can be manipulated using the "wildcard"   symbol *, which is used to select multiple files that follow similar patterns (useful for operations like moving or copying all files in a directory). For example, <em>mv ./*.jpeg ./my-images</em> will move all files ending with .jpeg into a directory named "my-images".</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Viewing System Information</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The command <em>w</em> requires no arguments and simply gives us a list of information on the user for our current session. <em>uname</em> displays details on the operating system we are running, but is only really useful when combined with different flags that let you output everything in a more detailed fashion (<em>uname -a</em> shows you the exact version of your operating system and distro, for example).</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Commandline tricks to make you work faster</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Your commandline's history is saved to a file, meaning you can access previous commands quickly. Press Ctrl + R to go into history mode where you can type in the beginnings of commands and have a type of auto-completion available, based on recent commands you entered (press Ctrl + C to exit it again if you cant find what you are looking).  Because of this, you can also toggle between last commands very quickly using the up and down arrow keys - this will significantly increase the rate at which you can work with the terminal. I also suggest looking into how you can quickly change positions on the terminal while you are working on it i.e. hopping to the start or end of the line using 7 and 1 on the numpad respectively.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>I hope you enjoyed this quick intro to the command line! If you want to learn more, want to share some of your own tricks or think that something is wrong/missing, please leave a comment.</p>
<!-- /wp:paragraph -->