๐ macOS Shell Basics
macOS provides a Unix-like command-line environment built on Darwin, Apple's Unix foundation. Understanding the fundamentals of macOS shell usage is essential for effective system administration and development.
๐ฏ macOS Shell Environment
Default Shell Evolution
macOS has evolved its default shell over the years:
1
2
3
4
5
6
7
8
9
10
11
12
13
14 | # Check current shell
echo $SHELL
# Check available shells
cat /etc/shells
# macOS Catalina+ default shell
zsh # Z Shell (since macOS 10.15)
# Earlier versions used bash
bash # Bourne Again Shell
# Traditional Unix shell
sh # Bourne Shell
|
Terminal Applications
macOS includes several terminal options:
1
2
3
4
5
6
7
8
9
10
11
12 | # Built-in Terminal.app
# Location: /Applications/Utilities/Terminal.app
# Third-party alternatives:
# - iTerm2 (popular choice)
# - Hyper
# - Alacritty
# - Kitty
# Customize Terminal preferences:
# Preferences โ Profiles โ Shell
# Set shell to: /bin/zsh or /bin/bash
|
๐ง Basic Command Structure
Core Unix Commands
macOS includes standard BSD-derived Unix commands:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 | # File operations
ls -la # List files with details
cp file1.txt file2.txt # Copy files
mv oldname.txt newname.txt # Move/rename files
rm file.txt # Remove files
mkdir newdir # Create directory
rmdir emptydir # Remove empty directory
rm -rf directory/ # Remove directory and contents
# File viewing and editing
cat file.txt # Display file contents
less file.txt # Page through file
head -n 10 file.txt # First 10 lines
tail -n 10 file.txt # Last 10 lines
nano file.txt # Simple text editor
vim file.txt # Advanced text editor
# System information
uname -a # System information
whoami # Current user
pwd # Present working directory
date # Current date/time
uptime # System uptime
|
Process Management
| # Process viewing
ps aux # All processes
top # Interactive process viewer
htop # Enhanced top (if installed)
activity monitor # GUI process manager
# Process control
kill 1234 # Terminate process by PID
kill -9 1234 # Force terminate process
killall Safari # Terminate all Safari processes
pkill -f "process_name" # Kill by process name pattern
|
๐ File System Navigation
macOS Directory Structure
macOS follows a hybrid Unix/macOS directory structure:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 | # Core directories
/ # Root directory
/Applications # User applications
/System # System files
/Users # User home directories
/Library # System-wide library files
/private # Private system files
/Volumes # Mounted volumes
/tmp # Temporary files
# User-specific directories
~/ # Home directory
~/Desktop # Desktop folder
~/Documents # Documents folder
~/Downloads # Downloads folder
~/Library # User library files
# System configuration
/etc # Symbolic link to /private/etc
/etc/hosts # Host name resolution
/etc/passwd # User account information
/etc/group # Group information
|
File Permissions and Ownership
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 | # View file permissions
ls -l filename # Long format listing
stat filename # Detailed file information
# Permission modification
chmod 755 script.sh # Set permissions (rwxr-xr-x)
chmod +x script.sh # Make executable
chmod -R 755 directory/ # Recursively set permissions
# Ownership modification
sudo chown user:group file.txt # Change owner and group
sudo chown user file.txt # Change owner only
sudo chgrp group file.txt # Change group only
# Permission checking
[ -r file.txt ] && echo "Readable"
[ -w file.txt ] && echo "Writable"
[ -x file.txt ] && echo "Executable"
|
๐ Network Operations
Network Configuration
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 | # Network interface information
ifconfig # All network interfaces
ifconfig en0 # Specific interface
networksetup -listallhardwareports # Hardware ports
# IP address configuration
ipconfig getifaddr en0 # Get IP address
dig google.com # DNS lookup
nslookup google.com # Alternative DNS lookup
ping google.com # Network connectivity test
traceroute google.com # Route tracing
# Network services
netstat -an # All network connections
lsof -i # Processes using network
nmap localhost # Port scanning (if installed)
|
File Transfer and Remote Access
1
2
3
4
5
6
7
8
9
10
11
12
13 | # Secure copy
scp file.txt user@host:/path/
scp user@host:/path/file.txt .
# Secure shell
ssh user@hostname
ssh -i ~/.ssh/key.pem user@host
# Remote file system mounting
sshfs user@host:/remote/path /local/mountpoint
# Built-in file sharing
# System Preferences โ Sharing โ File Sharing
|
๐ ๏ธ System Administration
Package Management
macOS offers multiple package management options:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 | # Homebrew (most popular)
brew install package_name
brew update # Update package list
brew upgrade # Upgrade installed packages
brew search term # Search for packages
brew list # List installed packages
brew uninstall package # Remove package
# MacPorts (alternative)
sudo port install package_name
sudo port update
sudo port upgrade outdated
# Built-in software update
softwareupdate -l # List available updates
softwareupdate -i -a # Install all updates
|
System Preferences and Configuration
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 | # System preference panes via command line
open /System/Library/PreferencePanes/
# System information
system_profiler SPSoftwareDataType # Software info
system_profiler SPHardwareDataType # Hardware info
sw_vers # macOS version
# Disk utility operations
diskutil list # List disks
diskutil info disk0 # Disk information
diskutil repairVolume / # Repair startup disk
# Launch services
launchctl list # List launch agents
launchctl load plist_file # Load agent
launchctl unload plist_file # Unload agent
|
๐จ Development Environment
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 | # Xcode Command Line Tools
xcode-select --install # Install developer tools
# Version control
git --version # Check Git installation
git clone repo_url # Clone repository
git status # Check repository status
# Compilers and build tools
gcc --version # GNU Compiler Collection
clang --version # Apple's LLVM compiler
make # Build automation
cmake # Cross-platform build system
# Scripting languages
python3 --version # Python 3
ruby --version # Ruby
node --version # Node.js
|
Environment Variables and Paths
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 | # View environment variables
env # All environment variables
echo $PATH # PATH variable
echo $HOME # Home directory
echo $USER # Current user
# Modify PATH temporarily
export PATH="/usr/local/bin:$PATH"
# Persistent environment setup
echo 'export PATH="/usr/local/bin:$PATH"' >> ~/.zshrc
# Shell configuration files
~/.zshrc # Zsh configuration
~/.bash_profile # Bash login configuration
~/.bashrc # Bash configuration
|
๐ Advanced Features
Spotlight Integration
| # Command-line Spotlight search
mdfind "keyword" # Search for files
mdls file.txt # List file metadata
mdimport file.txt # Import file metadata
|
Automation and Scripting
1
2
3
4
5
6
7
8
9
10
11
12 | # AppleScript integration
osascript -e 'display notification "Hello" with title "Notification"'
# Automator workflows
automator -i input workflow.workflow
# Shell scripting basics
#!/bin/bash
echo "Hello, World!"
for file in *.txt; do
echo "Processing $file"
done
|
๐งพ Summary Quick Reference
Essential Commands
| Category |
Command |
Description |
| Navigation |
cd, pwd, ls |
Directory navigation |
| Files |
cp, mv, rm, mkdir |
File operations |
| Viewing |
cat, less, head, tail |
File content viewing |
| Processes |
ps, top, kill |
Process management |
| Network |
ping, ifconfig, netstat |
Network operations |
| System |
uname, df, du |
System information |
macOS-Specific Locations
| Path |
Purpose |
/Applications |
Installed applications |
/Users/username |
User home directory |
/System/Library |
System frameworks |
/Library |
System-wide libraries |
~/Library |
User-specific libraries |
/private/var/log |
System logs |
๐ง Best Practices
macOS Shell Usage Guidelines
โ
Recommended Practices:
- Use Homebrew for package management
- Keep system updated regularly
- Use version control for configuration files
- Leverage built-in security features
- Understand file system permissions
- Use appropriate editors for tasks
โ Common Pitfalls to Avoid:
- Running commands with sudo unnecessarily
- Modifying system files without backup
- Installing software outside package managers
- Ignoring file permissions
- Not understanding directory structure
- Using deprecated commands
Security Considerations
1
2
3
4
5
6
7
8
9
10
11
12
13 | # Enable firewall
# System Preferences โ Security & Privacy โ Firewall
# Gatekeeper settings
sudo spctl --master-disable # Allow apps from anywhere (not recommended)
sudo spctl --master-enable # Re-enable Gatekeeper
# SSH security
sudo systemsetup -setremotelogin on/off
# Configure in /etc/ssh/sshd_config
# FileVault encryption
# System Preferences โ Security & Privacy โ FileVault
|
๐งพ See Also