Skip to content

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

TaskLinux/MacWindows PowerShellWindows CMD
Show all variablesprintenv or envdir env: or gci env:set
Show one variableecho $JAVA_HOME or printenv JAVA_HOME$env:JAVA_HOME or dir env:JAVA_HOMEecho %JAVA_HOME%
Set new variableexport NEW_VAR=value$env:NEW_VAR="value"set NEW_VAR=value
Search variables`printenvgrep 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/bin

Windows 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\path

Command Operators

FeatureLinux/MacWindows CMDWindows 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 ^
  ubuntu

Windows 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

TaskLinux/MacWindows PowerShellWindows CMD
Show command pathwhich <command>Get-Command <command>where <command>
Find filefind ./ -iname "*test*"dir -Recurse *test*dir /s *test*
Create filetouch file.txtNew-Item file.txttype nul > file.txt
Remove directoryrm -rf folderRemove-Item -Force -Recurse folderrmdir /s /q folder
Copy recursivelycp -r source destCopy-Item -Recurse source destxcopy /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

TaskLinux/MacWindows
Test porttelnet 192.168.1.55 80
nc -vz 192.168.1.55 80
Test-NetConnection 192.168.1.55 -Port 80
telnet 192.168.1.55 80
Flush DNSsudo killall -HUP mDNSResponder (macOS)
sudo /etc/init.d/networking restart (Linux)
ipconfig /flushdns
Show IP configifconfig or ip addripconfig 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 show

macOS:

bash
# Test if port is open
nc -vz 192.168.1.55 80

# Flush DNS cache
sudo killall -HUP mDNSResponder

# Show network interfaces
ifconfig

Windows PowerShell:

powershell
# Test if port is open
Test-NetConnection 192.168.1.55 -Port 80

# Flush DNS cache
ipconfig /flushdns

# Show network configuration
Get-NetIPAddress

File Monitoring

TaskLinux/MacWindows PowerShell
Watch file changestail -f /var/log/syslogGet-Content -Path "file.txt" -Wait
Follow log in real-timetail -f logfile.logGet-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.log

Windows 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 20

Process Management

TaskLinux/MacWindows PowerShell
List processesps auxGet-Process
Kill processkill -9 <pid>Stop-Process -Id <pid> -Force
Find process by name`ps auxgrep 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 nginx

Windows 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 -Force

Tips and Tricks

Cross-Platform Compatibility

When writing scripts that need to work on multiple platforms:

  1. Use cross-platform tools: Tools like Node.js, Python, or Git Bash provide consistent commands across platforms
  2. Detect OS in scripts: Check the operating system and use appropriate commands
  3. Use relative paths: Avoid hardcoding platform-specific paths
  4. 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.txt

See Also

Released under the MIT License.