What is the Command Line?
The command line, also known as the terminal, shell, or command prompt, is a text-based interface used to interact with the operating system. It allows users to execute commands to perform actions such as navigating directories, managing files, and running scripts.Common CLI Environments
- Windows: Command Prompt (
cmd.exe
), PowerShell - macOS & Linux: Terminal, Bash Shell, Zsh
Why Learn the Command Line?
- Efficiency: Faster navigation and file management.
- Automation: Execute scripts to perform repetitive tasks.
- Flexibility: Access advanced system features and configurations.
- Essential for Developers: Many programming tools require command-line knowledge.
Basic Command Line Commands
Here are some fundamental CLI commands categorized by functionality:1. Navigating the File System
Command | Description | Windows | macOS/Linux |
---|---|---|---|
pwd | Print current directory | ❌ | ✅ |
cd | Change directory | ✅ | ✅ |
ls | List files in directory | ❌ | ✅ |
dir | List files in directory | ✅ | ❌ |
2. File and Directory Management
Command | Description | Windows | macOS/Linux |
---|---|---|---|
mkdir <folder> | Create a new directory | ✅ | ✅ |
rmdir <folder> | Remove an empty directory | ✅ | ✅ |
rm -r <folder> | Remove a directory and its contents | ❌ | ✅ |
del <file> | Delete a file | ✅ | ❌ |
rm <file> | Delete a file | ❌ | ✅ |
3. Viewing File Content
Command | Description | Windows | macOS/Linux |
---|---|---|---|
type <file> | Display file contents | ✅ | ❌ |
cat <file> | Display file contents | ❌ | ✅ |
more <file> | View file one page at a time | ✅ | ✅ |
less <file> | View file with navigation | ❌ | ✅ |
4. Managing Processes
Command | Description | Windows | macOS/Linux |
---|---|---|---|
tasklist | Show running processes | ✅ | ❌ |
ps | Show running processes | ❌ | ✅ |
taskkill /F /IM <process> | Kill a process | ✅ | ❌ |
kill <PID> | Kill a process | ❌ | ✅ |
5. Networking Commands
Command | Description | Windows | macOS/Linux |
---|---|---|---|
ping <domain> | Check network connectivity | ✅ | ✅ |
ipconfig | Display network details | ✅ | ❌ |
ifconfig | Display network details | ❌ | ✅ |
tracert <domain> | Trace network route | ✅ | ❌ |
traceroute <domain> | Trace network route | ❌ | ✅ |
Advanced Command Line Usage
Using Wildcards
*.txt
- Selects all text files in a directory.?
- Represents a single character in a filename.
Chaining Commands
command1 && command2
- Runcommand2
only ifcommand1
succeeds.command1 || command2
- Runcommand2
only ifcommand1
fails.command1 ; command2
- Run both commands regardless of success.
Redirecting Output
>
- Redirect output to a file (echo Hello > output.txt
).>>
- Append output to a file (echo Hello >> output.txt
).|
- Pipe output from one command to another (ls | grep .txt
).
Real-World Exercises
To reinforce your learning, try these real-world exercises:-
Navigation Practice
- Open the terminal and navigate to different directories using
cd
. - Use
pwd
to check your current location. - List files in different directories using
ls
ordir
.
- Open the terminal and navigate to different directories using
-
File and Directory Management
- Create a directory named
Projects
and navigate into it. - Inside
Projects
, create a file callednotes.txt
usingtouch
(macOS/Linux) orecho. > notes.txt
(Windows). - Delete the file using
rm notes.txt
(Linux/macOS) ordel notes.txt
(Windows).
- Create a directory named
-
Redirecting and Piping
- Create a text file and add some lines to it using
echo
. - Use
cat
(ortype
on Windows) to view the content. - Redirect the output to another file using
>
. - Use
grep
(orfindstr
on Windows) to filter specific content from the file.
- Create a text file and add some lines to it using
-
Process Management
- Open a text editor or application.
- Use
ps
(Linux/macOS) ortasklist
(Windows) to find its process ID. - Kill the process using
kill <PID>
(Linux/macOS) ortaskkill /F /IM <process>
(Windows).
-
Networking Commands
- Use
ping google.com
to check internet connectivity. - Run
traceroute google.com
(Linux/macOS) ortracert google.com
(Windows) to see network hops. - Find your local network details using
ipconfig
orifconfig
.
- Use