Here are some useful commands for the Terminal on a MacBook:
These commands should cover a wide range of tasks and help you effectively manage your MacBook via the Terminal.
Basic Commands
Change Directory
cd <directory>
Example:
cd Documents
List Files and Directories
ls
Common options:
ls -l # long format ls -a # show all files, including hidden ls -lh # long format with human-readable file sizes
Print Working Directory
pwd
Create a New Directory
mkdir <directory>
Example:
mkdir new_folder
Create an Empty File
touch <filename>
Example:
touch newfile.txt
Copy Files or Directories
cp <source> <destination>
Example:
cp file1.txt file2.txt
Move or Rename Files or Directories
mv <source> <destination>
Example:
mv oldname.txt newname.txt
Remove Files or Directories
rm <file>
Remove directory and its contents:
rm -r <directory>
Display File Contents
bash cat <file>
Example:bash cat file.txt
System Information and Management
Check Disk Usage
df -h
Check Directory Size
du -sh <directory>
Example:
du -sh Documents
Display Running Processes
top
Terminate a Process
bash kill <pid>
Example:bash kill 1234
Network Commands
Check Network Configuration
ifconfig
Ping a Host
ping <hostname>
Example:
ping google.com
Check Open Network Ports
bash netstat -an
File Permissions and Ownership
Change File Permissions
chmod <permissions> <file>
Example:
chmod 755 script.sh
Change File Owner
bash chown <owner>:<group> <file>
Example:bash chown user:staff file.txt
Miscellaneous
Search for Files
find <path> -name <filename>
Example:
find / -name "*.txt"
Search Inside Files
grep <pattern> <file>
Example:
grep "search_term" file.txt
Compress Files
zip <archive.zip> <file>
Example:
zip archive.zip file.txt
Extract Files
bash unzip <archive.zip>
Package Management
Install Homebrew (Package Manager)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Install a Package with Homebrew
bash brew install <package>
Example:bash brew install wget
Git Commands
Clone a Repository
git clone <repository_url>
Check Repository Status
git status
Add Changes to Staging Area
git add <file>
Add all changes:
git add .
Commit Changes
git commit -m "commit message"
Push Changes to Remote Repository
git push
Pull Changes from Remote Repository
bash git pull