Git and GitHub for Beginners › Module 1: Getting Started › Lesson 1.2

The Command Line in Ten Minutes

Free previewLesson 1.2 · about 25 minutes · 5 exercises

0 of 5 exercises completed
Every terminal on this page is live: click in it and type

Get the full course

10 modules of hands-on Git and GitHub skills — a live practice terminal inside every lesson, in your browser, nothing to install. Lessons 1.1 and 1.2 are free; the rest of the course is Rs. 300 + 18% GST.

Enroll Now — Rs. 354.00

By the end of this lesson you will be able to

  • say where you are with pwd and look around with ls
  • move between folders with cd, using relative paths, .. and ~
  • make folders and files, copy them, move or rename them and delete them
  • put text into a file with echo, and read it back with cat
  • use the up arrow and the Tab key to type less
How the exercises work

Every terminal on this page is a live pretend computer. Click inside it, type a command and press Enter. Each exercise gives you a task and a terminal that starts in a fixed state. Do the task, then press Check my answer. The checker looks at the final state of the pretend computer, so any correct way of doing the task is accepted. Show solution is there if you get stuck, and Reset puts the terminal back as it started. The picture beside the terminal shows your files, and later your commits.

Why the command line?

Git can be used through buttons and menus, but it was built for the command line: a window where you type an instruction, press Enter and read the reply. Graphical tools for Git do the same jobs, and when something goes wrong, the command line is where you can see what is really happening. It is worth ten minutes to feel comfortable in it, and that is all this lesson asks.

The window is called a terminal, and the program inside it that reads what you type is called the shell. On the practice terminals in this course, the line where you type looks like this:

thomas@practice ~ $

It tells you who you are (thomas), which computer this is (practice), and where you are: ~ is a short name for your home folder. The $ means the shell is ready for a command. In the lessons we show only the command, after a $, and the reply below it. You never type the $ yourself.

Where am I? pwd and ls

The shell is always in one folder, called the current folder or working directory. Two commands let you look around:

Try both in the terminal below. The folder tea-house has been made for you.

Folders and files are arranged like a tree. A path such as /home/thomas/tea-house/menu.txt reads from the top of the tree (/) down to one file. Your home folder is /home/thomas, which the prompt shortens to ~.

Hidden files

A name that starts with a dot, such as .gitignore, is hidden. Plain ls does not show it. ls -a (“all”) does. You will meet several hidden names in this course, including the .git folder in which Git keeps its history.

Moving around: cd

cd (“change directory”) moves you into another folder. Give it the name of the folder:

$ cd tea-house
$ pwd
/home/thomas/tea-house
$ ls
menu.txt  photos  prices.txt

A few special names save a lot of typing:

You typeIt meansExample
cd foldergo into a folder inside the current onecd tea-house
cd ..go up one level, to the parent folderfrom tea-house back to ~
cd ~ or just cdgo straight to your home folderfrom anywhere
cd /home/thomasgo to a full path, starting at the topa path beginning with / works from anywhere
.the current folder itselfls . is the same as ls

A path that does not start with / is relative: it starts from where you are now. cd photos only works if there is a photos folder here. If you get “No such file or directory”, the usual cause is that you are in a different folder from the one you thought. Type pwd to check.

Two keys that make you faster

Press the Tab key after typing the first few letters of a name, and the shell completes it for you (cd tea then Tab gives cd tea-house/). Press the up arrow to bring back the previous command, so you can run it again or change it. Both work in the terminals on this page.

Making things: mkdir and touch

mkdir (“make directory”) creates a new folder. touch creates a new, empty file (if the file exists already, it is left alone).

$ mkdir recipes
$ touch recipes/chai.txt
$ ls recipes
chai.txt

Putting text into a file: echo, > and >>

You will normally write files in an editor, but the shell has a quick way to make a small file, and we will use it in every lesson. The command echo prints the words you give it. The symbol > sends that output into a file instead of the screen:

$ echo "Masala tea" > menu.txt
$ cat menu.txt
Masala tea

cat prints the contents of a file. Notice the two arrows, because they behave differently:

SymbolWhat it doesCareful!
>replaces the file with the new text (creating it if needed)anything that was in the file is gone
>>adds the new text to the end of the filesafe: the existing lines stay

Common mistake: overwriting with a single arrow

$ echo "Masala tea" > menu.txt
$ echo "Darjeeling tea" > menu.txt
$ cat menu.txt
Darjeeling tea

The second command used >, so it replaced the first line instead of adding to it. Use >> when you want to add. This is one of the easiest ways to lose work on the command line, so keep it in mind.

The quotation marks matter when the text has spaces or special characters: they tell the shell that everything inside is one piece of text. Use them every time. Try the commands here:

Copy, move, rename and delete: cp, mv, rm

CommandWhat it doesExample
cp old newcopies a filecp menu.txt menu-backup.txt
mv old newmoves a file, or renames it if the new name is in the same foldermv draft.txt final.txt
mv file folder/moves a file into a foldermv menu.txt photos/
rm filedeletes a filerm junk.txt
rm -r folderdeletes a folder and everything in itrm -r old
rmdir folderdeletes a folder, but only if it is emptyrmdir empty
There is no wastebasket

On the command line, rm deletes for good. Nothing goes to a recycle bin. This is exactly why we are learning Git: once a file has been saved in a Git commit, you can get it back even if you delete it by mistake. Until then, check twice before pressing Enter on any rm.

Reading files: cat, head, tail and grep

cat file prints a whole file. For a long file, head -n 3 file prints the first three lines and tail -n 3 file prints the last three. grep word file prints only the lines that contain a word, and grep -n adds the line numbers. In this course the files are small, so cat is what you will use most.

When you forget what a command does, or which commands exist, type help in the practice terminal for a short list.

Real terminals do a bit more

A real computer's shell has many more commands, and can join commands together with the | (pipe) symbol. The practice terminal keeps to the commands above, which is all that Git needs. Everything you type here works the same way on a real computer, except that on Windows the names of folders look like C:\Users\thomas (if you use Git Bash, as Lesson 1.3 suggests, they look like /c/Users/thomas).

Exercises

Each exercise starts with the terminal in a fixed state. Read the task, type the commands, then press Check my answer.

Exercise 1 · Make a folder and step into it

Create a new folder called tea-notes inside your home folder, and then move into it, so that the prompt ends with ~/tea-notes.

Exercise 2 · Create a file with text in it

You are already inside tea-notes. Create a file called menu.txt that holds the single line Masala tea.

Exercise 3 · Add a line without losing the others

The file menu.txt already has two drinks. Add a third line, Assam tea, so that all three remain.

Exercise 4 · Copy and rename

Make a copy of menu.txt called menu-backup.txt, and rename draft.txt to prices.txt. menu.txt itself must stay as it is.

Exercise 5 · Tidy up

Delete the file junk.txt and the folder old (with everything inside it). Keep menu.txt. Then move into the folder photos.

0 of 5 exercises completed

Quick check

Four questions to confirm the main ideas. Choose an answer to see the explanation.

Which command shows the folder you are in?

You are in ~/tea-house/photos. Which command takes you to ~/tea-house?

What is the difference between echo "x" > f and echo "x" >> f?

A file name that begins with a dot, such as .gitignore, is…

Summary

  • pwd shows where you are, ls shows what is here and cd moves you.
  • cd .. goes up one level, cd ~ goes home, and a path starting with / works from anywhere.
  • mkdir makes folders, touch makes empty files and echo "text" > file writes text. Use >> to add, because > replaces.
  • cp copies, mv moves or renames, and rm deletes for good, with no wastebasket.
  • Tab completes names and the up arrow brings back earlier commands.

Found a mistake on this page, or something unclear? Report a problem and mention “Git Lesson 1.2”.