Basic Terminal Commands and Practical Usage on macOS
This article introduces essential terminal commands and practical scenarios for macOS users, covering tools like man, grep, tail, awk, top, lsof, ansible, and nc, and offers efficiency tips such as aliases, plugins, and fuzzy finders to boost productivity.
Hey everyone, today I want to share a relatively basic topic: fundamental operations in the terminal. Many people feel reluctant to use the terminal, thinking it’s full of hard‑to‑remember commands and that GUI applications are more convenient. This article introduces common commands, demonstrates how to solve problems with them in real scenarios, and finally shares plugins that can improve efficiency.
The examples use macOS with iTerm as the terminal emulator and zsh as the shell (though you can use bash, fish, etc., according to personal preference).
1. Common Commands
man provides the manual for a command; you can also use tldr for a more concise summary.
grep is a powerful text‑search tool that supports regular expressions. Example:
grep abc xxx.logAdditional options: -v to exclude matches, -i for case‑insensitive search, and full regex support.
tail and head display the end or beginning of files. Examples:
tail -f filefollow file changes,
tail -n 20 fileshow the last 20 lines,
head -n 10 fileshow the first 10 lines. They are often combined with grep :
tail -f file | grep errorand can be used to list recent files:
ll -t | head -n 10awk is a versatile tool for processing and reporting data. It can read from files or standard input, split lines into fields, filter, and compute statistics. Typical usage for log analysis:
awk 'BEGIN{...} pattern{...} END{...}' fileRefer to the awk documentation for full syntax.
top shows real‑time system resource usage, similar to Windows Task Manager. Press P to sort by CPU, M for memory, c to view full command lines, and use top -p PID to monitor a specific process.
lsof lists open files and network sockets. Example to check if a port is in use:
lsof -i:8080Or to inspect a process’s file descriptors:
lsof -p 1234ansible enables batch execution of commands on multiple remote machines, useful for administrators who need to run the same command across a fleet.
nc (netcat) can probe ports:
nc -vz ip portfor TCP, and
nc -uz ip portfor UDP. It can also transfer files.
netstat displays network statistics. Common usage to view listening sockets:
netstat -tunlp2. Practical Scenarios
When an online service fails, check logs
1) SSH into the affected machine (set up key‑based authentication or use expect to avoid password prompts), then cd to the relevant directory and combine grep , awk , and tail to locate error messages.
2) If traffic is load‑balanced across many machines, use Ansible to run batch grep / tail / awk commands, or employ custom Go tools ( remote‑tail , remote‑grep ) that execute the same command on multiple hosts.
Investigating an unknown process
Find the PID:
ps aux | grep xxxThen inspect it with:
top -p pid lsof -p pid ll /proc/{pid}/exeDisk space is filling up
Check usage with df . List directory sizes:
du -sh * | sort -nrIf large files remain after deletion, find deleted but still‑open files:
lsof | grep delete3. Efficiency Boosters
1) Beautify your terminal with oh‑my‑zsh themes.
2) Enable case‑insensitive completion in zsh and install zsh‑autosuggestions to show recent commands in a faint font.
3) Use alias to shorten long commands (e.g., ll for ls -l ).
4) Replace long cd paths with short aliases or use autojump to jump to frequently visited directories by typing j part_of_path .
5) On macOS, replace Finder with ranger , a terminal file manager that uses Vim‑style keys and can be extended with custom scripts for batch operations.
6) Install fzf for fuzzy file searching; it integrates with Vim, Ranger, and can replace Ctrl‑R in the shell to quickly find recent commands.
Conclusion
Thank you for reading. I hope you now feel confident using the terminal for tasks that would otherwise require mouse clicks. Embrace command‑line workflows to boost efficiency, and may your screen look as cool as the Matrix when you type away.
Huajiao Technology
The Huajiao Technology channel shares the latest Huajiao app tech on an irregular basis, offering a learning and exchange platform for tech enthusiasts.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.