Skip to content

Master Terminal Commands: Effortlessly Navigate the Command Line

[

The Terminal: First Steps and Useful Commands

The terminal can be intimidating to work with when you’re used to working with graphical user interfaces (GUIs). However, it’s an important tool that you need to get used to in your journey as a Python developer. And once you level up your skill of using the terminal, it becomes an extremely powerful tool in your repertoire. With just a few commands in the terminal, you can do tasks that are impossible or at least very tedious to do in a GUI.

In this tutorial, you’ll learn how to:

  • Find the terminal on your operating system
  • Open the terminal for the first time
  • Navigate your file system with basic commands
  • Create files and folders with the terminal
  • Manage packages with pip commands
  • Keep track of your files with Git in the terminal

If you’re new to working with the terminal, or you’re looking to expand your understanding of its capabilities, then this tutorial is a great starting point. In it, you’ll get an introduction to some of the basic commands and learn how to use pip and Git to manage your projects in the terminal.

Understanding how to integrate the terminal, pip, and Git into your workflows is essential for you as a Python developer. However, it’s important to note that you’ll only scratch the surface of what the terminal can do, and there’s much more to learn as you continue to explore the terminal as an essential development tool.

Install and Open the Terminal

Back in the day, the term terminal referred to some clunky hardware that you used to enter data into a computer. Nowadays, people are usually talking about a terminal emulator when they say terminal, and they mean some kind of terminal software that you can find on most modern computers.

Note: There are two other terms that you might hear now and then in combination with the terminal:

  1. A shell is the program that you interact with when running commands in a terminal.
  2. A command-line interface (CLI) is a program designed to run in a shell inside the terminal.

In other words, the shell provides the commands that you use in a command-line interface, and the terminal is the application that you run to access the shell.

If you’re using a Linux or macOS machine, then the terminal is already built in. You can start using it right away.

On Windows, you also have access to command-line applications like the Command Prompt. However, for this tutorial and terminal work in general, you should use the Windows terminal application instead.

Read on to learn how to install and open the terminal on Windows and how to find the terminal on Linux and macOS.

Windows

The Windows terminal is a modern and feature-rich application that gives you access to the command line, multiple shells, and advanced customization options. If you have Windows 11 or above, chances are that the Windows terminal is already installed on your system.

To open the Windows terminal, follow these steps:

  1. Click on the Start menu.
  2. Search for Windows Terminal and select it from the search results.

The Windows terminal will open, and you’ll see a blank shell where you can start running commands.

Linux

On Linux, the terminal application is typically available by pressing Ctrl+Alt+T on your keyboard. Alternatively, you can search for Terminal in your application launcher.

Once you open the terminal, you’ll see a shell prompt where you can start typing commands.

macOS

On macOS, you can open the terminal application by pressing Cmd+Space to open Spotlight search. Then type Terminal and press Enter.

The terminal application will open, and you’ll see a shell prompt where you can start entering commands.

Learn Basic Terminal Commands

Now that you have the terminal open, it’s time to learn some basic commands that will help you navigate your file system and perform tasks.

To navigate your file system in the terminal, you’ll use the cd command, which stands for “change directory”. This command allows you to move between different folders on your computer.

Here are some examples of how to use the cd command:

  • To navigate to the home directory, which is the default directory when you open the terminal, simply type cd and press Enter.
  • To navigate to a specific directory, type cd followed by the path to the directory. For example, to navigate to the Documents directory, you would type cd Documents and press Enter.
  • To navigate up one level in the directory hierarchy, use the cd .. command. For example, if you are currently in the Documents directory and want to move up to the parent directory, you would type cd .. and press Enter.

Create Files and Folders

In addition to navigating your file system, you can also use the terminal to create files and folders.

To create a new folder, you can use the mkdir command, which stands for “make directory”. Here’s how you can create a new folder called new_folder:

mkdir new_folder

To create a new file, you can use the touch command. This command creates an empty file with the specified name. For example, to create a new file called new_file.txt, you would type:

touch new_file.txt

You can also combine the mkdir and touch commands to create a new folder and file inside the folder in a single command:

mkdir my_folder && touch my_folder/new_file.txt

Manage Packages With pip

The pip command is a powerful tool that allows you to manage Python packages and install third-party libraries. In this section, you’ll learn how to use some basic pip commands in the terminal.

Create a Virtual Environment

A virtual environment is a self-contained Python environment that allows you to install packages and dependencies without interfering with your system-wide Python installation. It’s a best practice to use virtual environments for your Python projects to ensure that each project has its own isolated environment.

To create a virtual environment, you can use the following command:

python3 -m venv myenv

This command creates a new virtual environment called myenv. You can replace myenv with the name you want for your virtual environment.

After creating the virtual environment, you can activate it by running the appropriate command for your operating system:

  • On Windows: myenv\Scripts\activate.bat
  • On Linux and macOS: source myenv/bin/activate

Install a Package

Once you have a virtual environment activated, you can use the pip install command to install packages from the Python Package Index (PyPI) or from an alternate package index.

To install a package, simply type pip install followed by the name of the package. For example, to install the requests package, you would type:

pip install requests

You can also specify a specific version of a package by appending the version number to the package name. For example, to install version 2.0.0 of the requests package, you would type:

pip install requests==2.0.0

Interact With Git

Git is a version control system that allows you to track changes to your code and collaborate with other developers. In this section, you’ll learn how to use some basic Git commands in the terminal to initiate a Git repository and track files.

Initiate a Git Repository

To initiate a Git repository in your project folder, navigate to the project folder in the terminal using the cd command. Then run the following command to initialize a new Git repository:

git init

This command creates a hidden .git folder that contains all the necessary files to track changes to your project.

Track Files With Git

Once you have a Git repository set up, you can start tracking files in your project. To track a file, use the git add command followed by the file name. For example, to track a file called script.py, you would type:

git add script.py

You can also use the git add . command to stage all files in the current directory and its subdirectories.

After adding files, you can use the git commit command to save the changes to the repository:

git commit -m "Initial commit"

This command creates a new commit with a message that describes the changes made.

Next Steps

Congratulations! You now have a basic understanding of how to use the terminal, pip, and Git to perform common tasks as a Python developer. This tutorial has provided you with a starting point to explore the full capabilities of the terminal and its potential to enhance your workflow.

To further expand your knowledge, here are some suggested next steps:

  • Explore more terminal commands and practice using them.
  • Learn about advanced pip commands and options.
  • Dive deeper into Git and learn about branches, merging, and advanced Git workflows.
  • Experiment with customizing your terminal environment.

Conclusion

In this tutorial, you learned how to find, install, and open the terminal on different operating systems. You also explored basic terminal commands for navigating the file system, creating files and folders, managing packages with pip, and interacting with Git.

Using the terminal is an essential skill for Python developers, as it allows you to work more efficiently and perform tasks that are not possible or difficult in a graphical user interface. As you continue your journey as a Python developer, be sure to explore more terminal commands and discover how you can incorporate the terminal into your development workflow.