Mastering Command Line Basics: A Beginner’s Guide
Introduction
For many new developers, the command line can seem intimidating—a black screen with a blinking cursor, waiting for you to type mysterious commands. However, becoming comfortable with the command line is an essential skill that will make you more efficient and open up powerful capabilities that aren’t available through graphical interfaces.
In this guide, I’ll walk you through the fundamental command line skills every developer should know, with practical examples that you can try right away.
Why Learn the Command Line?
Before diving into commands, let’s understand why the command line is worth learning:
- Efficiency: Many tasks are faster to perform with commands than clicking through interfaces
- Automation: You can script repetitive tasks to save time
- Remote access: You can work on remote servers where graphical interfaces aren’t available
- Developer tools: Many development tools are primarily or exclusively used via command line
- Understanding: It gives you a deeper understanding of how your computer works
Getting Started
Accessing the Terminal
On macOS:
- Open Spotlight (Cmd+Space) and type “Terminal”
- Or find it in Applications → Utilities → Terminal
On Windows:
- Windows has Command Prompt and PowerShell, but for a Unix-like experience, I recommend installing Windows Subsystem for Linux (WSL) or Git Bash
On Linux:
- Usually Ctrl+Alt+T or find Terminal in your applications menu
Understanding the Prompt
When you open the terminal, you’ll see something like:
username@hostname:~$
This tells you:
- Your username
- The hostname (computer name)
- Your current directory (
~
is shorthand for your home directory) $
indicates you’re using a regular user account (not root)
The prompt is where you’ll type commands.
Essential Navigation Commands
1. pwd (Print Working Directory)
The pwd
command shows your current location in the file system:
pwd
Output might look like:
/home/username
2. ls (List)
The ls
command lists files and directories in your current location:
ls
Common options:
ls -l # Long format with details
ls -a # Show hidden files (those starting with .)
ls -la # Combine options for long format and hidden files
ls -lh # Human-readable file sizes
Example output of ls -l
:
drwxr-xr-x 2 username group 4096 Mar 15 14:30 Documents
-rw-r--r-- 1 username group 8980 Mar 10 09:15 example.txt
This shows:
- File permissions
- Number of links
- Owner
- Group
- Size
- Last modified date
- Name
3. cd (Change Directory)
The cd
command lets you navigate between directories:
cd Documents # Go to Documents folder
cd /home/username/Downloads # Go to a specific path
cd .. # Go up one directory
cd ~ # Go to home directory
cd - # Go to previous directory
Working with Files and Directories
1. mkdir (Make Directory)
Create new directories:
mkdir Projects # Create a directory
mkdir -p Projects/Web/CSS # Create nested directories
2. touch
Create empty files or update timestamps of existing files:
touch file.txt # Create an empty file
touch file1.txt file2.txt # Create multiple files
3. cp (Copy)
Copy files and directories:
cp file.txt backup.txt # Copy a file
cp -r folder newfolder # Copy a directory and its contents
4. mv (Move/Rename)
Move or rename files and directories:
mv file.txt Documents/ # Move a file to another directory
mv oldname.txt newname.txt # Rename a file
mv folder newlocation/ # Move a directory
5. rm (Remove)
Delete files and directories (be careful, there’s no recycle bin!):
rm file.txt # Delete a file
rm -r folder # Delete a directory and its contents
rm -i file.txt # Interactive mode (asks for confirmation)
rm -f file.txt # Force deletion without confirmation
⚠️ Warning: Be extremely careful with rm -rf
as it can delete everything without asking!
6. cat (Concatenate)
Display file contents:
cat file.txt # Show file contents
cat file1.txt file2.txt # Show contents of multiple files
7. less
View file contents with pagination (useful for large files):
less file.txt
Navigation in less:
- Space: Next page
- b: Previous page
- /pattern: Search for “pattern”
- n: Next search result
- q: Quit
File Permissions
In Unix-like systems, files and directories have permissions that control who can read, write, or execute them.
Understanding Permission Notation
When you run ls -l
, you’ll see something like:
-rw-r--r-- 1 username group 8980 Mar 10 09:15 example.txt
The first 10 characters represent:
- 1st character: File type (
-
for regular file,d
for directory) - 2-4th characters: Owner permissions (read, write, execute)
- 5-7th characters: Group permissions
- 8-10th characters: Others permissions
Where:
r
= read permissionw
= write permissionx
= execute permission-
= no permission
chmod (Change Mode)
Change file permissions:
chmod u+x script.sh # Add execute permission for the owner
chmod g+w file.txt # Add write permission for the group
chmod o-r file.txt # Remove read permission for others
chmod 755 script.sh # Set specific permissions using octal notation
Octal notation:
- 4 = read
- 2 = write
- 1 = execute
Common permission combinations:
- 755 (rwxr-xr-x): Owner can read/write/execute, others can read/execute
- 644 (rw-r–r–): Owner can read/write, others can read
- 700 (rwx------): Owner can read/write/execute, no permissions for others
Redirecting Output
Redirecting to Files
command > file.txt # Redirect output to a file (overwrite)
command >> file.txt # Append output to a file
command 2> errors.txt # Redirect error messages to a file
command > output.txt 2> errors.txt # Redirect output and errors separately
command > output.txt 2>&1 # Redirect both output and errors to the same file
Piping
Pipes (|
) connect the output of one command to the input of another:
ls -l | grep ".txt" # List files and filter for those containing ".txt"
cat file.txt | wc -l # Count lines in a file
history | grep "git commit" # Find git commit commands in your history
Useful Text Processing Commands
1. grep (Global Regular Expression Print)
Search for patterns in files:
grep "pattern" file.txt # Search for "pattern" in file.txt
grep -i "pattern" file.txt # Case-insensitive search
grep -r "pattern" directory/ # Recursive search in directory
grep -n "pattern" file.txt # Show line numbers
grep -v "pattern" file.txt # Show lines that don't match
2. head and tail
View the beginning or end of a file:
head file.txt # Show first 10 lines
head -n 5 file.txt # Show first 5 lines
tail file.txt # Show last 10 lines
tail -n 20 file.txt # Show last 20 lines
tail -f log.txt # Follow the file (show updates in real-time)
3. wc (Word Count)
Count lines, words, and characters:
wc file.txt # Show lines, words, and characters
wc -l file.txt # Count lines only
wc -w file.txt # Count words only
wc -c file.txt # Count characters only
4. sort
Sort lines in a file:
sort file.txt # Sort alphabetically
sort -r file.txt # Sort in reverse
sort -n file.txt # Sort numerically
5. uniq
Filter out duplicate lines (works best after sorting):
sort file.txt | uniq # Show unique lines
sort file.txt | uniq -c # Count occurrences of each line
Finding Files
1. find
Search for files and directories:
find . -name "*.txt" # Find all .txt files in current directory and subdirectories
find /home -type d -name "Projects" # Find directories named "Projects"
find . -type f -size +1M # Find files larger than 1 MB
find . -type f -mtime -7 # Find files modified in the last 7 days
2. which
Find the location of a command:
which python # Show the path to the Python executable
which npm # Show the path to npm
Command History and Shortcuts
History
View and reuse previous commands:
history # Show command history
!42 # Run command number 42 from history
!! # Run the previous command
!string # Run the most recent command starting with "string"
Keyboard Shortcuts
Ctrl+C
: Interrupt (cancel) the current commandCtrl+D
: Exit the current shellCtrl+L
: Clear the screenCtrl+A
: Move cursor to beginning of lineCtrl+E
: Move cursor to end of lineCtrl+U
: Delete from cursor to beginning of lineCtrl+K
: Delete from cursor to end of lineCtrl+W
: Delete the word before the cursorTab
: Auto-complete commands, file names, and directoriesUp/Down arrows
: Navigate through command history
Environment Variables
Environment variables store information that can be used by the shell and programs.
Viewing Variables
echo $HOME # Show the value of HOME variable
echo $PATH # Show the PATH variable (where commands are searched)
env # List all environment variables
Setting Variables
export NAME="John" # Set a variable for the current session
echo $NAME # Use the variable
To make variables permanent, add them to your shell configuration file (e.g., ~/.bashrc
or ~/.zshrc
).
Practical Examples
Let’s put these commands together in some practical examples:
Example 1: Creating a Project Structure
# Create a new web project
mkdir -p my-project/{css,js,images}
touch my-project/index.html
touch my-project/css/style.css
touch my-project/js/script.js
echo "<!DOCTYPE html><html><head><title>My Project</title></head><body><h1>Hello World</h1></body></html>" > my-project/index.html
Example 2: Finding and Replacing Text
# Find all JavaScript files containing "function" and replace "var" with "let"
find . -name "*.js" -type f -exec grep -l "function" {} ; | xargs sed -i 's/var /let /g'
Example 3: Analyzing Log Files
# Count occurrences of each HTTP status code in an Apache log
grep -o "HTTP/1.1" [0-9]{3}" access.log | sort | uniq -c | sort -nr
Example 4: Monitoring a Process
# Watch the CPU and memory usage of a specific process
watch -n 1 "ps aux | grep [n]odejs"
Command Line Tips for Beginners
- Start small: Learn a few commands at a time and practice them
- Use tab completion: Press Tab to auto-complete commands and file names
- Read the manual: Use
man command
to read the manual for any command - Create aliases: Define shortcuts for commands you use frequently
- Use the history: Press Up arrow to cycle through previous commands
- Be careful with destructive commands: Double-check before using
rm
, especially with wildcards - Make use of –help: Most commands support
--help
flag for quick reference
Creating Aliases
Aliases are shortcuts for commands you use frequently:
# Add to your ~/.bashrc or ~/.zshrc
alias ll='ls -la'
alias gs='git status'
alias gp='git push'
After adding aliases, run source ~/.bashrc
(or your shell’s config file) to apply them.
Customizing Your Terminal
You can customize your terminal to make it more useful and visually appealing:
- Install a better shell: Consider Zsh with Oh My Zsh
- Use a color theme: Many terminals support custom color schemes
- Configure your prompt: Customize what information appears in your prompt
- Add useful information: Show git branch, current directory, etc.
Conclusion
The command line is a powerful tool that becomes more valuable the more you use it. Don’t try to memorize every command at once—start with the basics and gradually expand your knowledge as you need new functionality.
Remember these key points:
- Navigation:
pwd
,ls
,cd
- File operations:
mkdir
,touch
,cp
,mv
,rm
- Viewing content:
cat
,less
,head
,tail
- Finding things:
grep
,find
- Redirecting output:
>
,>>
,|
With these fundamentals, you’ll be well on your way to becoming proficient with the command line. As you grow more comfortable, you’ll discover that many tasks are faster and more powerful when performed in the terminal.
Further Resources
- The Linux Command Line - A comprehensive guide
- Explain Shell - Explains command line arguments
- Command Line Cheat Sheet
- Bash Scripting Tutorial