Przejdź do treści

🍏 macOS BSD Userland Differences

macOS inherits much of its userland from BSD but has evolved with Apple-specific modifications and GNU tool additions. Understanding these differences is crucial for effective system administration and cross-platform scripting.


🎯 Core Philosophy Differences

Design Philosophy

macOS BSD Userland: - Hybrid approach combining BSD foundations with Apple enhancements - Focus on user experience and integration with macOS ecosystem - Selective adoption of GNU tools for compatibility - Emphasis on graphical interface with command-line support - Closed-source proprietary components alongside open-source tools

Traditional BSD: - Pure BSD toolchain maintained by BSD projects - Conservative development with emphasis on stability - Complete open-source toolchain - Server-oriented design philosophy - Community-driven development


🔧 Command Syntax Differences

ls - List Directory Contents

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# macOS ls (BSD-based with GNU-like extensions)
ls -lahG                   # -G for color, no --color support
ls -@                      # Show extended attributes
ls -O                      # Show file flags

# Linux ls
ls -lah --color=auto       # GNU color support
ls --group-directories-first  # Group directories first

# Portable approach
if ls --version >/dev/null 2>&1; then
    # GNU version
    LS_COLOR="--color=auto"
    LS_OPTS="--group-directories-first"
else
    # BSD version
    LS_COLOR="-G"
    LS_OPTS=""
fi

ls -lah $LS_COLOR $LS_OPTS

grep - Pattern Searching

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# macOS grep (BSD-based)
grep -E "pattern" file     # Extended regex
grep -i "pattern" file     # Case insensitive
grep -r "pattern" .        # Recursive search

# macOS lacks some GNU extensions
# No --exclude-dir, --include, --color options

# Linux grep
grep -P "pattern" file     # Perl-compatible regex (GNU only)
grep --exclude-dir=.git "pattern" .  # Directory exclusion

# Portable approach
if grep --help 2>&1 | grep -q "perl-regexp"; then
    # GNU grep
    GREP_EXTENDED="-P"
else
    # BSD grep
    GREP_EXTENDED="-E"
fi
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# macOS find (BSD-based)
find . -name "*.txt" -mtime -7
find . -type f -name "*.log" -print0 | xargs -0 rm

# macOS lacks GNU-specific features:
# No -regextype, --daystart, -printf options

# macOS specific features:
find . -name "*.txt" -D tree  # Debug tree output

# Portable approach for common operations
find_files_by_extension() {
    local ext="$1"
    local days="${2:-7}"

    find . -name "*.${ext}" -mtime -${days} -type f
}

📋 System Administration Tools

Package Management

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# macOS package management (third-party)
# Homebrew (most popular)
brew list                  # List installed packages
brew search package-name   # Search for packages
brew install package-name  # Install package
brew uninstall package-name # Remove package
brew upgrade               # Upgrade all packages
brew outdated              # Check for outdated packages

# MacPorts (alternative)
port installed             # List installed packages
port search package-name   # Search packages
port install package-name  # Install package
port uninstall package-name # Remove package
port upgrade installed     # Upgrade all packages

# System updates (Apple)
softwareupdate -l          # List available updates
softwareupdate -i -a       # Install all updates

# Linux equivalents (varies by distribution)
# Debian/Ubuntu: apt-get, aptitude
# Red Hat/CentOS: yum, dnf
# Arch Linux: pacman

Service Management

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# macOS service management (launchd)
launchctl list             # List services
launchctl load /path/to/plist  # Load service
launchctl unload /path/to/plist # Unload service
launchctl start service    # Start service
launchctl stop service     # Stop service

# Check service status
sudo serveradmin status service-name

# Linux systemd equivalents
# systemctl start sshd
# systemctl stop sshd
# systemctl restart sshd
# systemctl status sshd
# systemctl enable sshd

System Information

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
# macOS system information
sw_vers                    # macOS version
system_profiler            # Detailed system information
system_profiler SPHardwareDataType  # Hardware info
uname -a                   # System information
sysctl hw.memsize          # Physical memory
sysctl kern.hostname       # Hostname
dmesg                      # Boot messages

# Network information
networksetup -listallhardwareports  # Network interfaces
ifconfig                   # Interface configuration
netstat -rn                # Routing table

# Linux equivalents
# uname -a
# cat /etc/os-release
# free
# hostname
# dmesg
# lspci
# lsusb

🛠️ Network Tools

Network Configuration

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
# macOS network configuration
ifconfig                   # Show all interfaces
ifconfig en0               # Show specific interface

# macOS-specific network tools
networksetup -listallnetworkservices  # List services
networksetup -setairportpower en0 on  # Enable WiFi
networksetup -getcomputername         # Computer name

# DNS configuration
scutil --dns               # Show DNS configuration
dscacheutil -flushcache    # Flush DNS cache

# Firewall management
/usr/libexec/ApplicationFirewall/socketfilterfw --getglobalstate  # Firewall status

# Linux equivalents
# ip addr show
# ip link set eth0 up
# route -n
# ss -tuln
# arp -a

File System Tools

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# macOS file system features
# APFS (Apple File System) - default since High Sierra
diskutil list              # List disks and volumes
diskutil info /            # Volume information
diskutil apfs list         # APFS container information

# HFS+ (Hierarchical File System Plus)
# Legacy file system support

# Spotlight metadata
mdls filename              # Show metadata
mdfind "kMDItemDisplayName == 'filename'"  # Search by metadata

# Extended attributes
xattr -l filename          # List extended attributes
xattr -w key value filename # Write extended attribute
xattr -d key filename      # Delete extended attribute

# Linux equivalents
# fdisk /dev/sda
# parted /dev/sda
# mkfs.ext4 /dev/sda1
# mount /dev/sda1 /mnt

🎨 Advanced macOS Features

Launch Services

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# Launch Services management
launchctl list                    # List launch agents/daemons
launchctl load ~/Library/LaunchAgents/com.example.app.plist
launchctl unload ~/Library/LaunchAgents/com.example.app.plist

# System-level daemons
sudo launchctl list | grep -v com.apple  # Third-party services

# User-level agents
launchctl list | grep com.example  # User services

# Launch Services database
/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister  # Rebuild database

Security and Privacy

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# Gatekeeper and code signing
spctl --status             # Gatekeeper status
spctl --add /path/to/app   # Allow app from identified developer
codesign -v /path/to/app   # Verify code signature

# System Integrity Protection (SIP)
csrutil status             # SIP status
# Note: Requires recovery mode to modify

# FileVault encryption
fdesetup status            # FileVault status
fdesetup list              # List enabled users

# Privacy preferences
tccutil reset All          # Reset all privacy permissions

🧪 Development Tools

Compiler and Build Tools

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# Xcode Command Line Tools
xcode-select --install     # Install command line tools
xcode-select -p            # Show tools path

# Compilers
clang                      # Default C compiler
gcc                        # GCC (if installed via Homebrew)
swift                      # Swift compiler

# Build systems
make                       # BSD make
cmake                      # CMake (if installed)

# macOS-specific development
# Xcode project management
xcodebuild -project Project.xcodeproj -target Target build

Debugging Tools

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# macOS debugging tools
# Note: Many traditional BSD tools are available

lsof                       # List open files
top                        # Process monitor
ps aux                     # Process list
vm_stat                    # Virtual memory statistics

# macOS-specific tools
fs_usage                  # File system activity monitor
opensnoop                 # Open file monitor
iosnoop                   # I/O monitor

# Crash reporting
sudo log show --predicate 'eventMessage contains "panic"' --last 1h

🗃️ File System and Storage

APFS Features

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# APFS (Apple File System) management
diskutil apfs list         # List APFS containers
diskutil apfs listVolumes containerID  # List volumes
diskutil apfs addVolume containerID APFS "VolumeName"  # Add volume

# Snapshots
tmutil localsnapshot       # Create local snapshot
tmutil listlocalsnapshots /  # List local snapshots

# Clone operations
cp -c source destination   # Clone file (copy-on-write)

Metadata and Extended Attributes

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# Spotlight metadata
mdimport -V filename       # Import metadata
mdimport -d1 filename      # Debug import
mdls filename              # List metadata attributes

# Extended attributes
xattr -l filename          # List all extended attributes
xattr -wx com.example.attr "$(echo 'value' | xxd -p)" filename  # Write hex data
xattr -px com.example.attr filename  # Read hex data

# Resource forks (legacy)
/System/Library/CoreServices/FixupResourceForks  # Fix resource forks

🧾 Summary of Key Differences

Command Differences Table

macOS Linux Description
ls -G ls --color Color output
brew apt/yum/pacman Package management
launchctl systemctl Service management
diskutil fdisk/parted Disk management
networksetup nmcli/ip Network configuration
sw_vers cat /etc/os-release Version information
xattr None Extended attributes
mdls/mdfind None Metadata search

Configuration Locations

macOS: - ~/Library/LaunchAgents/ - User agents - /Library/LaunchDaemons/ - System daemons - /System/Library/LaunchDaemons/ - System agents - ~/Library/Preferences/ - User preferences - /etc/ - System configuration files

Linux: - /etc/systemd/ - Systemd configuration - /etc/default/ - Service defaults - /etc/iptables/ - Firewall rules - /etc/passwd - User database - /etc/group - Group database


🧠 Best Practices for Cross-Platform Scripts

Detection and Adaptation

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# Detect macOS vs Linux
detect_platform() {
    case "$(uname -s)" in
        Darwin)
            echo "macos"
            ;;
        Linux)
            echo "linux"
            ;;
        *)
            echo "unknown"
            ;;
    esac
}

PLATFORM=$(detect_platform)

# Platform-specific functions
install_package() {
    local package="$1"

    case "$PLATFORM" in
        macos)
            if command -v brew >/dev/null 2>&1; then
                brew install "$package"
            elif command -v port >/dev/null 2>&1; then
                port install "$package"
            fi
            ;;
        linux)
            if command -v apt-get >/dev/null 2>&1; then
                apt-get update && apt-get install -y "$package"
            elif command -v yum >/dev/null 2>&1; then
                yum install -y "$package"
            fi
            ;;
    esac
}

# Service management abstraction
manage_service() {
    local service="$1"
    local action="$2"

    case "$PLATFORM" in
        macos)
            case "$action" in
                start) launchctl start "$service" ;;
                stop) launchctl stop "$service" ;;
                restart)
                    launchctl stop "$service"
                    sleep 2
                    launchctl start "$service"
                    ;;
                status) launchctl list | grep "$service" ;;
            esac
            ;;
        linux)
            systemctl "$action" "$service"
            ;;
    esac
}

Tool Compatibility

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# GNU tools on macOS (via Homebrew)
# brew install coreutils findutils gnu-sed gawk

# Use GNU tools with 'g' prefix
# gsed instead of sed
# gfind instead of find
# gawk instead of awk

# Check for GNU tools
if command -v gfind >/dev/null 2>&1; then
    FIND_CMD="gfind"
else
    FIND_CMD="find"
fi

# File manipulation with proper tools
safe_find() {
    if command -v gfind >/dev/null 2>&1; then
        gfind "$@"
    else
        find "$@"
    fi
}

🧾 See Also