By ChatGPT & Benji Asperheim | 2025-07-16

Linux OS and macOS Terminal Commands Overview

Understanding the basic command-line tools available in Linux and macOS is a game changer for anyone who wants to take control of their computer or server. Most UNIX-like systems—including Linux distributions and macOS—share a common set of POSIX-compliant commands, but subtle differences can trip up beginners. This section breaks down the core commands you'll use daily, explains what makes Linux and macOS unique, and highlights the key flags and options that matter most.

🖥️ What Linux Is: Understanding the Basics

What is Linux?

Linux is an operating system—just like Windows or macOS—but with some big differences:

  • Linux is open source and free. Anyone can download, use, or even modify it, legally.
  • Linux isn't just one thing. Instead of a single version, "Linux" refers to a whole family of related operating systems (called "distributions" or "distros") like Ubuntu, Fedora, Debian, and many others.
  • Linux powers the internet. Most servers, cloud computers, and even Android phones run on Linux behind the scenes.

Why Does Linux Matter?

  • Stability and security: Linux is famously reliable. That's why it runs everything from Google's servers to NASA's Mars rovers.
  • You control your computer: With Linux, you're not locked into a company's rules or software.
  • Endless customization: Almost everything about Linux can be tweaked, changed, or replaced.
  • It's not just for nerds: Many modern Linux distros are beginner-friendly, with easy installers and graphical interfaces.

How is Linux Different from Windows or macOS?

  • No single company owns it. It's built by a global community.
  • The command line is powerful (and often essential). While you can use a mouse and desktop, much of Linux's strength comes from its command-line tools.
  • Software installation is different. Instead of downloading apps from the web, you usually install software using a package manager (like apt, dnf, or yum).

Common Linux Myths

  • You don't have to be a programmer to use Linux. Many distributions are as easy to use as Windows or macOS.
  • Linux can run on almost any hardware. From old laptops to supercomputers.

In short:

Linux is a free, open-source operating system that's used everywhere—from personal laptops to the world's biggest websites. It's flexible, secure, and gives you real control over your system. Knowing even a little bit about Linux and its commands will make you more effective on almost any computer you touch.

File & Directory Management

Working with files and directories is at the heart of command-line usage. Whether you're moving files, creating project folders, or cleaning up clutter, knowing how to use commands like ls, cd, mkdir, cp, and rm—along with their most important options—will make your workflow smoother and faster. This section covers the practical steps to navigate, organize, and manage your files safely on both Linux and macOS systems.

ls — List Files/Directories

Usage:

ls                # List files in current dir
ls -l             # Long (detailed) listing
ls -a             # Include hidden files (start with .)
ls -lh            # Human-readable sizes
ls -la            # Long listing, all files

Difference:

  • ls is available everywhere.
  • ls -G colors output on macOS; ls --color does so on Linux. (ls on macOS doesn't support --color.)
  • macOS and Linux show similar output, but flags may differ for edge cases.

cd — Change Directory

Usage:

cd mydir          # Go into 'mydir'
cd ..             # Go up one level
cd                # Go to your home directory

Difference:

  • No major difference. Both behave the same.

pwd — Print Working Directory

Usage:

pwd               # Show full current path

Difference:

  • No difference.

mkdir — Make Directory

Usage:

mkdir mydir                   # Create single directory
mkdir -p parent/child/leaf    # Create nested dirs; parents if needed

Difference:

  • Always use -p if you're creating subfolders in a path that might not exist.

rm — Remove Files/Directories

Here's how you can delete or remove a directory (or delete files)—and it works the same in both macOS and Linux:

Usage:

rm file.txt                   # Remove file
rm -i file.txt                # Interactive prompt before delete
rm -r mydir                   # Recursively delete directory
rm -rf mydir                  # Recursive, force delete (dangerous!)

Difference:

  • Same in both systems.
  • On macOS, the file is never put into Trash—it gets deleted permanently.

cp — Copy Files/Directories

Usage:

cp file1.txt file2.txt        # Copy file
cp -r dir1 dir2               # Copy directories recursively
cp -i file.txt dest/          # Prompt before overwrite

Difference:

  • Flags are the same, but some advanced options (--parents) exist only on GNU/Linux.

mv — Move/Rename Files/Directories

Usage:

mv file.txt newloc/           # Move file to directory
mv oldname.txt newname.txt    # Rename file
mv -i file.txt dest/          # Prompt before overwrite

Difference:

  • No real difference for basics.

touch — Create/Update File

Usage:

touch newfile.txt             # Create file (if it doesn't exist) or update timestamp

Difference:

  • Same.

File Viewing & Editing

cat, less, more, head, tail

  • cat file.txt — Dump file to terminal
  • less file.txt — View file, scroll up/down (q to quit)
  • head file.txt — Show first 10 lines (-n 20 for 20 lines)
  • tail file.txt — Show last 10 lines (-f to "follow" updates, e.g., log files)

Difference:

  • All work on both macOS and Linux.
  • less is always better than more (which is more limited on macOS).

Finding Stuff

find

find . -name "*.js"           # Find files named *.js starting here

Difference:

  • Basic usage works the same.
  • Some advanced find flags differ (see man pages).

grep

grep "search" file.txt        # Find 'search' in file.txt
grep -r "pattern" .           # Search recursively in dir
grep -i "pattern" file.txt    # Case-insensitive search

Difference:

  • On macOS, you might get BSD grep. GNU grep on Linux has more features. For 100% compatibility, install GNU grep via Homebrew: brew install grep.

Permissions

chmod and chown

chmod +x myscript.sh          # Make script executable
chmod 755 mydir               # Set permissions
chown user:group file.txt     # Change ownership

Difference:

  • Syntax the same, but macOS users rarely use chown unless using sudo.

Networking

curl and wget

  • curl is installed everywhere:
  curl http://example.com     # Fetch page
  curl -O url/file.zip        # Save as file.zip
  • wget is not preinstalled on macOS; it is on most Linux.

ping

  • Linux: ping runs until you stop with Ctrl+C
  • macOS: By default, ping only sends a few packets unless you add -t (or -c N for count):
  ping -c 4 google.com        # Send 4 pings (both systems support this)

Process Management

ps, top, kill

  • ps aux — List all processes
  • top — Real-time view; on macOS, use htop for better output (install via Homebrew)
  • kill PID — Terminate a process by PID
  • kill -9 PID — Force kill

Difference:

  • Output columns may differ slightly.
  • htop is not preinstalled anywhere, but available everywhere.

System Info

uname, df, du, free

  • uname -a — System info
  • df -h — Free disk space
  • du -sh * — Dir/file sizes (human-readable)
  • free -h — Memory (Linux only; on macOS, use vm_stat)

Opening Files/Apps

Platform-Specific:

  • Linux:
  • xdg-open <file> — Opens file with default app
  • (e.g., xdg-open index.html launches browser or text editor)
  • macOS:
  • open <file> — Same idea
  • (e.g., open index.html opens in default browser/editor)

xdg-open is not available on macOS, and open is not available on Linux.

Know which to use for your OS.

Searching Commands

which, whereis, man

  • which <cmd> — Show full path of an executable
  • man <cmd> — Show manual for a command (e.g., man ls)
  • whereis <cmd> — Broader than which (Linux only; limited use)

macOS and Linux Differences: Quick Summary

PurposeLinux CommandmacOS Command
Open file/appxdg-openopen
Install pkgapt, yum, etc.brew (Homebrew)
GNU utilsdefaultuse Homebrew (gnu-sed, gnu-tar, etc)
View RAM usagefree -hvm_stat, top
Copy with progresscp -vcp -v (but less info on macOS)
Download filewget, curlcurl (wget needs install)

NOTE: On macOS you can install the Homebrew package manager (brew command) by executing /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" in a terminal session.

Beginner's Takeaways

  • Don't just run commands blindly; use man <command> or <command> --help for flag info.
  • The most common headaches are missing utilities (wget, GNU tools) on macOS. Use Homebrew: https://brew.sh/
  • For scripting portability: stick to POSIX flags and avoid Linux-only or macOS-only options.
  • Use -p for mkdir to make full directory trees in one go.
  • Use -r for recursive actions (rm, cp).
  • Know xdg-open (Linux) and open (macOS) for launching files.

💡 PowerShell: Cross-Platform Scripting for Windows, macOS, and Linux

PowerShell is Microsoft's modern, cross-platform command-line shell and scripting language. Originally exclusive to Windows, PowerShell now works on macOS and Linux as well. This makes it one of the few shells that can be used natively on all three major operating systems with (mostly) the same syntax.

How is PowerShell Different from Bash or Zsh?

  • Object-oriented: PowerShell commands output real data objects, not just text. This makes filtering and manipulating output much more powerful and less error-prone.
  • Consistent commands: Many PowerShell commands use "Verb-Noun" style, e.g., Get-Process, Set-Item, Copy-Item, which are easier to guess and self-documenting.
  • Not just for Windows: PowerShell Core (aka pwsh) is open source and works on macOS and Linux too.

Installing PowerShell

  • Windows 10+: Pre-installed as "Windows PowerShell". For the cross-platform version, install PowerShell Core.
  • macOS/Linux:
brew install --cask powershell    # macOS (Homebrew)
sudo apt install powershell       # Linux (Debian/Ubuntu, if repo available)

Or, download installers from: https://github.com/PowerShell/PowerShell

  • Launch it with:
pwsh

PowerShell Basics for Beginners

Bash/Zsh ExamplePowerShell EquivalentWhat It Does
lsGet-ChildItem or lsList files and directories
cd mydirSet-Location mydir or cdChange directory
cat file.txtGet-Content file.txt or catShow file contents
mv a.txt b.txtRename-Item a.txt b.txtRename a file
cp a.txt b.txtCopy-Item a.txt b.txtCopy a file
rm file.txtRemove-Item file.txtDelete a file
pwdGet-Location or pwdShow current directory

Tip: Common Linux/macOS aliases like ls, cat, pwd, and cd also work in PowerShell for convenience—but the real PowerShell cmdlets are more powerful and scriptable.

Working with Objects vs Text

  • PowerShell commands (cmdlets) output objects, not plain text.
Get-Process | Where-Object {$_.CPU -gt 100}
  • This filters all processes where CPU usage is over 100.
  • In Bash, you'd have to parse ps output as text. In PowerShell, you filter real properties.

Useful PowerShell Tips

  • Tab completion: Works everywhere, just like Bash.
  • Get help:
Get-Help Get-Process
  • Run a script:
./myscript.ps1

On non-Windows systems, you may need to set the script as executable or set execution policy.

When is PowerShell Worth Using on macOS/Linux?

  • Scripting across platforms: If you want a single script to work on Windows, macOS, and Linux, PowerShell is a great choice.
  • Working with structured data: (JSON, XML, CSV) is much easier in PowerShell than in Bash.
  • Interfacing with Windows tools/services: (e.g. Active Directory, registry, Windows-specific automation).

For basic day-to-day shell work, Bash/Zsh is simpler on macOS/Linux, but PowerShell's object-based approach is unmatched for automation and data manipulation across OSes.

Summary:

PowerShell is no longer just for Windows admins. If you work across OSes, learn the basics—it can save hours on cross-platform scripting and automation.

Linux Time Converter: How to Work with Dates and Times in the Terminal

Dealing with timestamps and converting time formats is a common challenge on Linux systems. Whether you need to convert a Unix timestamp to a readable date, change time zones, or format a date for scripting, the Linux terminal gives you flexible tools for all your "Linux time converter" needs. Here's what beginners need to know.

What Is a Linux Time Converter?

A "Linux time converter" usually refers to using built-in command-line tools to convert between different time representations—like Unix timestamps, human-readable dates, and different time zones—without any third-party software.

Linux Time Converter

  • Convert a Unix timestamp to readable date:
date -d @1689556789        # Linux
date -r 1689556789         # macOS / BSD
  • Replace 1689556789 with your timestamp.
  • On Linux, use -d @<timestamp>.
  • On macOS, use -r <timestamp>.
  • Get the Current Date and Time:
  date
  • Format Current date/time (Custom Output):
  date +"%Y-%m-%d %H:%M:%S"
  • This gives you output like 2025-07-16 10:42:01.
  • Convert Linux Time Zones:
  TZ="America/New_York" date
  TZ="Europe/London" date
  • Temporarily changes the time zone for this command only.

Converting Time in Scripts

  • To use a "linux time converter" in scripts, rely on date with custom format strings.
  • Example: Print yesterday's date:
    date -d "yesterday"   # Linux
    date -v-1d            # macOS
  • Find the difference between two times (in seconds):
  expr $(date -d '2025-07-16 12:00:00' +%s) - $(date -d '2025-07-16 11:45:00' +%s)   # Linux
  • macOS: You may need to use date -j -f with more complex syntax.

Summary

If you ever need to convert or format dates and times on Linux (or macOS), learn the date command—it's your built-in "linux time converter." With just a few flags, you can handle timestamps, time zones, and custom formats right from the terminal, making scripting and log analysis much easier.

🔒 Users, Permissions, and "Why Can't I Do That?"

Who Am I? Who's Allowed to Do What?

whoami — Shows your current user.

whoami

Output will be your username. This matters, because the permissions system decides what you can do, based on who you are.

Users, Groups, and Root

  • Every file and folder belongs to a user (owner) and a group.
  • root is the "superuser"—can do anything, anywhere.
  • Most of the time, you are not root.

To become root (just for one command), use sudo:

sudo 

E.g. sudo rm /var/log/somefile

When Do You Need sudo?

  • System files/folders:
  • Anything outside your home directory (/home/yourname on Linux, /Users/yourname on macOS) is likely "root-owned" and protected.
  • Example system directories: /etc, /usr, /bin, /sbin, /var, /System (macOS), /Library (macOS), /opt
  • When you see Permission denied for writing/creating/deleting outside your home, try with sudo.
  • But never run random commands with sudo. It's the fastest way to break things.

Understanding File Permissions: ls -l Output

  • Example:
ls -l myfile.txt
-rw-r--r--  1 jane  staff  0 Jul 16 09:15 myfile.txt

Breakdown:

  • -rw-r--r-- = permissions (10 chars: type + owner/group/other perms)
  • jane = owner
  • staff = group

Permissions split into three:

[owner][group][others]

  • r = read
  • w = write
  • x = execute (needed to run scripts, enter directories)

So:

  • -rw-r--r--
  • Owner can read/write (rw-)
  • Group can read only (r—)
  • Others can read only (r—)

Changing Permissions: Octal vs rwx Notation

  • Octal format: Each permission set is a digit (r=4, w=2, x=1, sum them):
  • chmod 755 myscript.sh → rwxr-xr-x
  • Breakdown:
  • 7 = 4+2+1 = rwx (owner: read/write/execute)
  • 5 = 4+1 = r-x (group: read/execute)
  • 5 = 4+1 = r-x (others: read/execute)
  • Most common permission combos:
  • chmod 644 file.txtrw-r--r-- (safe for docs)
  • chmod 600 secret.txtrw------- (owner only)
  • chmod 755 script.shrwxr-xr-x (owner can edit/run, others can run)
  • chmod +x script.sh — adds execute for everyone (typical for scripts)

Tip:

  • Use octal (chmod 755 ...) for scripts/automation.
  • Use symbolic (chmod +x ...) for quick manual tweaks.

Changing File Ownership

  • chown user:group file
sudo chown root:wheel /usr/local/bin/myprog

Usually, you need sudo to change ownership, and almost never do this unless you know why.

Checking Who Owns a File and What Permissions It Has

  • Use ls -l:
ls -l /etc/hosts

You'll usually see root as owner for system files.

Common Permission Errors:

ErrorCauseFix
Permission deniedNot enough rights to read/write/runUse sudo, or fix permissions/ownership
Operation not permittedEven sudo can't do it (immutable file, SIP on macOS)Special case—don't mess unless you really know
Command not foundFile is not executable, or not in $PATHchmod +x file, or specify path (./myscript.sh)

Key Beginner Rules:

  • Never use sudo in your home directory. You own everything there.
  • Never run sudo rm -rf / or similar. It will destroy your system.
  • If you don't know why you need sudo, you probably don't need it.
  • Only edit system files (e.g. /etc/hosts) with sudo and a reliable editor (sudo nano /etc/hosts).

Summary:

Files and folders have owners and permission sets. System files are "root-owned" and need sudo. Learn to read ls -l and understand chmod. When in doubt, check with ls -l and ask yourself if you really need more permissions, or if you're working in the wrong place.

UNIX/Linux/macOS Terminal Cheat Sheet

Here's a no-BS, practical Linux/macOS command-line cheat sheet with real copy-paste examples and notes on common gotchas. You'll see which options matter, which to skip, and what breaks between macOS and Linux:

Directory & File Basics

PurposeCommand & ExampleNotes / Differences
List filesls -la-l (long), -a (all incl. hidden)
Change dircd /path/to/dircd ~ for home
Make dirmkdir -p foo/bar/baz-p = parents (ALWAYS use for nesting)
Remove filerm file.txtNo trash/recycle, it's gone
Remove dirrm -rf mydir-r=recursive, -f=force
Copy filecp file1.txt backup.txtOverwrites without warning
Copy dircp -r sourcedir/ destdir/-r = recursive
Move/renamemv old.txt new.txtAlso moves files between dirs
Touch filetouch file.txtNew or update timestamp
Show pwdpwdShows absolute path

File Viewing/Editing

CommandExample(s)Notes
Show all file contentcat file.txtCareful with big files!
View + scrollless file.txtq to quit, /search to search
Head/tailhead -n 20 file.txt
tail -f logfile
-f follows updates
Edit (basic CLI)nano file.txtWorks everywhere, easier than vi
Edit (macOS GUI)open -e file.txtOpens TextEdit on macOS only
Edit (Linux GUI)xdg-open file.txtOpens default editor

Searching & Finding in Linux or macOS

CommandExampleNotes
Find filesfind . -name "*.js"Current dir down, match pattern
Search insidegrep -i "error" file.txt-i = case-insensitive
Search recur.grep -r "TODO" .Recursively in all files

POSIX Permissions & Ownership

CommandExampleNotes
Make executablechmod +x script.shNeeded to run as ./script.sh
Set permissionschmod 644 file.txtRead/write owner, read others
Change ownersudo chown $USER:staff file.txtUse sudo if not your file

Disk & System Info

CommandExampleNotes
Disk usagedu -sh *Dir/file sizes, human readable
Disk free spacedf -hHuman readable
Show RAMfree -h (Linux)
vm_stat (macOS)
macOS: vm_stat output is weird, search for "Pages free"
System infouname -aKernel and arch

Processes & Management

CommandExampleNotes
List all procsps auxAll user/system processes
Real-time toptopUse htop if installed
Kill processkill 12345By PID
Force killkill -9 12345Use if regular kill fails

Networking

CommandExampleNotes
Ping hostping -c 4 google.commacOS default is 1, Linux is infinite
Download filecurl -O http://example.com/file.zipwget not default on macOS
Check open ports`netstat -angrep LISTEN`Needs sudo for all ports sometimes

Open Files & Apps in Linux and macOS

OSCommandExampleNotes
macOSopenopen file.pdf or open .Opens in default app/Finder
Linuxxdg-openxdg-open file.pdfOpens in default app/file mgr

Installing Packages and Programs in a Terminal

Here's a general guide to installing packages on various Linux distros, or on macOS using Homebrew:

OSCommandExampleNotes
Debian/Ubuntusudo apt update && sudo apt install treesudo apt install treeUses APT package manager
Fedora/RHELsudo dnf install treesudo dnf install treeUses DNF package manager (Fedora 22+), YUM (Fedora 21-)
Arch Linuxsudo pacman -S treesudo pacman -S treeUses Pacman package manager
Alpine Linuxsudo apk add treesudo apk add treeUses APK package manager
Red Hatsudo yum install tree (RHEL 7 and earlier) or sudo dnf install tree (RHEL 8 and later)sudo yum install treeUses YUM (RHEL 7 and earlier), DNF (RHEL 8 and later)
macOS/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" and brew install treeInstall Homebrew, then installNative macOS pkg mgmt is limited; use Homebrew

NOTE: Some Linux distros may have different package managers or installation commands.

This table covers some of the most popular package managers in Linux:

  • APT (Debian, Ubuntu, and derivatives)
  • DNF (Fedora, RHEL 8 and later)
  • YUM (RHEL 7 and earlier)
  • Pacman (Arch Linux and derivatives)
  • APK (Alpine Linux)

Keep in mind that this is not an exhaustive list, and you may need to check the documentation for your specific Linux distro to find the correct installation command.

Other Useful Commands

CommandExampleNotes
Path of a cmdwhich pythonFull path to binary
Manual pageman grepRead docs in terminal
Show env varsprintenv or envAll your env vars
Previous cmdUp arrow / !!Run last command
AutocompleteTab keyComplete file/dir names

Scripting Portability Tips

  • Avoid sed -i on macOS unless you know how BSD sed syntax differs (sed -i '' ...)
  • Prefer POSIX flags in scripts.
  • Use /bin/sh as script shebang for max compatibility, unless you need bash features.
  • Use open only on macOS, xdg-open only on Linux for opening files/URLs.

This cheat sheet is enough to cover 99% of daily tasks for anyone new or casual with a Linux or macOS terminal.

Conclusion

Mastering the basics of the terminal is one of the fastest ways to become more productive—and less frustrated—on any Unix-based system. By understanding file permissions, user roles, and the subtle differences between Linux and macOS command-line tools, you avoid permission errors, protect your system, and save serious time. Refer back to the cheat sheet when in doubt. Remember: don't use sudo unless you know exactly why, and what its effects are. If you're ever stuck, run man <command> or search the official docs. The command line isn't just for power users—it's for anyone who wants to actually control their computer.

Discover expert insights and tutorials on adaptive software development, Python, DevOps, creating website builders, and more at Learn Programming. Elevate your coding skills today!