To master the basics of the command line, you'll want to familiarize yourself with three popular shells:
- Bash (Bourne Again Shell) – Common in Linux and macOS.
- CMD (Command Prompt) – Windows command-line interpreter.
- PowerShell – A more advanced command-line interface for Windows, with scripting capabilities.
Here’s a breakdown of essential commands for each of these:
1. Bash Basics (Linux/macOS)
Navigating Directories:
ls
: List directory contents.cd <directory>
: Change directory.
Searching Files:
grep <pattern> <file>
: Search for a pattern inside a file.find
: Search for files in a directory hierarchy.
Text Processing:
awk
: Process text files (often used for data extraction).sed
: Stream editor for modifying files.
Automation (Cron Jobs):
- Cron Jobs: Automate tasks on Linux/macOS (for scheduled tasks).
- To list cron jobs:
- Edit cron jobs:
- A cron job example to run a script every day at midnight:
- To list cron jobs:
- Cron Jobs: Automate tasks on Linux/macOS (for scheduled tasks).
2. CMD (Windows)
Navigating Directories:
dir
: List files in the current directory.cd <directory>
: Change directory.
Searching Files:
findstr <pattern> <file>
: Search for a pattern inside a file.
Text Processing:
- For basic text manipulation, you might use batch scripts with commands like
for
,set
, andecho
, but CMD lacks tools likeawk
andsed
by default.
- For basic text manipulation, you might use batch scripts with commands like
Automation (Task Scheduler):
- For scheduled tasks in Windows, use Task Scheduler instead of cron:
- Open Task Scheduler > Create Task > Set triggers and actions.
- For scheduled tasks in Windows, use Task Scheduler instead of cron:
3. PowerShell Basics (Windows)
Navigating Directories:
Get-ChildItem
(aliasls
): List files in the current directory.Set-Location <path>
(aliascd
): Change directory.
Searching Files:
Select-String <pattern> <file>
: Search for a pattern inside a file.
Text Processing:
- PowerShell has more advanced text manipulation features than CMD.
Select-Object
,Where-Object
, andForEach-Object
are commonly used for filtering and processing text.
Automation (Task Scheduler):
- Similar to CMD, PowerShell can create and manage tasks through Task Scheduler.
- Use
New-ScheduledTask
cmdlets to automate tasks.
- Use
- Similar to CMD, PowerShell can create and manage tasks through Task Scheduler.
Tips for Mastery:
- Practice Regularly: Start using the terminal every day. You can practice by doing basic file operations, text processing, and automation tasks.
- Learn Scripting: Both PowerShell and Bash support scripting. Learn how to write simple scripts to automate repetitive tasks.
- Read Documentation: Use
man <command>
in Bash to read the manual pages for commands (e.g.,man ls
), orGet-Help <command>
in PowerShell (e.g.,Get-Help Get-ChildItem
). - Experiment with Real Projects: Try creating cron jobs or task scheduler jobs to run scripts that monitor system health or perform backups.
0 Comments