Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Essential macOS Command Line Operations

Tech 1

Filesystem Navigation and Manipulation

# List directory contents
ls

# Change current working directory
cd ~/Desktop      # Move to Desktop
cd ..             # Go up one level
cd /              # Switch to root directory

# Print absolute path of the current directory
pwd

# Create a new directory
mkdir project_alpha

# Generate an empty file or update its timestamp
touch index.html

# Remove files or directories permanently (bypasses Trash)
rm obsolete_log.txt
rm -r legacy_code  # Delete directory recursively
rm -rf cache_build # Force recursive deletion

# Duplicate files and directories
cp draft.md final.md
cp -r src/ backup/ # Copy directory recursively

Terminal commands typically consist of three components: Options (prefixed by -), Arguments (targets for the command), and Extras (additional data). Options can often be combined, such as ps -e -f being equivalent to ps -ef. Arguments specify the target, e.g., ls /var/log.

File Inspection and Editing

# Output entire file content
cat configuration.yaml

# View file content with scrolling
less large_log.txt

# Edit files using terminal-based text editors
nano todo.txt
vim script.sh

System Diagnostics

# Display current date and time
date

# Output a calendar
cal

# Check disk space usage
df -h

# Monitor active processes and resource consumption
top

Network Utilities

# Verify network connectivity to a remote host
ping google.com

# Inspect network interface configurations (en0 is typical for macOS Wi-Fi)
ifconfig en0

# Fetch data from a URL
curl https://api.example.com/data

Archiving and Compression

# Archive files
tar -czf archive.tar.gz directory_name/

# Compress or decompress files
gzip large_file.txt
gunzip large_file.txt.gz

Access Control and Privileges

# Execute commands with elevated privileges
sudo apt update

# Identify the currently logged-in user
whoami

# Transfer file ownership
chown user:group target_file

# Modify file permissions
# u (user), g (group), o (others), a (all)
# r (read), w (write), x (execute)
chmod u=rwx,g=rx,o=r script.sh
chmod -R a+rx /opt/application  # Apply recursively

Process Management

# List currently running processes
ps aux

# Terminate a process by its ID
kill -9 12345

Background Execution

Appending & to a command runs it in the background, freeing up the terminal session.

npm run build &

Aborting Running Commands

Press Control + C in the active terminal window to send an interrupt signal and halt the currently executing process.

Miscellaneous Utilities

# Clear the terminal viewport
clear

# Review previously executed commands
history

# Search for specific text patterns within files
grep "ERROR" /var/log/syslog

# Locate files within a directory hierarchy
find . -name "*.js"

# Print text to the terminal
echo "Deployment complete"

# Compile source code
gcc main.c -o executable_bin

# Run a compiled binary
./executable_bin

# Disable Time Machine local snapshots
sudo tmutil disablelocal

# System power controls
sudo shutdown -h now   # Power off immediately
sudo shutdown -h +30   # Power off in 30 minutes
sudo shutdown -r now   # Reboot immediately

Homebrew Package Management

# Install, remove, or reinstall packages
brew install wget
brew uninstall wget
brew reinstall wget

# Query package information
brew list              # Show installed packages
brew search postgres   # Search for available formulas
brew info wget         # Display package details
brew deps wget --installed --tree # View dependency tree
brew --prefix wget     # Reveal installation path

# Maintain Homebrew
brew update            # Fetch latest version of Homebrew
brew upgrade           # Upgrade all outdated packages
brew cleanup           # Remove stale lock files and old versions

# Manage background services
brew services start redis
brew services stop redis
brew services restart redis
brew services list     # Show status of managed services

# Other operations
brew --version         # Display current Homebrew version
brew link --overwrite node # Overwrite existing files when linking
Tags: macos

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.