Introduction to the Command Line#

Overview

Questions:

  • What is the command line?

  • How can I navigate files and directories on the command line?

Objectives:

  • Learn basic shell commands for navigating and creating directories.

Lesson Slides#

Lesson Slides

Opening the Terminal#

In this course, we will be navigating files and using git (a software for version control) using the command line. The Linux command line is a text interface to your computer. When you use the command line, you use something called a shell. You can access the command line or shell using a terminal. If you are using WSL, the only interface to your Linux operating system is a command line or terminal.

In scientific computing, you often need to use the Linux command line on high-performance computing (HPC) servers. Knowing the command line will allow you to perform repetitive tasks quickly through shell scripting. You will learn more about the command line and scripting in Chem 274A. This course will focus on basic navigation, file creation, and using git from the command line.

Most modern operating systems have graphical user interfaces, or GUIs (often pronounced “gooey”), used to interact with the computer. However, you can also interact with the computer using text only.

Open your terminal. If you are using a Mac, you should open your terminal application. If you are using WSL, open your Linux distribution. You can use this interface to issue commands to your computer using text.

Viewing Directory Contents#

The first command we will discuss is the command pwd. pwd stands for “print working directory.” This command gives the name of the folder you are currently in. In Linux, “directory” means the same thing as “folder.”

pwd
/YOUR/PATH

When you open a terminal initially, you will be in your home directory. The path displayed as your output will be whatever your home directory is if you type pwd immediately after opening your terminal.

The ls command shows you the contents of the directory you are in. ls stands for “list,” and the command shows you the contents of the directory you are in.

ls

If you want to see the contents of another directory, you can follow ls with the path to that directory. In the command below, you should substitute a directory you can see from the previous ls command.

ls DIRECTORY_NAME

Clearing the screen

If you’d like to make room on your screen, you can use the clear command to get a fresh terminal. Pressing ctrl+L on your keyboard will also clear the screen.

The tab key

When typing a file or folder name into your terminal, you can autocomplete the name by pressing the tab key.

For example, if you have a folder called my_folder, you can type my_f and then press tab. If there is only one folder that starts with my_f, the terminal will complete the name for you. If there are multiple folders that start with my_f, pressing tab twice will show you the options.

Creating and navigating directories#

We will make a directory to keep our work in for the course. These directions will tell you how to create a folder in your home directory for the sake of uniformity. If you have another preference for where you would like to store your files and you can navigate files, you can use that location.

The command to make a directory is mkdir.

mkdir chem_280

Check Your Understanding

What command have you learned so far that you could use to see if your newly created folder is in your current location?

This has created an empty folder, or directory, named chem_280 in your home directory. To navigate inside that directory, we need to change the directory using the cd command.

Spaces in file and directory names

In general, you will notice that most file and directory names created on systems where the command line is used do not contain spaces.

On the command line, many applications and scripts may not work with file names or directories containing spaces. Underscores _ or dashes - are used in place of spaces to separate words in file names.

cd chem_280

We will create a file describing what is in this folder.

You can verify what folder you are in using the pwd command.

pwd

Open a text file in your text editor of choice. If you installed VSCode  , you will be able to open a file called README.md using VSCode with the following command. Note If you are on MacOS, you will need to add VSCode to your path manually.

code README.md

The Visual Studio Code text editor will open with a file called README.md. Type some information in the file and save it.

# Chem 280
This folder contains files and directories associated with Chem 280 - 
Foundations of Programming and Software Engineering.

This file uses Markdown, a text formatting language. Markdown is often used for README files, and we will use it throughout this course and the following courses. The hashtag (#) followed by the space results indicates a title. Save this file and exit.

Now, when you type ls, you will see that a file called README.md is in your directory.

To navigate out of this folder, you can use .. as the file location.

cd ..

The command above will move you back to your home directory.

Changing to the home directory from anywhere

If you use the cd command followed by no path, you will always be returned to your home directory.

Other commands that will be useful to you are mv, which is used for moving files from one place to another, and cp for copying files from one place to another. For example, you can create a copy of your README.md file:

cp chem_280/README.md .

This command will create a copy of the README.md file in your current directory. The dot (.) is a shortcut for your current directory. In this case, the created file will have the same name as the one you copied it from. You could also give the file another name:

cp chem_280/README.md README_copy.md

The mv command behaves similarly, except the original file is removed.

You can remove a file using the rm command. Let’s get rid of those copies we just made:

rm README.md
rm README_copy.md

Challenge

Navigate to your chem_280 directory and list the directory contents.

Key Points

  • The terminal or “command line” is a text interface to your computer.

  • You can use text commands in the command line to navigate directories and change files.

  • We will use a text mark-up notation called “markdown.” This will be in text files and doesn’t directly involve the terminal.