Command Line Basics (Linux and macOS)
Files and Folders
In this lesson you will
- Move between folders with cd
- Tell absolute and relative paths apart
- Create folders and empty files
Introduction
Your files live in folders inside folders, like a tree. In the terminal you move through that tree by typing where you want to go.
Paths
A path is an address for a file or folder.
- An absolute path starts from the very top, written
/. Example:/home/sam/Documents. - A relative path starts from where you are now. Example:
Documents/notes.
Shortcuts you will use all the time:
.means the current folder..means the folder above this one~means your home folder
Moving around
Terminal
$ cd Documents
$ pwd
/home/sam/Documents
$ cd ..
$ pwd
/home/sam
$ cd ~cd means change directory. On its own, cd takes you home.
Quick quiz
What does cd .. do?
Looking around
Terminal
$ ls -l # long list: size, date and permissions
$ ls -a # also show hidden files, which start with a dotCreate folders and files
Terminal
$ mkdir practice # make a folder
$ cd practice
$ touch notes.txt # make an empty file
$ mkdir -p projects/web # make nested folders in one go
$ ls
notes.txt projectsEverything after a # is a comment for you. The shell ignores it.
Tips that save time
- Press Tab to autocomplete file and folder names.
- Press the Up arrow to bring back your last command.
- If a name has spaces, put it in quotes:
cd "My Folder". - Names are usually case sensitive on Linux:
Notes.txtandnotes.txtare different.
Try it yourself
Create a folder called practice in your home folder. Inside it make two folders, a and b, and an empty file called todo.txt. Use ls to check, then use cd .. to go back up.
Summary
- Absolute paths start at
/, relative paths start from where you are. cdmoves,lslists,mkdirmakes folders andtouchmakes empty files.- Tab completion and the Up arrow make typing faster.
Lesson complete
+20 XP
Nice work. Keep going while it is fresh.
Sources and further reading