
    <div class="entry-content">
      <p>Learning to use the terminal is an important step in becoming a
        true power user of Linux, but it’s easy (and normal) to make
        mistakes along the way. Here are the top 5 mistakes new terminal
        users make, and what you can learn from them.</p>
      <h2 class="wp-block-heading">1. Current working directory</h2>
      <p>When you first open a terminal, your current working directory
        is your home folder. You have access to all those directories
        you see in your home directory every time you open a file
        manager (<code>Desktop</code>, <code>Documents</code>, <code>Downloads</code>,
        <code>Music</code>, <code>Pictures</code>, and <code>Videos</code>).
        You can verify your location with the <code>pwd</code> command:</p>
      <pre class="wp-block-code"><code>$ pwd
/home/seth</code></pre>
      <p>You can list the files and folders within your current
        directory with the <code>ls</code> or <code>dir</code> or <code>tree</code>
        commands:</p>
      <pre class="wp-block-code"><code>$ ls
Desktop  Documents  Downloads
Music    Pictures   Videos</code></pre>
      <p>But you don’t usually stay in one place while using the
        terminal. You frequently move from folder to folder so you can
        open or modify or edit files. It’s easy to get lost, forgetting
        what directory you’re in and what files are around you.</p>
      <p><strong>Lesson learned:</strong> When working in the terminal,
        it’s important to regularly verify your current working
        directory with <code>pwd</code> so you don’t accidentally issue
        a command you intended to run in a different location.</p>
      <h2 class="wp-block-heading">2. Use interactive options when using
        wildcards</h2>
      <p>Wildcards are great shorthand to make command entry faster, and
        to perform bulk actions on lots of files. However, they can be
        dangerous when you get them wrong.<br>
        It’s easy to process hundreds of the wrong files by using a
        wildcard in the wrong directory, or by using a wildcard that’s
        too broad.</p>
      <p>For example, suppose you want to run a <code>sed</code>
        command on all HTML files in a directory, so you run this:</p>
      <pre class="wp-block-code"><code>$ sed --in-place 's/day/night/g' *ml</code></pre>
      <p>Job done, until you find out that you accidentally ran that
        command on all your XML files, too.</p>
      <p><strong>Lesson learned:</strong> Run a safe test command on the
        wildcard you <em>think</em> you want to target before making a
        change. Some commands have a literal <code>--dry-run</code>
        option. Others have an <code>--interactive</code> option that
        forces the command to prompt you to confirm that you want to
        carry out the action. Sometimes the logic is reversed: a command
        refuses to make a major change unless you use a command (for
        example, <code>sed</code> doesn’t write changes to a file
        without the <code>--in-place</code> option or redirection).</p>
      <p>When in doubt, improvise. You can always “expand” a wildcard
        using the <code>echo</code> command:</p>
      <pre class="wp-block-code"><code>$ echo ./*ml
./four.html ./one.xml ./three.html ./two.xml
$ echo ./*tml
./four.html ./three.html</code></pre>
      <h2 class="wp-block-heading">3. File paths</h2>
      <p>Many new terminal users don’t understand where files are
        located within the file system. It’s not a common mistake to
        make on the desktop because there are visual reminders there.
        You wouldn’t try to double-click on a document to open it if
        there was no icon to double-click. It’s easy to assume that the
        terminal application contains all your files all at once, but
        the terminal is, by design, limited in scope. Were the terminal
        to have access to every file everywhere on your system all at
        once, you’d end up accidentally renaming and moving and copying
        a lot more files than intended. Specificity is a super power,
        and it’s defined by the <em>file path</em>.</p>
      <p>A file path describes where a file exists in a file system. A
        full (or “absolute”) file path always starts from the single
        folder at the start of your operating system, indicated by just
        a <code>/</code>, and then lists each folder within that folder
        until it traces the path to a specific file. For example, I have
        a file called <code>IMG_0001.JPG</code> in my <code>Pictures</code>
        directory. You probably have a mental image of where that file
        is and how you’d get there on the desktop. But for the terminal
        to understand how to find it, the location must be expressed as
        <code>/home/seth/Pictures/IMG_0001.JPG</code>.</p>
      <p>An absolute file path is definitive. The terminal always
        understands an absolute file path, no matter what your current
        working directory is.</p>
      <p>The absolute path to a file can be unwieldy, though. Once you
        understand absolute paths, you can abbreviate any path to a <em>relative</em>
        file path.</p>
      <p>A relative file path is based on your current location in the
        terminal. As long as you’re in the <code>Pictures</code>
        folder, the full path <code>/home/seth/Pictures/IMG_0001.JPG</code>
        can be shortened to just <code>IMG_0001.JPG</code>, or <code>./IMG_0001.JPG</code>
        for added clarity (the <code>.</code> indicates no movement
        from your current location, and the <code>/</code> is a
        directory separator as usual).</p>
      <p>But suppose your current working directory was your home
        directory. Your <code>Pictures</code> folder is located in your
        home directory, so to get to <code>IMG_0001.JPG</code> you have
        to enter <code>Pictures</code> first. The relative path in that
        case is <code>./Pictures/IMG_0001.JPG</code> or just <code>Pictures/IMG_0001.JPG</code>.</p>
      <p><strong>Lesson learned:</strong> An absolute file path always
        starts from the start of a file system. A relative file path
        changes based on your location. The terminal understands both.
        For new users, the absolute file path is the most explicit and
        exact way to reference a file, so practice using them until
        you’re comfortable with the concept of file paths.</p>
      <h2 class="wp-block-heading">4. Executable permissions</h2>
      <p>By default, most files aren’t executable. You can’t run them
        like an application, because most files are meant to be opened <em>in</em>
        an application. That’s not true for shell scripts, though.</p>
      <p>Shell scripts are text files containing a list of commands, and
        they’re meant to be run like an application. They’re a powerful
        way to string existing commands together to form a new custom
        command. However, because a shell script starts out as a regular
        text file, it’s not seen by your terminal as an executable
        entity.</p>
      <p>To execute a file as an application, you can grant it
        executable permission with the <code>chmod</code> command:</p>
      <pre class="wp-block-code"><code>$ chmod +x ./example.sh</code></pre>
      <p>Alternatively, you can run the file in a sub-shell:</p>
      <pre class="wp-block-code"><code>$ bash ./example.sh</code></pre>
      <p>Notice that in these examples, I use the <code>./</code>
        notation as if the <code>example.sh</code> shell script exists
        in my current directory.</p>
      <h2 class="wp-block-heading">5. Typing errors</h2>
      <p>It sometimes feels like the more you type, the more you’re
        getting done. In a terminal, though, typing too much is one of
        the best ways to introduce mistakes.</p>
      <p>When you try to type a long and complex command, you’re liable
        to spell something wrong or use the wrong option. When you try
        to type a filename or a file path, you might forget to escape
        special characters (like spaces). The errors aren’t usually
        catastrophic, but they’re frustrating and time consuming.</p>
      <p><strong>Lesson learned:</strong> There are several ways to
        ensure you’re entering the correct commands into your terminal:</p>
      <ul class="wp-block-list">
        <li><strong>Copy and paste:</strong> If you’re using a command
          you found on a trusted website, copy it in your browser and
          then paste it into your terminal using <strong>Ctrl</strong>+<strong>Shift</strong>+<strong>V</strong>
          or right-click and select <strong>Paste</strong>.</li>
        <li><strong>TAB:</strong> You can type part of a command or file
          path, and then press the <strong>TAB</strong> key on your
          keyboard for either auto-completion or for suggested
          completions. Use it even when you don’t think you need it.
          It’ll save you errors <em>every single time</em>, even when
          it appears to not work (hint: it’s not working because you’re
          trying to auto-complete something that’s not where you think
          it is).</li>
        <li><strong>Drag-and-drop:</strong> It’s the 21st century! You
          can drag a file or folder from anywhere on your computer, and
          drop it into your terminal. It gets replaced by its absolute
          path automatically.</li>
      </ul>
      <h2 class="wp-block-heading">Practice makes perfect</h2>
      <p>To get good in the terminal, you have to use it as often as you
        can. You don’t have to use it for “serious” work at first, and
        you arguably shouldn’t, but you can and should do simple
        exercises in the terminal. Understand file paths, get used to
        wildcards, learn shortcuts, use the <strong>TAB</strong> key.
        The biggest mistake you can make when learning the terminal is
        to not use the terminal, so open it up every day, do your
        exercise, and you’ll be an expert in the terminal in no time.</p>
    </div>