Cross-Platform Terminal Commands
This page provides side-by-side comparisons of common terminal commands across different operating systems. Use this reference when switching between Linux/Mac and Windows environments.
Environment Variables
View Environment Variables
| Task | Linux/Mac | Windows PowerShell | Windows CMD |
|---|---|---|---|
| Show all variables | printenv or env | dir env: or gci env: | set |
| Show one variable | echo $JAVA_HOME or printenv JAVA_HOME | $env:JAVA_HOME or dir env:JAVA_HOME | echo %JAVA_HOME% |
| Set new variable | export NEW_VAR=value | $env:NEW_VAR="value" | set NEW_VAR=value |
| Search variables | `printenv | grep JAVA` | `dir env: |
Examples
Linux/Mac:
bash
# Show all environment variables
printenv
# Show specific variable
echo $JAVA_HOME
# Set temporary variable (current session only)
export MY_VAR="hello"
# Add to PATH
export PATH=$PATH:/usr/local/binWindows PowerShell:
powershell
# Show all environment variables
dir env:
# Show specific variable
$env:JAVA_HOME
# Set temporary variable (current session only)
$env:MY_VAR="hello"
# Add to PATH
$env:PATH += ";C:\my\path"Windows CMD:
cmd
:: Show all environment variables
set
:: Show specific variable
echo %JAVA_HOME%
:: Set temporary variable (current session only)
set MY_VAR=hello
:: Add to PATH
set PATH=%PATH%;C:\my\pathCommand Operators
| Feature | Linux/Mac | Windows CMD | Windows PowerShell |
|---|---|---|---|
| AND operator | && | && | && or ; |
| Line break | \ | ^ | ` (backtick) |
| Command substitution | $(pwd) or `pwd` | N/A | $(Get-Location) |
Examples
Linux/Mac:
bash
# Chain commands (only run second if first succeeds)
cd /tmp && ls
# Multi-line command
docker run \
-it \
--name mycontainer \
ubuntu
# Command substitution
echo "Current directory: $(pwd)"Windows CMD:
cmd
:: Chain commands
cd C:\Temp && dir
:: Multi-line command
docker run ^
-it ^
--name mycontainer ^
ubuntuWindows PowerShell:
powershell
# Chain commands
cd C:\Temp; dir
# Multi-line command
docker run `
-it `
--name mycontainer `
ubuntu
# Command substitution
Write-Output "Current directory: $(Get-Location)"File and Path Operations
| Task | Linux/Mac | Windows PowerShell | Windows CMD |
|---|---|---|---|
| Show command path | which <command> | Get-Command <command> | where <command> |
| Find file | find ./ -iname "*test*" | dir -Recurse *test* | dir /s *test* |
| Create file | touch file.txt | New-Item file.txt | type nul > file.txt |
| Remove directory | rm -rf folder | Remove-Item -Force -Recurse folder | rmdir /s /q folder |
| Copy recursively | cp -r source dest | Copy-Item -Recurse source dest | xcopy /e source dest |
Examples
Linux/Mac:
bash
# Find command location
which python
# Find files recursively
find ./ -iname "*config*"
# Create empty file
touch newfile.txt
# Remove directory and contents
rm -rf old_folder
# Copy directory recursively
cp -r ~/Documents/project ~/backup/Windows PowerShell:
powershell
# Find command location
Get-Command python
# Find files recursively
dir -Recurse *config*
# Create empty file
New-Item newfile.txt
# Remove directory and contents
Remove-Item -Force -Recurse old_folder
# Copy directory recursively
Copy-Item -Recurse C:\Users\Me\Documents\project C:\backup\Network Operations
| Task | Linux/Mac | Windows |
|---|---|---|
| Test port | telnet 192.168.1.55 80nc -vz 192.168.1.55 80 | Test-NetConnection 192.168.1.55 -Port 80telnet 192.168.1.55 80 |
| Flush DNS | sudo killall -HUP mDNSResponder (macOS)sudo /etc/init.d/networking restart (Linux) | ipconfig /flushdns |
| Show IP config | ifconfig or ip addr | ipconfig or Get-NetIPAddress |
Examples
Linux:
bash
# Test if port is open
nc -vz 192.168.1.55 80
# Flush DNS cache (varies by distribution)
sudo /etc/init.d/networking restart
# Show network interfaces
ip addr showmacOS:
bash
# Test if port is open
nc -vz 192.168.1.55 80
# Flush DNS cache
sudo killall -HUP mDNSResponder
# Show network interfaces
ifconfigWindows PowerShell:
powershell
# Test if port is open
Test-NetConnection 192.168.1.55 -Port 80
# Flush DNS cache
ipconfig /flushdns
# Show network configuration
Get-NetIPAddressFile Monitoring
| Task | Linux/Mac | Windows PowerShell |
|---|---|---|
| Watch file changes | tail -f /var/log/syslog | Get-Content -Path "file.txt" -Wait |
| Follow log in real-time | tail -f logfile.log | Get-Content -Path "logfile.log" -Wait -Tail 10 |
Examples
Linux/Mac:
bash
# Watch log file in real-time
tail -f /var/log/syslog
# Show last 20 lines and follow
tail -n 20 -f application.logWindows PowerShell:
powershell
# Watch file in real-time
Get-Content -Path "C:\logs\app.log" -Wait
# Show last 20 lines and follow
Get-Content -Path "C:\logs\app.log" -Wait -Tail 20Process Management
| Task | Linux/Mac | Windows PowerShell |
|---|---|---|
| List processes | ps aux | Get-Process |
| Kill process | kill -9 <pid> | Stop-Process -Id <pid> -Force |
| Find process by name | `ps aux | grep firefox` |
Examples
Linux/Mac:
bash
# List all processes
ps aux
# Find specific process
ps aux | grep nginx
# Kill process by PID
kill -9 1234
# Kill process by name
pkill nginxWindows PowerShell:
powershell
# List all processes
Get-Process
# Find specific process
Get-Process | Where-Object {$_.Name -like "*nginx*"}
# Kill process by PID
Stop-Process -Id 1234 -Force
# Kill process by name
Stop-Process -Name nginx -ForceTips and Tricks
Cross-Platform Compatibility
When writing scripts that need to work on multiple platforms:
- Use cross-platform tools: Tools like Node.js, Python, or Git Bash provide consistent commands across platforms
- Detect OS in scripts: Check the operating system and use appropriate commands
- Use relative paths: Avoid hardcoding platform-specific paths
- Test on all platforms: Always test scripts on target operating systems
Windows Subsystem for Linux (WSL)
WSL allows you to run a Linux environment directly on Windows:
powershell
# Install WSL (Windows 10/11)
wsl --install
# Run Linux commands on Windows
wsl ls -la
wsl grep "pattern" file.txtSee Also
- Windows Environment Variables - Complete list of Windows variables
- Linux System Monitoring - Linux system commands
- Language Syntax Comparison - Programming language syntax differences