Skip to content

1.1.2 Basic Terminal Operations

Diagram of the relationship between terminal paths and command execution

This is the section where you start using the terminal in a hands-on way. First, you’ll learn how to tell “which directory am I in right now?”, then you’ll master the most common operations for files, directories, paths, pipes, and environment variables. This will prepare you for running Python, managing dependencies, and using Git later on.

  • Master 10+ core commands, covering 90% of everyday tasks
  • Understand absolute paths and relative paths
  • Learn how to use pipes and redirection
  • Understand the concept of environment variables

First, find and open your terminal:

Operating systemHow to open
WindowsSearch for “PowerShell” or “Windows Terminal” and click to open
macOSPress Command + Space, search for “Terminal”, and press Enter
LinuxCtrl + Alt + T

You’ll see a window with a blinking cursor waiting for your command. That is the terminal.


The command line has no graphical interface, so you need to tell the computer in text which folder you want to work in. That is called a path.

Terminal window
pwd

pwd = Print Working Directory

The output might look like this:

/Users/zhangsan # macOS
/home/zhangsan # Linux
C:\Users\zhangsan # Windows PowerShell

This is the folder you are currently in, called the working directory.

/Users/zhangsan/projects/ai-course/data/train.csv

This is an absolute path — it starts from the root directory / and fully describes the file’s location. It’s like a complete street address in real life.

data/train.csv

This is a relative path — it depends on the folder you are currently in. If you are in /Users/zhangsan/projects/ai-course/, then data/train.csv refers to the same file as the absolute path above. It’s like saying “the second floor of the building next door.”

SymbolMeaningExample
/Root directory (the starting point of all files)cd /
~The current user’s home directorycd ~ is the same as cd /Users/zhangsan
.Current directory./run.py means run.py in the current directory
..Parent directorycd .. goes up one level

Here’s a small exercise to help you understand:

Terminal window
# Suppose you are in /Users/zhangsan/projects/ai-course
pwd # Output: /Users/zhangsan/projects/ai-course
cd .. # Go up one level
pwd # Output: /Users/zhangsan/projects
cd ~ # Return to the Home directory
pwd # Output: /Users/zhangsan
cd ~/projects/ai-course # Go back using an absolute path
pwd # Output: /Users/zhangsan/projects/ai-course

These are commands you will use every day. Type them along with the tutorial first—you don’t need to memorize them. The more you use them, the more natural they’ll become.

Terminal window
cd projects # Enter the projects folder
cd .. # Go back to the parent directory
cd ~ # Return to the Home directory
cd ~/Desktop # Go to the Desktop
cd - # Go back to the previous directory (very useful!)
Terminal window
ls # List files and folders in the current directory
ls -l # Detailed list (shows size, date, permissions)
ls -a # Show hidden files (files starting with .)
ls -la # Combine both
ls projects/ # List the contents of the projects folder
Terminal window
mkdir my-project # Create a folder
mkdir -p a/b/c # Create nested folders in one go
Terminal window
touch hello.py # Create an empty Python file
touch README.md # Create an empty Markdown file
Terminal window
cp file.txt file_backup.txt # Copy a file
cp file.txt ~/Desktop/ # Copy to the Desktop
cp -r my-folder/ my-folder-backup/ # Copy an entire folder (`-r` means recursive)
Terminal window
mv old_name.py new_name.py # Rename a file
mv file.txt ~/Desktop/ # Move to the Desktop
mv project/ ~/projects/ # Move a folder
Terminal window
rm file.txt # Delete a file
rm -r my-folder/ # Delete a folder and everything inside it
Terminal window
cat file.txt # Display the entire file contents (good for small files)
head file.txt # Show the first 10 lines
head -20 file.txt # Show the first 20 lines
tail file.txt # Show the last 10 lines
tail -f log.txt # Follow file updates in real time (very useful for logs)
Terminal window
grep "error" log.txt # Search for lines containing "error" in a file
grep -r "import torch" ./ # Search all files in the current directory
grep -n "def train" model.py # Search and show line numbers

grep will become a great debugging helper in the future — it helps you quickly find where a function or variable is used across dozens of files.

Terminal window
clear # Clear the screen (or press Ctrl + L)
history # View all the commands you’ve run before
which python # Show the path of the python command (often used to diagnose environment issues)
echo "hello" # Print some text

Terminal pipe, redirection, and PATH data flow

These two concepts are where the command line becomes truly powerful.

A pipe means: take the output of the previous command and use it as the input of the next command.

Terminal window
# List all files, then find .py files among them
ls -la | grep ".py"
# Check the command history for git commands
history | grep "git"
# Count how many Python files are in the current directory
ls *.py | wc -l

You can think of a pipe as an assembly line in a factory: the output of one step becomes the input to the next.

Save a command’s output to a file instead of showing it on the screen:

Terminal window
# Save the output of ls to filelist.txt (overwrite)
ls -la > filelist.txt
# Append output to the end of a file (do not overwrite)
echo "A new line" >> notes.txt
# Save the output of a Python script to a file
python train.py > training_log.txt

> overwrites, while >> appends. In practice, these are often used to save training logs.

Terminal window
# Run a script and save both normal output and error output to a log file
python train.py > log.txt 2>&1
# Count how many lines of code are in a Python file
cat model.py | wc -l
# Find all files containing "TODO" and count them
grep -r "TODO" ./ | wc -l

Environment variables are some “global settings” stored in the system. Many programs read them to decide how to behave.

Terminal window
# View all environment variables
env
# View the value of a specific environment variable
echo $PATH
echo $HOME

The most important environment variable: PATH

Section titled “The most important environment variable: PATH”

PATH determines which directories the system searches when you type a command in the terminal.

/Users/zhangsan/miniconda3/bin
echo $PATH

These paths are separated by :. When you type python, the system looks for a file named python in each of these directories in order, and runs the first one it finds.

If you get command not found, it usually means the program is not in any directory listed in PATH.

Terminal window
# Set temporarily (only valid in the current terminal window)
export MY_API_KEY="your_api_key_here"
echo $MY_API_KEY # Output: your_api_key_here
# Verify: close the terminal and reopen it, and MY_API_KEY will be gone
Terminal window
# Set permanently (write to a configuration file)
# For macOS/Linux with zsh:
echo 'export MY_API_KEY="your_api_key_here"' >> ~/.zshrc
source ~/.zshrc # Take effect immediately
# If you use bash:
echo 'export MY_API_KEY="your_api_key_here"' >> ~/.bashrc
source ~/.bashrc

Open your terminal and complete the following steps in order:

Terminal window
# 1. Confirm where you are
pwd
# 2. Go to the Home directory
cd ~
# 3. Create a study project folder
mkdir -p ai-study/ch01-tools/terminal-practice
# 4. Enter this folder
cd ai-study/ch01-tools/terminal-practice
# 5. Create a few files
touch hello.py notes.txt data.csv
# 6. Check the files you created
ls -la
# 7. Write some content into the files
echo "print('Hello, AI!')" > hello.py
echo "Day 1 study notes" > notes.txt
# 8. View the file contents
cat hello.py
cat notes.txt
# 9. Copy notes.txt to make a backup
cp notes.txt notes_backup.txt
# 10. Confirm the backup was created successfully
ls
# 11. Append more content to notes.txt
echo "Learned the cd, ls, mkdir, touch, cp, and cat commands" >> notes.txt
cat notes.txt
# 12. Search for files containing "AI"
grep -r "AI" ./
# 13. Go back to the parent directory
cd ..
pwd

If everything worked, congratulations — you’ve already mastered the most essential command-line operations.

Operation guide and checkpoints
  1. After step 4, pwd should end with ai-study/ch01-tools/terminal-practice.
  2. ls -la should show hello.py, notes.txt, and data.csv; after the backup step, it should also show notes_backup.txt.
  3. cat hello.py should print the code text. It does not run the program; running it requires python hello.py.
  4. >> should keep the first note and append a second line. If the original text disappeared, you probably used > by mistake.
  5. grep -r "AI" ./ should find hello.py because the string appears in the code. If it finds nothing, check your current directory and capitalization.

CommandPurposeCommon options
pwdShow current directory
cdChange directory.. parent, ~ Home, - previous
lsList files-l detailed, -a hidden files
mkdirCreate a folder-p create multiple levels
touchCreate an empty file
cpCopy-r copy folders
mvMove / rename
rmDelete-r delete folders
catView file contents
head / tailView the beginning / end-n number specify line count
grepSearch text-r recursive, -n line numbers
echoPrint text
clearClear the screen
historyCommand history
whichShow command path

Keep this page’s proof of learning as a small evidence card:

Command
exact terminal command you ran
Working Dir
pwd/current folder and important files listed
Output
copied command output or screenshot of the result
Failure Check
wrong path, missing command, permission issue, or shell mismatch
Expected Output
reproducible terminal action with the command and result side by side