Basic Terminal Commands
Here you will learn the terminal commands engineers use every day. You do not need to memorize everything at once. Learn them little by little as you use them.
Command List
Section titled “Command List”| Command | Meaning | Main Use |
|---|---|---|
pwd | Show current location | pwd |
ls | List files and folders | ls, ls -la |
cd | Change directories | cd Documents, cd .., cd ~ |
mkdir | Create a folder | mkdir my-project |
touch | Create a file | touch index.html |
rm | Delete files or folders | rm file.txt, rm -rf folder/ |
cp | Copy files or folders | cp a.txt b.txt |
mv | Move or rename files or folders | mv old.txt new.txt |
cat | Show file contents | cat README.md |
open . | Open the current folder in Finder | open . |
clear | Clear the screen | clear or Ctrl + L |
Command Details
Section titled “Command Details”pwd - Check Where You Are
Section titled “pwd - Check Where You Are”Short for Print Working Directory. It shows the path of the folder you are currently working in.
pwdThe output looks like this:
/Users/usernameIt is the first command to run when you get lost.
ls - List Files and Folders
Section titled “ls - List Files and Folders”Short for List. It shows the contents of the current folder.
lsAdd the -la option to show hidden files and detailed information.
ls -latotal 48
drwxr-xr-x 8 username staff 256 Mar 21 10:00 .
drwxr-xr-x 12 username staff 384 Mar 20 09:00 ..
-rw-r--r-- 1 username staff 1234 Mar 21 10:00 README.md
drwxr-xr-x 5 username staff 160 Mar 21 10:00 srcFiles and folders that start with . are hidden files. They do not appear in normal ls output, but you can see them with -la.
cd - Move Between Folders
Section titled “cd - Move Between Folders”Short for Change Directory. Use it to switch the folder you are working in.
# Move to your home folder
cd ~
# Move to the Documents folder
cd Documents
# Move up one folder
cd ..
# Move up two folders
cd ../..
# Move by specifying the full path
cd /Users/username/Documents/my-projectTip: While typing a folder name after
cd, pressTabto auto-complete it.
mkdir - Create a Folder
Section titled “mkdir - Create a Folder”Short for Make Directory. It creates a new folder.
mkdir my-projectUse the -p option to create missing parent folders at the same time.
mkdir -p my-project/src/componentstouch - Create a File
Section titled “touch - Create a File”Creates an empty file.
touch index.html
touch README.md
touch .envrm - Delete Files or Folders
Section titled “rm - Delete Files or Folders”Short for Remove. It deletes files.
# Delete a file
rm old-file.txtUse the -rf option to delete a folder and everything inside it.
rm -rf old-folder/Warning: Files deleted with
rm -rfdo not go to the Trash. They are deleted immediately and permanently. Always check the target before running it. Never runrm -rf /orrm -rf ~/.
cp - Copy Files or Folders
Section titled “cp - Copy Files or Folders”Short for Copy.
# Copy a file
cp README.md README.md.backup
# Copy a folder (`-r` is required)
cp -r src/ src-backup/mv - Move or Rename Files or Folders
Section titled “mv - Move or Rename Files or Folders”Short for Move. It is used for both moving and renaming.
# Move a file to another folder
mv index.html src/index.html
# Rename a file
mv old-name.txt new-name.txtcat - Show File Contents
Section titled “cat - Show File Contents”Short for Concatenate. It prints the contents of a file in the terminal.
cat README.md
cat package.jsonopen . - Open in Finder on Mac
Section titled “open . - Open in Finder on Mac”Opens the current folder in Finder, which is useful when switching between the terminal and the GUI.
open .You can also open a specific file with its default app:
open index.htmlclear / Ctrl + L - Clear the Screen
Section titled “clear / Ctrl + L - Clear the Screen”Clears the terminal screen. It only hides old commands; it does not delete your work.
clearYou can also use Ctrl + L for the same effect.
Time-Saving Tips
Section titled “Time-Saving Tips”Tab Completion (Most Important)
Section titled “Tab Completion (Most Important)”If you type part of a command or file name and press Tab, the terminal auto-completes it.
cd Doc[Tab]
# -> completes to cd Documents/If multiple candidates exist, press Tab again to see the list.
Use the Up and Down Arrow Keys to Access History
Section titled “Use the Up and Down Arrow Keys to Access History”Pressing ↑ shows the last command you ran. It saves you from retyping commands you want to repeat.
↑ # one command back
↑ # two commands back
↓ # one command forwardPractice: Create a Project Folder and Files
Section titled “Practice: Create a Project Folder and Files”Follow these steps to create a project folder with commands.
# 1. Move to your home folder
cd ~
# 2. Check where you are
pwd
# -> /Users/username
# 3. Move to the Documents folder
cd Documents
# 4. Create a project folder
mkdir my-first-project
# 5. Move into the folder you created
cd my-first-project
# 6. Check the folder contents (it should be empty)
ls
# 7. Create files
touch index.html
touch README.md
# 8. Confirm the files were created
ls
# -> index.html README.md
# 9. Write text into README.md
echo "# My First Project" > README.md
# 10. Check the contents
cat README.md
# -> # My First Project
# 11. Open it in Finder
open .Common Mistakes and Fixes
Section titled “Common Mistakes and Fixes”cd: no such file or directory
Section titled “cd: no such file or directory”The folder does not exist or the name is misspelled.
# Bad: typo
cd Docuemnts
# Good: use Tab completion
cd Doc[Tab]Permission denied
Section titled “Permission denied”You do not have permission to access that file or folder. You may be trying to operate on a system file. In normal learning work, staying inside your home folder (~/) usually avoids this error.
The command seems stuck
Section titled “The command seems stuck”Press Ctrl + C to stop the current command. Use it when the terminal appears frozen.
You worked in the wrong folder
Section titled “You worked in the wrong folder”Check your current location with pwd, then return to the home folder with cd ~ and move to the correct folder again.
Next: Homebrew Installation