Mastering the Command Line: A Beginner's Guide

Introduction

The command line, also known as the terminal, console, or shell, is a powerful text-based interface for interacting with your computer's operating system. While graphical user interfaces (GUIs) are intuitive for many tasks, the command line offers efficiency, automation capabilities, and access to functionalities not always available through a GUI. For data scientists, web developers, and system administrators, command line proficiency is an invaluable asset.

What is the Command Line?

When referring to this, you may have heard the terms Terminal, console, command line, CLI, and shell. Differentiating each isn't necessarily crucial knowledge to have, but it will help clarify things. In plain English, it's your computer screen, keyboard, and mouse.

Terminal

A terminal is a text input and output environment. Keep in mind the terminal is a program, just like any other. And like any program, you can install it and uninstall it as you please.

Shell

A shell is a program that acts as a command-line interpreter. It processes commands and outputs the results. Different shells come with different syntax and characteristics as well. In most Linux and Mac operating systems the default shell is Bash. While on Windows it's Powershell. Shells work also as programming languages, in the sense that with them we can build scripts to make our computer execute a certain task. We'll take a look at scripts later on in this article. Also keep in mind that the terminal is the program in which the shell will run. But both programs are independent. That means, I can have any shell run on any terminal.

CLI

The CLI is the interface in which we enter commands for the computer to process. We just mentioned that most operating systems come with a GUI. The first reason is that for many tasks, it's just more efficient. We'll see some examples in a second, but there are many tasks where a GUI would require many clicks around different windows.

Read also: Learn Forex Trading

Why Learn the Command Line?

  • Efficiency: Many tasks can be performed more quickly and efficiently using command line commands compared to navigating through graphical interfaces.
  • Automation: The command line allows you to automate repetitive tasks by writing scripts.
  • Remote Access: The CLI is often the primary or only way to interact with remote servers, cloud platforms, and embedded systems.
  • Version Control: Tools like Git, essential for collaborative software development and data science projects, are primarily used through the command line.
  • Ubiquity: While graphical tools come and go, the core concepts and commands you learn in a Unix shell will remain relevant and useful for decades to come. That's because most shells have a common foundation - once you know one, it's relatively easy to pick up others.
  • Resource Optimization: Command line tools are often more lightweight and consume fewer resources than their graphical counterparts, making them ideal for resource-constrained environments.

Overcoming the Challenges of Learning the Command Line

Mastering the command line can be tough for many new learners. The command line lacks the visual cues of a graphical interface. Remembering the wide range of commands and options is part of the steep learning curve. Many educational programs gloss over foundational command line skills, which leaves students unprepared for real-world tasks.

Many command line learning resources fall short for beginners. When I first started exploring these materials, I often felt they were disjointed. I found myself completing random exercises without understanding how they related to the data science work I wanted to do. My motivation for mastering data science took a big hit when I had to learn command line basics. Engaging with commands like cd and ls, which seemed arbitrary at the time, only added to my frustration. It was only later that I realized how important they are for daily data science tasks.

Like was my experience, many command line tutorials don't cater well to beginners. They tend to focus on memorizing commands and syntax without providing enough practical exercises or real-world applications. Additionally, these resources often assume prior knowledge. They overwhelm learners with technical jargon and complex explanations that don't follow a logical progression. I remember one tutorial in particular that I got "stuck" in because I didn't know how to quit a program running from the command prompt. I had to keep closing the entire command prompt and restarting it each time I wanted to quit. Maybe silly in hindsight, but at the time it was so frustrating and could have been avoided by a simple sentence explaining how to quit.

To sum it up, effective command line education should prioritize clarity, practical examples, and a structured approach.

Getting Started: Essential Concepts and Commands

Accessing the Command Line

  • Linux: Open the programs menu and search for “Terminal”. It's probably under Applications → Accessories → Terminal, or Applications → System → Terminal, but that may depend on your system.
  • macOS: Open your Applications > Utilities folder and find “Terminal”. You can also use Spotlight search to open Terminal. Press Cmd + Space to open Spotlight, and search for “Terminal”.
  • Windows:
    • Go to the Start screen, hover your mouse in the lower-left corner of the screen, and click the down arrow that appears (on a touch screen, instead flick up from the bottom of the screen). The Apps page should open.
    • Hold the special Windows key on your keyboard and press the "X" key.
    • Hold the Windows key and press the "R" key to get a "Run" window.

The window that opens will be mostly blank, with the exception of some text that will vary based on your operating system. On Linux and older Macs, the line will end with $ and on newer Macs, the line will end with %. This symbol - called the prompt - indicates that the terminal is waiting for you to enter a command.

Read also: Understanding the Heart

Basic Navigation

  • pwd (print working directory): Displays the current directory you are in.
  • ls (list): Presents you the contents of the directory you're currently in.
    • ls -a: It will also show you hidden files or directories.
  • cd (change directory): Change the current directory to DIR. The variable CDPATH defines the search path for the directory containing DIR.
    • cd ..: Moves you up one directory (to the parent directory). Where will cd .. take you?
    • cd ~: Returns you to your home directory.
    • cd directory_name: Navigates into the specified directory.

File and Directory Manipulation

  • mkdir directory_name: mkdir stands for make directory and it will create a new directory for you.
  • touch file_name: Allows you to create an empty file in your current directory.
  • cp (copy): cp [OPTION]… cp [OPTION]… SOURCE… cp [OPTION]…
  • mv (move): Is short for move, and lets us move a file or directory from one place to another.
  • rm file_name: Allows you to delete files, in the same way rmdir allows you to remove directories. Attention: Deleting files using del, rmdir or rm is irrecoverable, meaning the deleted files will be gone forever!
  • rmdir directory_name: Rmdir stands for Remove directory and it does just that.

Viewing File Content

  • cat file_name: Cat stands for concatenate and will return the full contents of the file that follows it.
  • less file_name: Allows you to view the contents of a file one screen at a time.

Getting Help

  • man command_name: macOS and Linux have a man command, which gives you help on commands. Try man pwd and see what it says, or put man before other commands to see their help. The output of man is normally paged. You can even enter man bash and that will return a huge manual about everything there's to know about this shell.
  • help command_name (Bash built-in commands): Provides help for built-in shell commands.
  • command_name /?: Adding a /? suffix to most commands will print the help page. You may need to scroll your command window up to see it all.

Important tips

  • By hitting tab you will get autocompletion based on the text you've written so far. For example if I write edit test and tab twice, I get testFolder/ test.txt. If I write edit test.
  • When using a command in the terminal that requires you to enter your password for authentication (such as sudo), you will notice that the characters aren’t visible as you type them.
  • When you’re inside the command line, use Ctrl + Shift + C (Mac: Cmd + C) to copy and Ctrl + Shift + V (Mac: Cmd + V) to paste.

Practical Exercises

  1. Navigation:
    • Create a directory named "practice" on your desktop.
    • Navigate into the "practice" directory.
    • Create a subdirectory named "test" inside "practice".
    • Return to your home directory.
  2. File Manipulation:
    • Create an empty file named "test.txt" in the "practice" directory.
    • Add the text "Hello, command line!" to "test.txt" using the echo command and redirection (>>).
    • View the contents of "test.txt" using the cat command.
    • Copy "test.txt" to a new file named "test_copy.txt".
    • Delete "test.txt".
  3. Command Exploration:
    • Use the man command to learn about the ls command and its various options.
    • Experiment with different options of the ls command, such as -l (long listing) and -t (sort by modification time).

Shell Scripting: Automating Tasks

As previously mentioned, we can build scripts with our shell and later on execute those scripts whenever we want. To explain how we can code one, we'll use a simple example that will allow us to create a Github repo by running a single command.

  1. First thing to do is create a .sh file. You can put it wherever want. On our first line, we'll write the following: #!. Remember previously when we mentioned that we can use a given shell for general interaction and another given shell for executing a script? As mentioned too, we're using a "stripped down" shell (also known as sh shells) to run the scripts as they're more efficient (though the difference might be unnoticeable to be honest, It's just a personal preference). If we wanted this script to run with bash the shebang would be #!.
  2. A parameter is a set of characters that is entered after the script/command. So we're expecting the repository name as parameter of our script. But what happens if the user forgets to enter it?
  3. Then it's time to upload our repo to github. And last we're using the -d flag to pass parameters to this command.
  4. Cool, we're almost done now! Then we're piping the return value of our request. Piping just means passing the return value of a process as the input value of another process. And finally we run the jq command, which is a tool for processing JSON inputs.
  5. And as last step, we rename our master branch to main, add the remote origin we just obtained, and push our code to GitHub! #!
  6. Now it's time to test our script! And that's it! We have our script up and running. But there's something a bit annoying about this. We need to remember the exact route of the script directory.
  7. To create a new alias, we need to edit the bash configuration files in our system. This files are normally located in the home directory. If you don't know what the absolute path of your script is, go to the script directory on your terminal and enter readlink -f newGhRepo.sh. That should return the full path for you. After we're done editing, we save our file, restart our terminal, and voilà! Now we can run our script by just entering newghrepo, no matter in what directory we currently are. Much quicker than opening the browser and clicking around to create our repo!

Choosing a Shell

Posix works for shells in a very similar way that ECMAScript works for JavaScript. This standard was established in the 1980's and most current shells were developed according to that standard. To know what shell you're currently running, just open your terminal and enter echo $0.

There's not A LOT of difference between most shells. Zsh is very similar to Bash, but it was created after it and comes with some nice improvements over it. Fish is another commonly used shell that comes with some nice built-in features and configurations such as autocompletion and syntax highlighting. The thing about Fish is that it's not Posix complaint, while Bash and Zsh are. This means that some of the commands you'll be able to run on Bash and Zsh won't run on Fish and vice versa.

There are also other shells like Ash or Dash (the naming just makes everything more confusing, I know…) that are stripped-down versions of Posix shells. This means they only offer the features required in Posix, and nothing else.

If had to recommend a shell, I would recommend bash as it's the most standard and commonly-used one. But again, truth is there's not A LOT of difference between most shells. So in any case you can try a few and see which one you like best.

Read also: Guide to Female Sexual Wellness

I just mentioned that Fish comes with built-in configuration such as autocompletion and syntax highlighting. The point is that shells are customizable. We won't see customization options in detail here, but know that when you install a shell in your computer, certain files will be created on your system. Also, there are many plugins available online that allow you to customize your shell in an easier way. You just install them and get the features that plugin offers.

If you're starting out, all this information can feel a bit overwhelming. But just know that there are many options available, and each option can be customized too.

Command Line for Data Science

Command line skills are the bread and butter of any data professional's toolbox. According to Stack Overflow's Developer Survey, Bash/Shell (aka "the command line") ranked as the 6th most popular programming language, just two places behind Python and SQL. It also correlated heavily with data science technologies like Jupyter notebooks, TensorFlow, and PyTorch.

Data Processing

Working with text files - a common task in almost any data science project - is also far easier in the command line.

Automation

Need to regularly acquire, process, and display data in the same way? With a few terminal commands, you can write scripts to handle the entire pipeline automatically.

Cloud Computing

And with the rise of cloud computing, command line proficiency is more important than ever.

Resources for Learning

  • Dataquest's Command Line course guides beginners through essential concepts like filesystem navigation and user management.
  • freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers.
  • Zach Gollwitzer has an awesome crash course series on youtube. He has also great tutorials on other topics such as Node and Javascript, so I recommend that you follow him.
  • The Unix Shell course designed by the Software Carpentry Foundation.
  • StackOverflow
  • Official documentation

tags: #learn #command #line #basics

Popular posts: