Command Line for Beginners: pwd, ls, cd and mkdir
A terminal is a window where you type commands instead of clicking. The program that reads those commands is the shell; on Linux and in Git Bash it is usually bash, and on macOS it is zsh, which accepts the same basic commands. Developers use the terminal because it is fast, scriptable and the same on every server.
Reading a transcript
In this track, examples are transcripts. A line starting with $ is something you type (do not type the $ itself, it stands for the prompt). The lines after it are what the computer printed. Our test suite runs every transcript in a fresh empty folder, so the output is real. Your own folder names and prompt will look different.
Where am I? pwd
Everything in the shell happens inside a current directory (directory is the traditional word for folder). pwd, "print working directory", shows it. In our transcripts the test folder is shown as ~/demo; ~ is shorthand for your home directory.
What is here? ls
ls lists the contents of the current directory. ls -l adds details such as size and date, and ls -a also shows hidden entries whose names start with a dot, like .git. Options like -l are called flags, and most commands accept several.
Moving: cd
cd projects changes into the projects folder. Two special names help you move around:
..is the parent directory, socd ..goes up one level..is the current directory, used in commands like./script.sh.
cd with no argument returns to your home directory. Paths starting with / are absolute (from the top of the file system); others are relative to where you are.
Creating folders: mkdir
mkdir notes creates a directory. mkdir -p a/b/c creates the whole chain at once and does not complain if parts already exist.
# Each example starts in an empty folder, shown as ~/demo
pwd
mkdir -p projects/website
ls
cd projects
pwd
ls
cd website
pwd
cd ..
cd ..
pwd Transcript (verified by our test suite in a fresh temp folder)
$ pwd ~/demo $ mkdir -p projects/website $ ls projects $ cd projects $ pwd ~/demo/projects $ ls website $ cd website $ pwd ~/demo/projects/website $ cd .. $ cd .. $ pwd ~/demo
Follow the transcript line by line and watch pwd change as cd moves around. The ls after cd .. shows the folder we created.
Tab completion and history
Two habits save a lot of typing. Press Tab after typing the start of a file or folder name and the shell completes it. Press the Up arrow to bring back earlier commands. Both also prevent typos.
Common mistakes
- Spaces in names:
cd my foldertries to entermy. Quote it:cd "my folder". - Upper versus lower case: on Linux,
Notesandnotesare different folders. - Typing the
$from a transcript; it is only the prompt.
Summary
pwdshows where you are,lswhat is there.cd name,cd ..andcdmove you;mkdir -pcreates folders.$marks commands in a transcript; the rest is output.
Quiz: Terminal basics