Command Line
The command line, also known as the terminal, shell, or console, is a text-based interface for interacting with your computer’s operating system. Instead of using a graphical user interface (GUI) with windows, icons, and a mouse, you type commands to tell the computer what to do. While GUIs are great for everyday tasks, the command line is essential for developers, system administrators, and anyone who wants to work more efficiently with computers.
Why Learn the Command Line?
- Efficiency: Many tasks can be done much faster on the command line than with a mouse
- Automation: You can write scripts to automate repetitive tasks
- Remote Access: The command line is the primary way to access and manage remote servers
- Development Tools: Many development tools (like Git, Docker, and build tools) are command-line based
- System Administration: Essential for managing operating systems and servers
- Power and Flexibility: Gives you more control over your system
- Understanding How Computers Work: Helps you understand the underlying workings of your operating system
Basic Concepts
The Shell
The shell is a program that interprets your commands and communicates them to the operating system. Common shells include:
- Bash (Bourne-Again SHell): The default on most Linux distributions and macOS
- Zsh (Z Shell): Popular alternative with more features (default on newer macOS versions)
- Fish (Friendly Interactive SHell): User-friendly with auto-suggestions
- PowerShell: Microsoft’s modern shell for Windows
- Command Prompt (cmd): The traditional Windows command-line interpreter
The Prompt
The prompt is what you see when the shell is ready to accept a command. It typically includes:
- Username: The account you’re logged in as
- Hostname: The name of the computer
- Current Directory: The folder you’re currently in
- Symbol: Usually
$for regular users and#for the root user
Example prompt: username@hostname:~/documents$
Essential Commands
Here are some of the most important commands to learn, categorized by function:
File and Directory Management
| Command | Description |
|---|---|
pwd | Print Working Directory - Shows your current location |
ls | List - Shows files and directories in the current directory |
ls -l | Long format - Shows detailed information (permissions, owner, size, date) |
ls -a | Show all files, including hidden ones (starting with .) |
cd | Change Directory - Move to a different directory |
cd .. | Move up one directory level |
cd ~ | Move to your home directory |
mkdir | Make Directory - Create a new directory |
rmdir | Remove Directory - Delete an empty directory |
touch | Create an empty file or update a file’s timestamp |
cp | Copy - Copy files or directories |
mv | Move - Move or rename files or directories |
rm | Remove - Delete files or directories |
rm -r | Recursive remove - Delete directories and their contents |
File Manipulation
| Command | Description |
|---|---|
cat | Concatenate - Display file contents |
less | View file contents one page at a time |
more | Similar to less, but older and less feature-rich |
head | Display the beginning of a file |
tail | Display the end of a file |
tail -f | Follow - Display file contents as they’re written (great for logs) |
nano | Simple text editor |
vim | Powerful text editor (steeper learning curve) |
emacs | Another powerful text editor |
System Information
| Command | Description |
|---|---|
whoami | Show current username |
date | Display current date and time |
cal | Display calendar |
df | Disk Free - Show disk space usage |
du | Disk Usage - Show file/directory space usage |
free | Show memory usage |
top | Display running processes (real-time) |
htop | Enhanced version of top (often needs to be installed) |
ps | Process Status - List running processes |
kill | Terminate a process |
Network Commands
| Command | Description |
|---|---|
ping | Test network connectivity to a host |
ip addr | Show IP addresses and network interfaces (Linux) |
ifconfig | Show network interfaces (older, but still common) |
netstat | Show network connections |
curl | Transfer a URL - Make HTTP requests |
wget | Download files from the web |
ssh | Secure Shell - Connect to remote servers |
scp | Secure Copy - Copy files between computers |
Permissions
| Command | Description |
|---|---|
chmod | Change Mode - Change file permissions |
chown | Change Owner - Change file ownership |
chgrp | Change Group - Change file group |
Command Line Tips and Tricks
1. Tab Completion
Press the Tab key to auto-complete commands, filenames, and directory names. Press Tab twice to see all possible completions.
2. Command History
- Use the up and down arrow keys to navigate through previously executed commands
- Press
Ctrl+Rto search your command history - Type
historyto see a list of all commands you’ve run
3. Wildcards
Wildcards allow you to match multiple files with a single pattern:
*: Matches zero or more characters (e.g.,*.txtmatches all files ending with.txt)?: Matches exactly one character (e.g.,file?.txtmatchesfile1.txt,file2.txt, etc.)[]: Matches any character within the brackets (e.g.,file[123].txtmatchesfile1.txt,file2.txt,file3.txt)
4. Input/Output Redirection
Redirect the output of a command to a file or use a file as input:
>: Redirect output (overwrites the file)>>: Append output (adds to the end of the file)<: Redirect input from a file
Examples:
ls -l > file_list.txt # Save file list to a file
cat file.txt >> another.txt # Append file contents to another file
wc -l < file.txt # Count lines in a file
5. Pipes
Use the pipe operator | to send the output of one command as input to another command:
ls -l | grep .txt # List files and filter for .txt files
ps aux | grep nginx # Find processes related to nginx
df -h | grep /dev/sda1 # Check disk space for a specific partition
6. Aliases
Create shortcuts for long commands:
alias ll='ls -lh'
alias update='sudo apt update && sudo apt upgrade'
alias cls='clear'
7. Background Processes
Run a command in the background by adding & at the end:
./my_long_running_script.sh &
8. Job Control
Ctrl+C: Interrupt (stop) the current foreground processCtrl+Z: Suspend the current foreground processjobs: List suspended jobsfg: Bring a suspended job to the foregroundbg: Resume a suspended job in the background
9. Command Chaining
Execute multiple commands in sequence:
&&: Run the next command only if the previous one succeeded||: Run the next command only if the previous one failed;: Run commands sequentially regardless of success
Examples:
mkdir my_Premium Content
Unlock Command Line and all premium lessons with a subscription.
From ₹199.99/year — See plans