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
2. File and Directory Management
3. Viewing File Content
4. Managing Processes
5. Networking Commands
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- Runcommand2only ifcommand1succeeds.command1 || command2- Runcommand2only ifcommand1fails.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
pwdto check your current location. - List files in different directories using
lsordir.
- Open the terminal and navigate to different directories using
-
File and Directory Management
- Create a directory named
Projectsand navigate into it. - Inside
Projects, create a file callednotes.txtusingtouch(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(ortypeon Windows) to view the content. - Redirect the output to another file using
>. - Use
grep(orfindstron 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.comto check internet connectivity. - Run
traceroute google.com(Linux/macOS) ortracert google.com(Windows) to see network hops. - Find your local network details using
ipconfigorifconfig.
- Use