Command Line Cheatsheet
The command line (also called the terminal or shell) is a text interface for giving instructions to your computer. As a developer, you will use it daily to run programs, navigate files, install packages, and use Git. Comfort with the terminal is essential for web development.
Opening the terminal
- macOS: Press Cmd+Space, type “Terminal”, press Enter. Or find it in Applications > Utilities > Terminal.
- Windows: Use Git Bash (installed with Git for Windows), or Windows Terminal with PowerShell.
- Linux: Ctrl+Alt+T opens the terminal on most distributions.
Understanding paths
Files and folders on your computer are organised in a tree structure starting from a root directory.
Absolute path: starts from the root. Always works regardless of where you are.
/Users/hannah/Documents/project/index.js # macOS / Linux
C:\Users\hannah\Documents\project\index.js # Windows
Relative path: starts from your current directory. Shorter, but depends on where you are.
./src/index.js # ./ means "current directory"
../index.js # ../ means "one level up"
../../config.js # two levels up
Use ~ as a shortcut for your home directory:
~/Documents/project
Navigation
Where am I? (pwd)
pwd stands for print working directory. It shows the full path of the folder you are currently in.
pwd
# /Users/hannah/Documents/project
What is in this folder? (ls)
ls lists the files and folders in the current directory.
ls
Common options:
ls -a # show hidden files (files starting with .)
ls -l # show as a list with permissions, size, and date
ls -la # list format including hidden files
ls -lt # sort by last modified time
Move to a different folder (cd)
cd (change directory) moves you into a folder.
cd Documents
cd Documents/project # move into a nested folder
cd .. # go up one level
cd ../.. # go up two levels
cd ~ # go to your home directory
cd - # go back to the previous directory
There is a space between cd and the path. cd.. without a space may work in some shells, but cd .. is correct and portable.
Creating files and folders
Create a folder (mkdir)
mkdir my-project # create one folder
mkdir -p src/components/buttons # create nested folders in one command
Create a file (touch)
touch index.js
touch .env .gitignore # create multiple files at once
touch creates an empty file. If the file already exists, it updates its timestamp.
Write output to a file
echo "Hello, world" > hello.txt # create or overwrite the file
echo "Another line" >> hello.txt # append to the file
Copying, moving, and renaming
Copy a file (cp)
cp original.txt copy.txt # copy a file to a new file
cp index.js src/index.js # copy a file into a folder
cp -r src/ backup/ # copy a folder and all its contents (-r means recursive)
Move or rename a file (mv)
mv moves a file to a new location. If you give it a new name in the same folder, it renames the file.
mv old-name.txt new-name.txt # rename a file
mv index.js src/index.js # move a file into a folder
mv src/ app/ # rename a folder
Deleting files and folders
Warning: Deletions in the terminal are permanent. There is no Trash or Undo. Double-check the path before pressing Enter.
Delete a file (rm)
rm unwanted.txt
Delete a folder and its contents (rm -r)
rm -r old-folder/
Warning:
rm -rdeletes everything inside a folder recursively and cannot be undone. Never runrm -rf /orrm -rf ~or similar commands. These will wipe your entire filesystem or home directory.
Viewing file contents
Print a file to the terminal (cat)
cat index.js
cat package.json
Useful for short files. For long files, use less.
Scroll through a long file (less)
less README.md
Inside less:
- Arrow keys or
j/kto scroll /search-termto searchqto quit
Print a line of text (echo)
echo "Hello, world"
echo $HOME # print the value of an environment variable
Searching
Search inside files (grep)
grep searches for a text pattern in files and prints matching lines.
grep "function" index.js # find lines containing "function"
grep -r "TODO" src/ # search recursively in a folder
grep -n "import" app.js # show line numbers with matches
grep -i "error" logs.txt # case-insensitive search
Find files by name (find)
find . -name "*.js" # find all .js files in the current folder
find . -name ".env" # find a specific file
find src/ -name "*.test.js" # find test files inside src/
Sort and filter output
sort names.txt # sort lines alphabetically
sort -r names.txt # reverse sort
uniq names.txt # remove duplicate adjacent lines
sort names.txt | uniq # sort then remove duplicates
Redirect and pipes
# Send output of a command to a file
ls > filelist.txt # overwrite
ls >> filelist.txt # append
# Pipe the output of one command into another
cat names.txt | sort | uniq
grep "error" app.log | less
Useful shortcuts
Stop a running process: Ctrl+C
When you start a dev server (npm run dev, node index.js, etc.) it keeps running until you stop it. Press Ctrl+C to stop any running process.
Clear the screen
clear
Or press Ctrl+L.
Search command history
Press the Up arrow to cycle through previous commands. Press Ctrl+R and start typing to search through history.
Tab completion
Start typing a file or folder name and press Tab to autocomplete. This saves typing and prevents typos in paths.
Find and replace in files (sed)
sed is a text processing tool. The most common use is find-and-replace:
sed 's/old-text/new-text/' input.txt
sstands for substitute- The first
/word/is what to find - The second
/word/is the replacement
This prints the result without changing the original file. To save the change in place, use -i:
sed -i 's/localhost/example.com/g' config.txt
Environment variables
Environment variables store configuration values that your shell and programs can read.
echo $HOME # print the home directory path
echo $PATH # print the directories the shell searches for commands
Set a temporary variable for the current session:
export PORT=3000
For variables that should persist across sessions, add export lines to your shell config (~/.zshrc on macOS with zsh, ~/.bashrc on Linux with bash).
For project-specific variables, use a .env file with the dotenv package. See the Environment Variables page
.
Running programs
node index.js # run a Node.js file
npm install # install npm packages
npm run dev # run an npm script
python script.py # run a Python file
Most dev tools run as a process in the terminal until you press Ctrl+C.
What to read next
- Git : version control for your projects
- Node.js installation : installing Node.js and npm
- npm : managing packages and scripts
- Environment variables : using .env files in projects