Getting Started with Shell + 5 Common Commands

An Introduction to Shell for Beginners

Wafiq Syed
Towards Data Science

--

Photo by Goran Ivos on Unsplash

Using shell can be intimidating. It’s understandable to prefer tools with pretty user interfaces. Before we look at shell, what is shell exactly?

Defining Shell

Let’s keep it simple, by starting with how we use computers. Think of your computer as a robot that’s waiting for instructions. For most of your needs, you use applications to get the computer to perform actions. For example, right now you are using an Internet Browser application (like Google Chrome) to read this article (unless you’re on the Medium mobile app, which I definitely recommend downloading). The application is your method of communication with the computer. In other words, we don’t talk to the computer and tell it verbally “Open the internet browser and go to Medium.” We get the application to do it. Applications serve an important role — they translate what we want to do into understandable instructions for the computer. Different applications serve different roles. Google Chrome is used to browse the internet, Microsoft Word to type essays, and Finder/File Explorer to access files.

Shell is a direct way for us to communicate with the computer and make it perform actions. In technical speak, it is a command interpreter and programming language. Instead of going through an application like Finder to view your documents, you can use shell to directly instruct the computer to show your documents. This direct channel of communication with the computer gives us more power over what we want the computer to do.

Looking at Shell

Now back to our point about shell being intimidating. Let’s look at an example.

Deleting a File on Finder vs. Shell

Say you want to delete a file called myFile . Doing this on Finder (or File Explorer for PC) is easy and intuitive. You go to Finder, find myFile, right-click on the file, and select Move to Trash/Delete. Simple.

Deleting a File Using Finder

When using the command line, it can seem more difficult and frightening, especially for first-time users. On a Mac, open Terminal (press command and the spacebar, and type Terminal, then press Enter). To delete myFile, you type code to go to your Documents, and then code to view your Documents to make sure myFile is there, and finally delete myFile with more code. See below.

Deleting a File Using Terminal

If you’ve never opened the command line interface before (the command line interface is the program that you type shell commands into — Terminal on Mac or Command Prompt on Windows), you’re probably intrigued at the gif above. Or intimidated. Or both. Here are three reasons why users prefer avoiding shell :

  • You need to know commands. On Finder you can use your mouse to navigate, but the command line interface only accepts text input through your keyboard. This requires memorizing the necessary commands. In our example above, rm is the command to delete a file (rm is short for remove).
  • It’s easier to mess up. Using shell gives you more control over tasks. So it’s understandable to fear typing the wrong text, and doing something wrong, like deleting a bunch of files.
  • There are applications that are easy to use (Finder/File Explorer, FileZilla, etc.)

You might already be convinced to avoid using the command line interface. But shell proves to be really powerful.

Why Use Shell?

Most Developer Roles Require Knowledge of Shell

If you’re pursuing any job that involves coding (software developer, data engineer, data scientist, etc), you’re likely going to use shell at some point in your career. Getting familiar with using shell will increase your productivity and show your team that you’re experienced.

For many developer jobs, if a candidate doesn’t know how to use shell, it’s reasonable to conclude that they don’t have much experience. As a data engineer/scientist, I’m required use shell every single day.

Shell allows you to Automate Repetitive Tasks

There are many tasks you can do on the command line that aren’t as easy to do using standard applications like Finder. Let’s say you’re researching COVID-19 cases in Canada. To get the CSV file with the daily updated number of cases, you need to go to Canada’s Public Health website and download the CSV file. If the file is updated every day at 6 PM, then you need to get the updated file daily. This means every day after 6 PM, you need to go to the website and download the file.

Downloading CSV File of Covid-19 Cases from Canada Public Health

Compare this to downloading the file in Shell. All you need to do is open Terminal, and type the following:

curl -O https://health-infobase.canada.ca/src/data/covidLive/covid19.csv

Let’s break down the code.curl is the command to download a file from a link (curl is short for Client URL). -O is an optional flag (we’ll explore that soon) that tells the computer we want to save the file in the original name (O for Original). And finally we pass the download link. To summarize, we’re telling the computer to download the file at the given link, and save the file with its original name, covid19.csv.

Using Shell to Download a File Link

That was quick! No need to open an internet browser, go to the link, and then scroll to the download button. Just type one line and you’re done. You might be thinking, it’s really not a big deal to go and download the file from the website. However, if your research requires you to download the file daily, you could use write a simple shell script (just once) that automates this daily task. So every day at 6:05 PM (or any time we specify in the script) the new daily file is in your Downloads folder. This already saves a couple of minutes of your day. Now imagine how much time you would save if you have multiple files you need to download daily.

There are numerous additional benefits to using shell. But let’s explore shell and briefly understand how to use it.

Quick Intro to Shell Commands

Shell commands are used in the following manner:

command [-flag] [parameter]
  • Command is the action you want to take. For example, the ls command means to list the contents of a folder. We’ll come back to ls shortly.
  • [-flag] is an optional detail you can specify. Note, you don’t actually type the square brackets. The square brackets indicate that this part of the code is optional. For example, by default, thels command doesn’t list files that start with a dot. Same goes for your Finder. To list these files, type ls -a (-a meaning list all files).
  • [parameter] is the entity (path/file/link) that you want to perform your command, or action, on. Notice the square brackets, meaning the parameter part is optional. For ls, by default, it’ll list the contents of the folder you’re in. You can tell which folder you’re in by reading the text before the % sign. In this case, we’re in Documents. If you want to list the contents of a different folder, you need to pass the folder name as a parameter. Say we had a folder insider our Documents called “StayPositive”. To see what’s inside the StayPositive folder, pass StayPositive as the parameter: ls StayPositive.
5 reasons to StayPositive :)

5 Commands to Know

You don’t need to master using the command line interface. If you try to learn it, you’ll get overwhelmed. Plus, you’ll most likely forget commands if you don’t use them frequently. Here are five shell commands used often that you should know.

ls (List)

Short for List, ls is the command to list a directory. If you type ls and press Enter, you’ll get a list of files stored in the folder you’re in.

cd (Change Directory)

cd command is used to change the directory you’re looking at. This is like clicking on a directory/folder using your mouse in Finder to look at its contents. For example, cd Documents will take you to your Documents directory. Using the previous command, if you type ls you can list what’s in your Documents.

cd and ls

man (manual)

Whenever you don’t know how to use a command, or forget which flags to pass, use man. man will show you the help manual of a command that you pass as a parameter. Here we use man to list out how to use ls. Note, you can scroll through the manual by using your mouse, or pressing the spacebar. Every time you press space, the page will scroll down. When you’re ready to leave the manual, press q for quit.

rm (remove file)

rm is used to delete a file. It’s important to note that rm will get rid of the file completely, not move it to Trash. To use rm, include the name of the file as a parameter. So to remove myFile, type rm myFile.

mkdir (make directory)

mkdir will make a directory/folder. Pass the name of the directory as a parameter. To create myFolder, type mkdir myFolder.

Next Steps to Learn Shell

While this post introduces you to shell and common commands, I recommend taking an introductory course on shell to get a better understanding. You can find a bunch online. Just google “learn shell.” Two of my favourite shell courses are by DataCamp — Intro to Shell and Data Processing with Shell. Note, only the first chapter is free for these courses. And I’m in no way affiliated with DataCamp, I just really benefit from their courses due to the interactive exercises.

The fruits of your learning will show when you come across a task that you perform and want to automate it. Make sure to google how you can automate that task using shell. Chances are, someone has already asked the same question and got it answered. You’ll be off to automating tasks that you never knew were so easy to automate. Till next time, happy learning!

--

--