Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Command Line

reading/command_line.livemd

Command Line

Mix.install([
  {:jason, "~> 1.4"},
  {:kino, "~> 0.9", override: true},
  {:youtube, github: "brooklinjazz/youtube"},
  {:hidden_cell, github: "brooklinjazz/hidden_cell"}
])

Navigation

Home Report An Issue Computer HardwareGit

Review Questions

Upon completing this lesson, a student should be able to answer the following questions.

  • How do you use your command line to navigate the file system?
  • How do you use your command line to manipulate (create and remove) folders and files?

Overview

We use the command line to interact with our computer through a text interface rather than through a Graphical User Interface (GUI).

Many programs only run through the command line. Generally, we call these Command-Line Applications, CLI Applications (for Command Line Interface), or just CLIs.

The command line will differ depending on your operating system (Windows, macOS, GNU/Linux). Thus, we will include the commands specific instructions for each operating system.

Each operating system uses a different program for its command line. Follow the instructions for your operating system to open your command line.

  • Windows: Open the start menu with the Win key. Search for Command Prompt. Ensure you run in administrator mode to avoid permission issues.
  • macOS or GNU/Linux: Open the Terminal App Ctrl+Alt+T
  • macOS Click the Launchpad icon in the Dock or open the Launchpad with Command + Space. Type Terminal in the search field, then click the Terminal program.

For an overview of the Command-Line Interface, we recommend this excellent video by Crash Course Computer Science.

YouTube.new("https://www.youtube.com/watch?v=4RPtJ9UyHS0")

Current Working Directory

Your command line should display the current working directory. You’ll often see $ represent the current working directory in tutorials. Omit the $ when entering text into your command line.

For example, on Windows 11, mine is:

C:\Users\brook>

Your Turn

Open the command line on your computer and enter the whoami command to see your current user.

You should see a similar output.

$ whoami
your_user_name

Running Elixir Files

With Elixir installed we can use the command line to execute an Elixir file ending in .ex or .exs.

Elixir treats both file extensions similarly. However, we generally use .exs files for scripts.

Let’s create an Elixir script. First, create a script.exs file with the following content. This prints a "Hello, world!" message in your console.

IO.puts("Hello, world!")

Then execute the file from the command line. Any Elixir code inside of the file will execute.

$ elixir script.ex
Hello, world!

Permissions (sudo)

Some commands require permissions to enter. You should see an error in the command line if the command requires permissions.

In Windows you often need to run the Command Prompt or Powershell in administrator mode.

For macOS & GNU/Linux you can preface commands with sudo. You’ll be prompted for your computer’s password. You’ll notice that whoami returns a different value (probably root).

Root is the superuser account in macOS and GNU/Linux.

$ sudo whoami
root

List Files & Folders

Our computer stores files and folders. The terms folder and directory are interchangeable.

We use the following commands to list files and folders.

  • Windows: dir
  • macOS or GNU/Linux: ls

macOS and GNU/Linux hide specific files. For example, files starting with . are automatically hidden.

You can see hidden files with ls -a.

$ ls -a

On Windows, all files and folders should display by default.

C:\> dir

Your Turn

Use the command line the files and folders in the current working directory. If you are on macOS or GNU/Linux, use both ls and ls -a to see the difference.

Create Files

You can create a file using the following commands

  • macOS & GNU/Linux: touch
  • Windows: echo

macOS & GNU/Linux Allow you to enter touch with the name of a file.

$ touch ./example_file.txt

Windows requires you to enter the content into (>) the file.

C:\> echo example content > ./example_file.txt

On Windows, you can use type nul to create an empty file.

C:\> echo type nul > ./example_file.txt

./ represents the current working directory. You can generally omit it.

Here are the same commands without the ./.

$ touch example_file.txt
$ echo example content > example_file.txt

Your Turn

Using the command line, create a file called test.txt.

On macOS & GNU/Linux run:

$ touch test.txt

On Windows run:

C:\> type nul > text.txt

Create Folders

You can create a folder with mkdir on every operating system. It’s short for make directory.

Your Turn

Using the command line, create an example folder using mkdir.

$ mkdir example

Then use the command line to create a file called in_example.txt inside of the example folder.

On macOS or GNU/Linux, enter:

$ touch example/in_example.txt

On Windows enter:

C:\> echo type nul > example/in_example.txt

Navigating The File System

You can navigate the file system with cd on every operating system.

You navigate through files and folders the same way you would click through the file explorer.

Enter cd with the path of the folder to navigate to it. The folder must exist.

$ cd example_folder
$ cd first_folder/second_folder/third_folder

You can navigate up folders using ../. You can go up any number of folders by repeating ../.

$ cd ../
$ cd ../../../

Let’s walk through an example with several nested folders like so.

/top
  /folder_a
    /folder_b
      /folder_c
  /folder_1
    /folder_2
      /folder_3

We use cd to navigate between these folders and set the current working directory.

flowchart LR
top --> folder_1 --> folder_2 --> folder_3
top --> folder_a --> folder_b --> folder_c
style top fill:lightgreen

For example, we can cd folder_1/folder_2/ to set the current working directory to folder_2.

flowchart LR
top_level_folder --> folder_1 --> folder_2 --> folder_3
top_level_folder --> folder_a --> folder_b --> folder_c
style folder_2 fill:lightgreen

And use cd ../../folder_a/folder_b/folder_c to set the current working directory to folder_c.

flowchart LR
top_level_folder --> folder_1 --> folder_2 --> folder_3
top_level_folder --> folder_a --> folder_b --> folder_c
style folder_c fill:lightgreen

Your Turn

Using the command line, go into the example folder you created earlier. List all of the files in the folder using ls or dir. You should see the in_example.txt file from the previous exercise.

$ cd example

Remove Files

You can remove files using the following commands.

  • macOS & GNU/Linux: rm
  • Windows: del

Your Turn

Using the command line, delete the in_example.txt file from the previous exercise.

On macOS & GNU/Linux run:

$ rm example/in_example.txt

On Windows run:

C:\> del example/in_example.txt

Remove Folders

You can remove folders using the following commands.

  • macOS & GNU/Linux: rm -rf
  • Windows: rmdir

Your Turn

Remove the example folder from the previous exercise.

On macOS & GNU/Linux run:

$ rm -rf example

On Windows run:

C:\> rmdir example

Reading Files

You can read file contents with the following commands.

  • macOS & GNU/Linux: cat
  • Windows: type.

Your Turn

Create a new file read_example.txt with some text content. You can use echo on every operating system to create a file with some content. However, type nul only works on Windows.

run:

$ echo content > read_example.txt

Then read the content.

On macOS & GNU/Linux run:

$ cat read_example.txt

On Windows run:

C:\> type read_example.txt

Tab Completion

To save time, you can generally use the Tab button to autocomplete a command or filename.

Your Turn

Using the command line, create a new folder called tab_to_folder.

$ mkdir tab_to_folder

Navigate to the folder using autocompletion. Start typing tab then use the Tab key to autocomplete the folder name.

$ cd tab_to_folder

With Great Power

The command line is powerful. Therefore the command line is also dangerous. For example, a simple command can delete every file on your computer.

Open the dropdown below to see the command. You should never run it.

DANGER DO NOT RUN rm -rf *

So be careful about running commands on your command line.

Summary

This lesson is a brief primer on the command line. Here’s a quick summary of the key takeaways.

macOS & GNU/Linux

  • list files and folders: ls and ls -a.
  • create files: touch.
  • create folders: mkdir.
  • navigate: cd.
  • remove files: rm.
  • remove folders: rm -rf.
  • read file contents: cat.

Windows

  • list files and folders: dir.
  • create files: echo.
  • create folders: mkdir.
  • navigate: cd.
  • remove files: del.
  • remove folders: rmdir.
  • read file contents: type.

You can now handle everyday command-line tasks! Expect to learn more about the command line as you encounter more programming challenges.

Commit Your Progress

DockYard Academy now recommends you use the latest Release rather than forking or cloning our repository.

Run git status to ensure there are no undesirable changes. Then run the following in your command line from the curriculum folder to commit your progress.

$ git add .
$ git commit -m "finish Command Line reading"
$ git push

We’re proud to offer our open-source curriculum free of charge for anyone to learn from at their own pace.

We also offer a paid course where you can learn from an instructor alongside a cohort of your peers. We will accept applications for the June-August 2023 cohort soon.

Navigation

Home Report An Issue Computer HardwareGit