Command Line Student Material

The command line is a way of controlling a computer without a GUI. In this unit, we will learn the basic commands to interact with our computer.

Key Takeaways

  • Students will move around their computer with commands.
  • Students will create, move, copy, and delete files and folders.

What Is The Command Line?

Most users most of the time use a Graphical User Interface (GUI) to interact with the computer. GUIs typically rely on a mouse or touch to manipulate windows with icons, buttons, scrollbars, etc.

The command line is an alternate view into the computer. Sometimes referred to as "a shell", "the terminal", "bash", or a "text interface", the command line is:

  • A means of interacting with a computer program where the user types in commands. e.g:
open README.txt
  • A way to navigate and operate your computer without a mouse.
  • A non-graphic way to create, read, update, operate on, and delete your files and directories. e.g.:
mv README.txt readme.txt

The GUI and the Command Line are looking at the same thing! They are different ways of interacting with the computer.

When Do We Use The Command Line?

Command-line interfaces are often preferred by developers:

  • They provide a more concise means to control a program or operating system. Why? Because everything happens in the same window, and with practice it is less effort than finding and manipulating things with a mouse.
  • They easily expose options for controlling a command. Why? Menus in GUIs provide a very small number of options. In command line, there are hundreds of commands, and a few characters added to a command can make it do many different things. e.g.
  • ls Basic list files
  • ls -a List all files, including normally hidden files
  • ls -l Long list, showing information about each file
  • ls -R Recursive, list the contents of directories
  • ls -la Long list all files
  • They make it easy to act on lots of files at once in complex ways.
for i in *.png; do pngtogif $i `echo $i | sed s/png/gif/`; done
  • They provide a means to program the operating system (shell programs) e.g. The example above is a very simple shell program designed to modify images with file names ending in .png.

Command Line In Action

Parts of a Command Line Command

All commands have three parts:

  • The command (or utility) always comes first.
  • Flags are optional, depending on the utility that you are using. Flags always start with a - (minus).
  • Arguments are the things for the command to act upon, or necessary information. Often file names.

Framing

Context & purpose

The command line is a way of controlling a computer without a mouse or a GUI (Graphical User Interface). Before Apple Computer started building machines with icons and folders you could click on, the only way to interact with your computer was through the command line.

As developers, we use the command line because it's a lot faster and more powerful that the GUI - no need to waste time clicking around. The command line can allow you to perform pretty much any interaction you have with your computer.

Since the terminal is a much simpler interface, it has a big advantage over the GUI when you need to remotely connect to a computer over a network link. This is something that developers do very frequently - when traveling, for example.

In this lesson we're going to learn the basic commands to interact with our computer by: creating files and directories, moving folders, changing directories, and listing the contents of a folder.

Learning objectives

Students will be able to

  • Use Terminal to navigate files and folders
  • List out contents of a folder
  • Create files and folders on the command line
  • Move files and folders
  • Copy files and folders
  • Delete files and folders

Getting Started

Accessing the Terminal

Section framing and purpose

Terminal provides a Command Line Interface (CLI) to the operating system. With it you can give your computer direct, text-based instructions.

In order to do this, we need to launch your terminal based on the system that you're using.

When terminal launches, it will start in your computer's home directory (whatever you named your computer). Your home directory is denoted by the tilde ~.

When discussing our world inside the Terminal, Directories are the same as Folders (we may just call them Directories sometimes).

Changing Directories

Section framing and purpose

In order to talk to our computer in the Terminal and tell it what we want to do, we need to speak a common language. The Command Line understands commands written in the bash scripting language. The commands are acronyms or abbreviations of English words.

When you're navigating in your terminal, it's important to know where you are (this translates directly to real life as well - if you want to get somewhere, you first have to know your current location).

# See your current location - abbreviation for 'print working directory'
pwd

List available files and directories

Inside your working directory, there are likely several files you can access. Normally, we rely on visual cues like icons to tell us what's available, but it would be difficult to navigate our computer if we were just guessing where files and folders were located. In the command line, we'll use a specific command to list out what we can access from any given working directory / any given location.

In order to see which files or folders are in our current directory, use the ls command (an abbreviation for list) to see a list view of the contents.

# See files and folders in current location - abbreviation for 'list'
ls

Change working directory

If you need access to a file or directory not present in the list, you might want to change your working directory in order to more easily access it.

The syntax to tell the Command Line to move you to a new directory (also known as a folder) is cd followed (on the same line) by the path to the new folder.

If you navigate to a directory that is inside the directory you're currently in (i.e. a subfolder of your working directory), use the command cd followed by the name of the folder.

# Change your current working directory (current location)
# Abbreviation for 'change directory'
cd Documents

You can also navigate starting at the home ~ directory.

# Add a `~/` to begin from the home directory.
cd ~/Downloads

If you've changed directories but need to change back to the home directory, do so either by navigating directly back to home using ~, or by using .. to navigate up one level.

# Option 1: navigate directly to the home directory
cd ~

# Option 2: navigate back one level
cd ..

Note that cd .. only goes up (back) one level, so if you've changed directories three times so far (down three levels), you'll need to use cd .. three times to go all the way home, but would only need to use cd ~ once.

Mini-challenges

  • Use ls to see what's inside your current directory.
  • Navigate back to your home directory and use ls to see the contents of your home folder.
  • Choose the name of one of the folders that are inside the home directory and navigate to this new folder.
  • Use the list command to see what's inside.

Protip: Autocompletion with tab

When changing directories with the cd command, a single typo will prevent the command from executing correctly. However, using the tab key will autocomplete with the name of any available directories that are inside your current directory. So instead of typing cd Documents, type just cd Do and then press the tab key and the terminal will autocomplete the command cd Documents/ for you (the / at the end is optional but lets you know that the destination is a directory).

This autocomplete not only saves time when it works, it also provides valuable information when it doesn't work. If the tab button doesn't autocomplete, you've likely made one of three really common issues:

  1. The directory you want to navigate to isn't accessible from your current working directory.
  2. There are two directories that have similar starting names (like Documents and Downloads) and you haven't provided enough information for the autocomplete to know which one you want. Press tab again to see a list of the directories that could match what you've typed. Then type a little more (enough so that there's only one match) and press tab again.
  3. You've already included a typo in the first half of the word.

The sooner you can get in the habit of using tab to autocomplete your commands, the more likely you will be to handle frustrating situations where your commands seem not to be working.

Parent and Child Directories

Directories and files can have child and parent relationships to each other. If directory_b is inside directory_a, directory_b is referred to as the child of directory_a. directory_a is then known as the parent of directory_b. If a third item, my_file, is inside of a directory_b, it is considered a child of directory_b and a grandchild of directory_a.

These relationships are often illustrated on forums with a text diagram, like this:

directory_a/
  directory_b/
    my_file

Notice that directory names end in a / while file names do not. This hierarchical organization is generally referred to as the tree structure of a filesystem.

Siblings

If there are multiple files or folders on the same level, they are known as siblings. Ex: If file_b and file_c are located inside of directory_a they are siblings.

That text diagram would look like this:

directory_a/
  file_b
  file_c

Note that since file_b and file_c are siblings, they are indented the same amount.

File Paths

Here's a more complex text diagram that features both parent-child and sibling relationships.

north_america/
  united_states/
    new_york/
      new_york_city/
        manhattan
        queens
        staten_island
        bronx
        brooklyn
    california/
      mountain_view
      los_angeles
    illinois/
      chicago
  mexico/
    mexico_city

In this example, new_york_city/ is a directory name, but it also has a path, which details how to get there: ~/north_america/united_states/new_york/new_york_city/.

To change directory into new_york_city/, you could navigate in steps using names, or you could navigate all the way there in one command using the path.

# Option 1: in four steps, using directory names.
cd ~ # navigate home, where these folders are located
cd north_america/
cd united_states/
cd new_york/
cd new_york_city/

# Option 2: in one step, using the path.
cd ~/north_america/united_states/new_york/new_york_city/

Summary of Navigation Commands

# See your current location - abbreviation for 'print working directory'
pwd

# Change to a new location - abbreviation for 'change directory'
cd ~/Downloads

# Navigates into a child directory called `some_directory`
# Only works if `some_directory` is a child of the current directory
cd some_directory

# Navigates into the parent of the current directory
# `..` is shorthand for parent.
cd ..

# Will take you back home
cd

Mini-challenges

  • Use pwd to find out the current folder that you're in.
  • Change to a different directory and confirm your new location.
  • Try it again and navigate to a different folder. Use your new commands to see your location and also see the files inside this directory.

Making Files & Directories

Section framing and purpose

As developers, we will need to create files with code. To keep those files organized, we'll need to create folders / directories where we can store our files.

Be aware of your location in the terminal before creating your files and folders.

# See your current location.
pwd

To make a file, use the command touch followed by the file name.

# `touch` creates a new file.
touch style.css

When creating files, add a file extension to specify the type. Example file types:

  • .js make as a JavaScript file
  • .html make an HTML file
  • .css makes a CSS file
  • .md makes a markdown file

The file names should not include spaces.

# make a file - this will create a file in the current folder
touch my_text_file.txt

# make a file - this will create a file on the desktop
touch ~/Desktop/my_new_style_sheet.css

Making multiple files

You can create multiple files at once by including spaces between each of the names.

# make multiple files - this will create multiple files in the current folder
touch my_new_js_file.js my_new_html_file.html my_second_html_file.html

Making directories

To make a directory / folder, use the command mkdir - an abbreviation for ‘make directory' - followed by the name of the folder.

# make a folder - this will create a directory in the current folder
mkdir my_new_folder

# make a folder - this will create a directory on your desktop
mkdir ~/Desktop/my_new_desktop_folder

Making multiple directories

You can create multiple folders at once by including spaces between each of the names.

# make multiple folders - this will create multiple folders in the current
# directory
mkdir folder_one folder_two folder_three

Mini-challenges

  • Create a new file.
  • Create a few files in one command.
  • Create a new directory.
  • Create a few more new directories. Try using just one command to do this.
  • Using what you know about navigating directories and creating files and folders, construct a ‘family_tree' on your desktop using files and folders.

Moving & Copying

Moving Files & Directories

Section framing and purpose

We can move a file or folder on the command line, but we always need to specify three things:

  1. We use the mv command - abbreviation for move.
  2. Follow the move command with the name of the file or folder that you want to move.
  3. Follow the name of the file with the destination where you want to move it.

For example, if you have a file structure like this:

my_child_folder/
my_parent_folder/

You can see that it isn't organized as it ought to be - they are siblings when they ought to be parent and child. Use this command:

# move a directory - this will move the folder `my_child_folder` into the
# folder named `my_parent_folder` that is in the current folder
mv my_child_folder/ my_parent_folder/

The resulting structure will look like this.

my_parent_folder/
  my_child_folder/

Here are some other variations of the mv command:

# move a directory - this will move the folder `my_folder` into the file named
# `my_desktop_folder` that is located on the Desktop
mv my_folder ~/Desktop/my_desktop_folder

# move a file - this will move the file `my_file.txt` into the file named
# `my_folder` that is in the current folder
mv my_file.txt my_folder

# move a file - this will move the file `my_file.txt` into the file named
# `my_desktop_folder` that is located on the Desktop
mv my_file.txt ~/Desktop/my_desktop_folder

Warning: mv also happens to be the command to rename a file using the syntax mv old_name new_name. That means that if you move a file to a directory that does not exist, or if you misspell the destination, then the file itself will be renamed instead of moved.

Mini-challenges

  1. Make 5 text (.txt) files.
  2. Make a new directory.
  3. Move the 5 files that you created into the folder.
  4. Make a folder and move this new folder inside of another folder.

Copying Files & Directories

Section framing and purpose

As we build our webapps, we may wish to duplicate a file (if we're building two similar webpages, we might not want to start over from scratch), and the syntax for the cp (copy) command is really similar to the mv syntax.

Copying a file

The copy command has three parts:

  1. The command cp - abbreviation for copy
  2. The name of the file to be copied
  3. The name of the copy (if staying in the same folder) or the location and name (if moving to a different folder).
# Copy a file named `my_file.txt` to a file called `my_file_copy.txt`
# The copy will be located in your Documents folder.
cp my_file.txt ~/Documents/my_file_copy.txt

# Copy a file named `my_file.txt` to a file called `my_file_copy.txt`.
# Since no directory was specified, this will occur within the same folder.
cp my_file.txt my_file_copy.txt

If you want the copy to retain the same name as the original, you can specify the copy location without a file name.

# Copy a file named `my_file.txt` to your Documents folder.
# The destination ends with a directory, so the copy will have the same name.
cp my_file.txt ~/Documents/

Having lots of files with the same name can be confusing, so only use this method if you're certain it's what you need.

Copying a directory

Copying a folder or directory is a little more complicated, because you likely want to copy not just the folder, but also everything inside of it.

  1. The command cp -R - abbreviation for copy recursive - use this for both empty and non-empty folders
  2. The source name (folder or file that we want to move)
  3. The name of the copy (if staying in the same folder) or the location and name (if moving to a different folder).

Warning: When specifying the name for the copy, if the file or folder was not created previously, it will be created when you make the copy. If the file was created before you run the copy command, it will overwrite anything that was previously in the file, so be careful!

# copy a directory named `my_folder` from your Desktop folder to a folder
# called `my_folder_copy` in your Documents folder
cp -R ~/Desktop/my_folder ~/Documents/my_folder_copy

# copy a file named `my_file.txt` from your Desktop folder to your Documents
# folder
cp -R my_folder my_folder_copy

Note: the -R component of the cp -R is called a flag. Flags always start with a dash, and are used in combinations with commands to give more specific instructions for how that command is to be executed.

Mini-challenges

  1. Create a file called my_original.txt
  2. Make a copy of my_original.txt named something new in the same folder.
  3. Make a new directory.
  4. Make a copy of that directory, but name it something different.
  5. Make a copy your copied file (from step 2) but place it into the copied directory (from step 4).

Removing Files & Folders

Section framing and purpose

We can delete files and folders on the command line, but doing so is permanent.

Removing a file

To remove a file, we can use the command rm (abbreviation for remove) followed by the name of the file.

# delete a file in the working directory
rm unwanted_file.txt

# delete a file not in the working directory
rm ~/Desktop/unwanted_file.txt

Removing a directory that is empty

To remove an empty directory, we can use the command rmdir - abbreviation for remove directory - followed by the name of the folder.

# delete an empty folder
rmdir empty_directory

# delete an empty folder
rmdir ~/Documents/my_files/empty_directory

Removing a directory that is not empty

To remove a folder that is not empty, we have to put a flag -R - abbreviation for recursive - after the rm command which tells the computer to delete the contents inside of the folder. As a shortcut, some developers use the flag -Rf which forcefully removes everything (otherwise, you may receive prompts about deleting some of the child files or folders).

# delete a folder which contains other files or folders
rm -R directory_which_isnt_empty

# delete a folder which contains other files or folders
rm -R ~/Downloads/directory_which_isnt_empty

Warning: Using the rm command is not like the recycle bin or the deleted section of your email - these files cannot be easily recovered. Be EXTREMELY CAUTIOUS when deleting files in this way.

Mini-challenges

  1. Create 4 files.
  2. Delete 2 of the files.
  3. Create a folder.
  4. Create another folder and create a file inside this folder.
  5. Delete both of the directories.

You can find the code for this lab in the following folder inside the students repository that you downloaded from GitHub: command-line/labs/around-the-world

Background

Your semester abroad is right around the corner and it's time you started planning your European travels...

With three months of some serious living in the near future, you want to make sure that you take full advantage of every weekend trip.

Fortunately you're not starting from scratch and your friends have provided you with some of their favorite activities from their semester abroad. Unfortunately, some of the files have gotten mixed up...

Don't fret, with the magic of command line you'll have them fixed in no time!

Task

Make sure every city is in the correct country and that each city folder has the right monuments inside. Be careful or you might end up in Amsterdam looking for the Eiffel Tower!

If you're feeling REAL confident with this, hide your GUI and face this challenge straight from the command line. You should stay in the general around-the-world folder while you do all of this, that means using absolute paths when referring to folders and files!

Once you're sure everything is in the right place, it's your turn to take command of your next few months. Brainstorm new cities, countries, and monuments that you NEED to see and add them in to your file tree!

Congratulations! You've officially learned how to speak computer. Ready to take it a step further? Let's talk extensions...

Extensions

  1. Mix up all of your files again, then make your way into a city of your choice. Reorganize the files again, while staying in that city.
  2. Try one of the fun command line games linked below
    Terminus Game
    Advanced Game (be ready to Google)
  3. Huge fan of Star Wars? Try running telnet towel.blinkenlights.nl in your command line and pressing enter.... (Note that in recent versions of MacOS the telnet command is no longer installed by default; in this case the command nc towel.blinkenlights.nl 23 can be used instead.)

You can find the code for this lab in the following folder inside the students repository that you downloaded from GitHub: command-line/labs/bashing-away-repetition

Ready to bash the repetition out of your daily command line usage? Great, so am I!

Background

If there's one thing programmers hate, it's repetition. As computer scientists, it's our job to make the computer do as much of our work as possible. Writing bash scripts will help us make our command line usage more efficient.

First things first, What is a bash script?

GREAT question. Thankfully our friend Ryan Chadwick, over at this great place to find command line tutorials, has an answer for us.

A Bash script allows us to define a series of actions which the computer will then perform without us having to enter the commands ourselves. If a particular task is done often, or it is repetitive, then a script can be a useful tool.

No repetition? That's what I'm talking about.

Tutorial

First things first, in order to be able to run your script, you're going to need to give your computer permission. Do this by running the command below.

chmod 755 myScript.sh

Alright, time to write your first program. Head over to your myScript.sh (see if you can open the file from the command line). At the end of the file, add:

echo "Hello World"

Now run:

./myScript.sh

in your terminal. ** TA DA ** you should see "Hello World" printed in your terminal.

celebration

Everything that you write in your bash script will run as if you had typed it into your terminal. Let's walk through one more example before jumping into extensions. First create a file for your script, this can be called anything you want but it should have the extension .sh . Next make sure to give your script permission to run by typing chmod 755 and then the name of your script in your terminal and pressing enter. Every time you start a new bash script you should make sure to complete those two steps.

Intro Challenge

Now, let's try writing a script that creates a new folder. What command do we know creates a new folder? If you're thinking of a word that starts with a m and ends with kdir, you're on the right track. Write the command that will create your new folder and name it something cool in your new .sh file, then run it in your terminal.

If it works, move on to the other challenges listed below. If you're still a little confused try looking back at your around the world lab and thinking about how you created new country and city folders.

Don't be afraid to use the tutorial linked above and be ready to whip out your best Googling skills. Write and run scripts that do the following:

Main Tasks

  1. Add a command to the file you created above that navigates into the folder you're creating. (What command do we use to change directories?)
  • Date stamp your new folder name. For example, your program should create a folder named "2017-12-07my_super_cool_folder"
  1. Prints out "My current directory is: " followed by your working directory, and "Here is a list of everything in this directory! " followed by the contents of your current directory.
  2. Figure out how to give your bash script an input, and create a file based on that input string. The tutorial linked earlier in the lab has some great examples in the variables section to check out.
  3. Ask the user for their name, then print out "Hi nice to meet you insert their name here"
  • Try asking your user for more info and creating a customized message for them.

CONTEXT

The Command Line (or Terminal) provides a direct interaction with the computer Operating System (OS) - everything that the graphical user interface (GUI) does, but via text commands - from navigation, to making new files, to executing scripts. The command line will be used to open files, and to initiate the development environment when using Google App Engine.

Command Line In Action

Accessing the Terminal

Mac Shortcut:

  • Open Spotlight with Command + space.
  • Type Terminal in the Spotlight Search.
# See your current location - abbreviation for 'print working directory'.
pwd

# Change to a new location - abbreviation for 'change directory'.
cd /Users/jsmith/Downloads

# Navigate into a child directory called `some_directory`. This only works if
# `some_directory` is a child of the current directory.
cd some_directory

# Navigate into the parent of the current directory. `..` is shorthand for parent.
cd ..

# Return home.
cd

# Change directories to a folder called `my_folder` that is within the current
# directory.
cd my_folder ```

#### File Path Shorthand `~`

The `~` is generally short for "home". Home can be configured to mean different
things for different users and across different operating systems, but in the
example above, for a Mac user named "jsmith", the `~` is short for that user's
home directory: `/Users/jsmith`.

This means that `~/Downloads` and `/Users/jsmith/Downloads` are the same for
this user. But starting with a `~` allows us to specify absolute file paths
(agnostic of your current working directory, and starting at the root `/`)
without having to write out a full path each time.

### <a id="view"></a>Viewing the Contents of Folders

```bash
# See files and folders in current location - this is an abbreviation for 'list'.
ls

Creating Files & Folders

Creating Files

# Make a file - this will create a file in the current folder.
touch my_text_file.txt

# Make a file - this will create a file on the Desktop.
touch ~/Desktop/my_new_style_sheet.css

# Make multiple files - this will create multiple files in the current folder.
touch my_new_js_file.js my_new_html_file.html my_second_html_file.html

Creating Folders

# Make a folder - this will create a directory in the current folder.
mkdir my_new_folder

# Make a folder - this will create a directory on your Desktop.
mkdir ~/Desktop/my_new_desktop_folder

# Make multiple folders - this will create multiple folders in the current
# directory.
mkdir folder_one folder_two folder_three

Moving Files & Folders

Moving Files

# Move a file - this will move the file `my_file.txt` into the folder named
# `my_folder` that is in the current folder.
mv my_file.txt my_folder
# Note that this will rename the file instead of moving it if the destination
# folder does not yet exist.  Read below for more details on renaming.

# Move a file - this will move the file `my_file.txt` into the folder named
# `my_desktop_folder` that is located on the Desktop.
mv my_file.txt ~/Desktop/my_desktop_folder

Moving Folders

# Move a directory - this will move the folder `my_child_folder` into the
# folder named `my_parent_folder` that is in the current folder.
mv my_child_folder my_parent_folder

# Move a directory - this will move the folder `my_folder` into the folder
# named `my_desktop_folder` that is located on the Desktop.
mv my_folder ~/Desktop/my_desktop_folder

Moving is Renaming

Bear in mind that mv is also the same command used to rename files or folders.

# Rename a file from `old_name.txt` to `new_name.txt`.
mv old_name.txt new_name.txt

Copying Files & Folders

Copying Files

# Copy a file named `my_file.txt` from your Desktop folder to your Documents
# folder.
cp ~/Desktop/my_file.txt ~/Documents

# Copy a file named `my_file.txt` to a file called `my_file_copy.txt` within
# the same folder.
cp my_file.txt my_file_copy.txt

Copying Folders

# Copy a directory named `my_folder` (and its contents) from your Desktop
# folder to a folder called `my_folder_copy` in your Documents folder.
cp -r ~/Desktop/my_folder ~/Documents/my_folder_copy

# Copy a directory named `my_folder` (and its contents) from the current
# working directory to a folder called `my_folder_copy` in the current working
# directory.
cp -r my_folder my_folder_copy

Deleting Files & Folders

Deleting Files

# Delete a file in the current working directory.
rm unwanted_file.txt

# Delete a file using its exact location and name.
rm ~/Desktop/unwanted_file.txt

Deleting Folders

# Delete an empty folder.
rmdir empty_directory

# Delete a folder which contains other files or folders.
rm -r directory_which_isnt_empty

Tips & Tricks

  • Practice using the command line, even when you don't think you need to. You'll get better at it over time.
  • The tab key will autocomplete your file or folder name (it can be case-sensitive).
  • If there is only one remaining matching file or folder, it will autocomplete.
  • If there are multiple matching files or folders, press tab twice to see a list of the remaining matches.
  • The up / down arrow will cycle your command history so that you don't have to re-type commands.
  • Use the command clear to clear the Terminal window. This keeps all your earlier work - scroll up to see it.
  • Use reset to restart the terminal. This will not keep your earlier work on screen, but can resolve several issues if the Terminal starts behaving in unpredictable ways.
  • Press option + arrow to move the cursor by a word

Best Practices Summary

  • Navigate the Terminal using cd, cd .., or cd <folder name>.
  • View the contents of folders using ls.
  • Create files using touch; create folders using mkdir.
  • Move files and folders using mv <item to move> <destination>.
  • Copy files and folders using cp <item to copy> <destination>.
  • Delete files and folders using rm <item to remove>.

Question 1

What would you use to identify your current working directory?

  1. pwd
  2. ls
  3. cd
  4. mkdir
  5. touch

Question 2

How would you move up two levels from your current working directory?

  1. mv ~
  2. cd ../..
  3. mv up/up
  4. cd ^(2)

Question 3

Which command would you use to rename a file?

  1. mv
  2. cd
  3. ls
  4. python
  5. rename

Question 4

Why would the command mv script.py throw an error?

  1. Because ‘script.py' is an invalid file name.
  2. Because the mv command can't be followed by any other information.
  3. This is a trick question; it would not throw an error.
  4. Because moving a file requires at least two arguments - a target and a destination

Question 5

What would the command cd ~ do?

  1. Change your working directory to your home directory.
  2. Repeat the previous command.
  3. Use an algorithm to determine the most useful command at this time.
  4. Throw an error.

Question 6

Which of the following is NOT a reason to use tab to autocomplete filenames?

  1. Because it saves time.
  2. Because it helps eliminate spelling errors.
  3. Because it requires less processing power.

Question 7

If a new developer tries to create a file called "my script.py", why will the command touch my script.py not work?

  1. Because touch cannot create new files.
  2. Because .py is not a valid file extension.
  3. Because it will create two files, one called "my" and one called "script.py" instead of just one.
  4. Because it is impossible to create new files from the command line.

Question 1

What would you use to identify your current working directory?

pwd

Question 2

How would you move up two levels from your current working directory?

With cd ../.. you move up two levels (each .. represents one level above).

Question 3

Which command would you use to rename a file?

mv is used to move a file from one location to another and can be used to change the file name as well.

Question 4

Why would the command mv script.py throw an error?

The mv command requires two arguments, a source file and a destination file.

Question 5

What would the command cd ~ do?

It would change your working directory to your home directory. The special character ~ is used to identify the home directory of the current user. It can also be used in combination with a different username: ~user2 to point to their home folder.

Question 6

Which of the following is NOT a reason to use tab to autocomplete filenames?

Because it requires less processing power is not correct. tab requires a (frequently negligible) amount of processing power to scan the folders looking for filename to autocomplete, but it saves time and avoids spelling errors in the process.

Question 7

If a new developer tries to create a file called "my script.py", why will the command touch my script.py not work?

It will create two files instead of just one. The touch command will create new files. In the terminal spaces are separators, not characters to be included in the file name. It is best to avoid them completely, but if you must use them, the filename should be wrapped in double quotes:

touch "my script.py"

Question 1

The terminal command for changing directories is:

  1. move
  2. mkdir
  3. pwd
  4. cd

Question 2

What command would you run if you wanted to navigate to the parent folder of your current working directory?

  1. cd up
  2. cd .
  3. cd ..
  4. cd ~

Question 3

Which command is used to create files?

  1. touch
  2. mkdir
  3. create
  4. pwd

Question 4

Which of the following commands will create two files called index.html and style.css?

  1. touch index.html, touch style.css
  2. touch index.html, style.css
  3. touch index.html style.css
  4. touch index.html + style.css

Question 5

Which command do you use to rename a file or folder?

  1. rename
  2. move
  3. mv
  4. rm

Question 6

What flag do the rm and cp commands need to work on folders?

  1. -f
  2. -r
  3. -x
  4. -d

Question 7

In order to move projects to ~/Documents and rename it to Projects, which command must go in the blank?

cd ~
mv projects ~/Documents
# fill in the blank here
mv projects Projects
  1. pwd
  2. cd projects
  3. mv ~ projects
  4. cd ~/Documents
  5. ls

Question 8

What will the following command do?

rm -r projects

  1. Delete a file called projects
  2. Delete a folder called projects
  3. Rename a file called projects
  4. Search the contents of the current directory for projects

Question 1

The terminal command for changing directories is:

The terminal command for changing directories is cd. The command itself is an abbreviation for "change directory".

Question 2

What command would you run if you wanted to navigate to the parent folder of your current working directory?

The command for navigating to the parent folder is cd ..

Question 3

Which command is used to create files?

The command to create files is touch

Question 4

Which of the following commands will create two files called index.html and style.css?

The command touch index.html style.css will create the two files. No comma is necessary and any number of files can be created this way, separated by spaces.

Question 5

Which command do you use to rename a file or folder?

Although it stands for "move" and can be used to move files or folders, mv can also be used to rename files and folders.

Question 6

What flag do the rm and cp commands need to work on folders?

rm and cp commands require the -r flag to work on folders, since the -r flag stands for "recursive" and re-runs the command for any files and folders contained within.

Question 7

In order to move projects to ~/Documents and rename it to Projects, which command must go in the blank?

cd ~
mv projects ~/Documents
# fill in the blank here
mv projects Projects

Since we start in the home directory, we would need to run cd ~/Documents in order to get the final command to be run in the correct directory.

Question 8

What will the following command do?

rm -r projects

The command will delete either a file OR a folder called projects because it is called with the -r, or recursive flag. Without that flag, this command would only work to delete a file with that name.