By the end of this lesson you will be able to
- say where you are with
pwdand look around withls - 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 withcat - use the up arrow and the Tab key to type less
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:
pwd(“print working directory”) shows the full path of the folder you are in.ls(“list”) shows what is inside it.
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 ~.
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 type | It means | Example |
|---|---|---|
cd folder | go into a folder inside the current one | cd tea-house |
cd .. | go up one level, to the parent folder | from tea-house back to ~ |
cd ~ or just cd | go straight to your home folder | from anywhere |
cd /home/thomas | go to a full path, starting at the top | a path beginning with / works from anywhere |
. | the current folder itself | ls . 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.
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:
| Symbol | What it does | Careful! |
|---|---|---|
> | 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 file | safe: 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
| Command | What it does | Example |
|---|---|---|
cp old new | copies a file | cp menu.txt menu-backup.txt |
mv old new | moves a file, or renames it if the new name is in the same folder | mv draft.txt final.txt |
mv file folder/ | moves a file into a folder | mv menu.txt photos/ |
rm file | deletes a file | rm junk.txt |
rm -r folder | deletes a folder and everything in it | rm -r old |
rmdir folder | deletes a folder, but only if it is empty | rmdir empty |
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.
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.
Quick check
Four questions to confirm the main ideas. Choose an answer to see the explanation.
Which command shows the folder you are in?
pwd prints the working directory. ls lists what is inside it.
You are in ~/tea-house/photos. Which command takes you to ~/tea-house?
.. means the parent folder, so cd .. goes up one level. cd ~ would go to the home folder instead.
What is the difference between echo "x" > f and echo "x" >> f?
A single arrow replaces the contents of the file; two arrows add to the end.
A file name that begins with a dot, such as .gitignore, is…
Names beginning with a dot are hidden. Use ls -a to see them.
Summary
pwdshows where you are,lsshows what is here andcdmoves you.cd ..goes up one level,cd ~goes home, and a path starting with/works from anywhere.mkdirmakes folders,touchmakes empty files andecho "text" > filewrites text. Use>>to add, because>replaces.cpcopies,mvmoves or renames, andrmdeletes 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”.
