Basic Command Line

Intro

  • We’ll be using the command line interface (CLI) for parts of this class

    • A.K.A. terminal, console, shell, etc.

  • CLI is the minimal connectivity you may have on a machine

    • Typically servers don’t have a UI (unnecessary resource usage)

  • This lesson will cover basic commands (pre-git stuff)

  • Focus will be on Linux/Unix commands for relevance across Mac, Windows, and Linux

Software

  • CLI Applications

    • Windows: Git Bash

    • Mac: Terminal (or iTerm2, etc.)

    • Linux: Terminal (or other terminal emulators)

  • The sub-sections here will also guide on installation for future lessons

Mac

Carefully follow the directions in the output especially in the final steps

Linux

  • Like Mac, you’ll probably use the built-in terminal, but there are others you can install

    • Use apt-get install git or the valid steps per your distro’s package manager

Windows

  • We won’t be using CMD/PowerShell since they implement their own commands

    • Won’t be using Windows Subsystem Linux (WSL) either

  • Git bash from https://gitforwindows.org/ or https://git-scm.com/downloads

    • You may get the portable version and install it to a flash drive if you wish

    • It’s recommended to use the default install location so it plays nicely with VS Code

    • When installing, it’s highly recommended to enable "show in context menu"; this saves a lot of hassle later (same for VS Code)

Understanding Paths

This is a crucial, yet basic, topic: how to define where something is.

  • We have two ways:

    • Refer to its relative path

    • Refer to its absolute path

Relative path is the relation to where we are currently (e.g., MyFolder/MyHomework.txt)

There’s no leading /

Absolute path is the relation from the root of the system (noted as / or C:/, etc)

(e.g., /c/Users/Me/My Documents)

The leading / makes the difference since / is root; this gives us the exact path to the file or folder.

Keep relative and absolute paths in mind for the next slides.

Commands

Where am I?: pwd

Command: pwd

What’s it do? Prints (outputs) the working directory path to the console

The working directory is the location you’re…​doing work. This will show the absolute path to the current directory.

It doesn’t stand for “password" in this case.

Where to now?: cd

Command: cd

What’s it do? Changes the directory (this is how we travel in and out of folders)

Let’s say pwd is /c/Users

The following command cd .. would go up a directory

cd .. moves up one directory, but cd …​ doesn’t exist, instead to go up two directories, you would do cd ../..

If you run cd ., you go nowhere since . is the current directory

Assuming pwd is /c/Users and there’s a folder called Me, then cd Me would bring me to /c/Users/Me.

You can traverse the filesystem with relative or absolute paths, but how do we know where to go?

Finding Our Path: ls

Command: ls

What’s it do? It’s short for "list"; it shows files and/or folders in the directory you pass as an argument.

Examples:

  • List files in the current directory: ls or ls .

  • List files in a specific directory: ls MyFolder

  • List files in the parent directory: ls ..

By default, hidden files won’t be visible. A folder or file is hidden if it begins with a . in the file name (we’ll see .gitignore in the future)

If you want to see hidden files in the current directory, -a must be appended to the command:

ls -a

Let’s Move: mv

Command: mv

What’s it do? It has two purposes: Moving a file or renaming a file

Be very careful with paths and names

This command takes two arguments
source and destination

Examples:

  • Move a file up a directory: mv myfile.txt ..

  • Rename a file: mv myfile.txt myfile.txt.backup

Let’s Copy: cp

Command: cp

What’s it do? It creates a copy of a file (or folder if passing -r as an argument)

Be very careful with paths and names

Like mv this command takes two arguments
source and destination

Examples:

  • Make a backup of a file: cp MyImportantFile MyImportantFile.backup

  • Make a copy of a folder: cp -r MyFolder MyFolder2

-r means recursive and is usually required when commands deal with folders

When moving critical items, it’s best to use the cp command instead of the mv command. In case something goes wrong, you should still have the original.

Cleanup: rm

Command: rm

What’s it do? It deletes a file (or folder if you pass -r)

This is a very dangerous command, and recovery is either impossible or very difficult; be extremely careful with paths

Examples:

  • Delete a file: rm MyFile.txt

  • Delete a folder and its contents: rm -r MyFolder

(Dangerous) Force delete: rm -f MyProtectedFile (typically this will require another part of the command in some cases)

(Even more dangerous) Recursively force delete: rm -rf some/path/to/remove

I say so: sudo

Command: sudo

What’s it do? It’s not a standalone command, but it elevates the current user to have superuser/root permissions

This can be dangerous if you don’t know what you’re doing

Linux/Mac Users: This is a common command used to run commands with elevated permissions. It stands for "superuser do" and allows you to execute commands as the superuser (root) or another user.

Windows Users: Although gitbash/etc. gives some Linux commands, we shouldn’t need to attempt to use sudo in Windows. Instead, we can run gitbash as an administrator if needed.

As of Windows 11, sudo is now available as a native command

File Editing: nano, vi

Commands: nano or vi

What’s it do? Both of these are command line text editors. vi/vim is default on most systems and has a bit of a learning curve. For now, if you need to exit vi, just do :q. It’s recommended to stick to nano for quick edits since it’s more beginner-friendly for the class.

Nano is to vi as notepad is to notepad++ (or some extended text editor)

Examples:

  • To edit/create a file with nano: nano MyFile.txt

  • To edit/create a file with vi: vi MyFile.txt

Creation happens upon saving the file as long as the path is valid

To show line numbers in nano: nano -l Myfile (that’s L for line)

Quick File Creation: touch

Command: touch

What’s it do? It creates an empty file without opening an editor (or it updates the modified timestamp of a file)

Examples:

  • Create an empty file: touch MyFile.txt

  • Update the timestamp of an existing file: touch ExistingFile.txt

Quick Output: cat

Command: cat

What’s it do? It’s typically used for concatenation, but it can be used to output the content of a file to the console without opening a text editor

Example:

  • cat MyFile.txt

We’ll use this later when generating SSH keys to output the public key

Summary

  • You should now have a basic understanding of the following:

    • How to navigate the command line

    • Create and manipulate files

    • Understand paths

  • These are all common across Linux-based systems or systems that emulate these commands (like Windows with gitbash)

  • Memorize these basics, and you’ll be set for most scenarios in future classes and the job world