Learn / Programming / Git & Command Line / Command Line Essentials

Git & Command Line · Lesson 1 of 5

Command Line Essentials

Navigate files, read output, pipe commands and use the shell productively.

  • Beginner
  • 14 min read
  • 3 objectives

What you will learn

  • Navigate with cd and ls
  • Create, copy and delete safely
  • Use pipes and grep

The command line (terminal, shell) lets you control your computer by typing commands. Developers use it constantly because it is fast, scriptable and the same on servers as on laptops. Git, Docker, npm and most tools are driven from it. The examples use bash/zsh (macOS, Linux, WSL, Git Bash).

Why learn the terminal at all?

Every graphical app you use is a friendly layer on top of commands. The terminal is the plain, direct way to talk to your computer: you type a short instruction, press Enter and it answers. It looks intimidating for a day and then becomes the fastest tool you own. Nearly every developer tool (Git, Docker, Python, Node, cloud services) is driven from it, and it is the same on servers where there is no mouse at all.

One rule makes it less scary: a command is a verb plus optional details. ls lists, cd changes folder, mkdir makes a folder. Add a flag such as -l for options and a path for the target.

Where am I? Moving around

pwd                 # print working directory
ls                  # list files
ls -la              # long format, including hidden files
cd projects         # go into a folder
cd ..               # go up one level
cd ~                # go home
cd -                # go back to the previous folder

Paths can be absolute (start with /, like /Users/amar/code) or relative to where you are. Press Tab to autocomplete names and the up arrow to recall previous commands.

Creating, copying, moving, deleting

mkdir notes                 # new folder
mkdir -p a/b/c              # nested folders
touch todo.txt              # empty file
cp todo.txt backup.txt      # copy
mv backup.txt notes/        # move (also renames)
rm todo.txt                 # delete a file
rm -r notes                 # delete a folder and contents

Looking inside files

cat file.txt            # print whole file
less file.txt           # scroll through (q to quit)
head -n 5 file.txt      # first 5 lines
tail -n 20 app.log      # last 20 lines
tail -f app.log         # follow a growing log
wc -l file.txt          # count lines

Searching

grep "error" app.log            # lines containing "error"
grep -rn "TODO" src/            # search recursively, show line numbers
grep -i "warning" app.log       # ignore case
find . -name "*.py"             # find files by name

Pipes and redirection

The pipe | sends one command's output into another's input, letting you build powerful one-liners from small tools. > writes output to a file, and >> appends.

cat access.log | grep " 500 " | wc -l         # how many 500 errors?
ls -l | sort -k5 -n | tail -3                # three biggest files
echo "hello" > out.txt
echo "world" >> out.txt

Environment and permissions

echo $HOME
export API_URL="https://example.com"      # set a variable for this session
chmod +x script.sh                        # make a file executable
./script.sh                               # run it
which python3                             # where is this command?

Getting help

man ls           # the manual page
ls --help        # quick usage
history | grep git

Anatomy of a command

ls -la ~/projects
#  |  |    |
#  |  |    +-- argument: what to act on
#  |  +------- flags: change how it behaves (-l long list, -a include hidden)
#  +---------- command: what to do

A first session, step by step

Type these one at a time and read what each prints. $ is the prompt, not part of the command.

$ pwd                    # where am I?
/Users/ada
$ mkdir practice         # make a folder
$ cd practice            # go into it
$ echo "hello" > a.txt   # create a file containing hello
$ ls
a.txt
$ cat a.txt              # show its contents
hello
$ cd ..                  # go back up one level

Paths: absolute and relative

  • Absolute paths start at the root: /Users/ada/practice/a.txt. They work from anywhere.
  • Relative paths start from where you are: practice/a.txt. . means here, .. means the parent folder, and ~ means your home folder.

Keyboard shortcuts that save hours

  • Tab completes file and folder names. Press it constantly; it prevents typos.
  • Up arrow brings back previous commands; Ctrl+R searches your history.
  • Ctrl+C stops a running command; Ctrl+L clears the screen.
  • man ls or ls --help shows the manual for any command.

Key takeaways

  • A command is a verb, optional flags and arguments: ls -la folder.
  • pwd, ls, cd, mkdir, cat are the survival kit.
  • Use Tab to complete, the Up arrow for history and --help when stuck.
  • rm is permanent; double-check before deleting.
# Write your solution here
Up next · Lesson 2Git BasicsRepositories, commits, staging and reading history.