This guide covers a selection of essential terminal commands used on both macOS and Linux systems.
Introduction to the terminal
The terminal provides a command line interface (CLI) where users can type commands to perform operations without a graphical user interface (GUI). macOS terminal commands and Linux terminal commands are often very similar because both systems are Unix-based.
Basic terminal commands
1. Navigating the file system
pwd(print working directory): Displays the path of the current directory you are in.cd <directory>(change directory): Changes the current directory to<directory>. Usecd ..to go up one directory level orcdto return to the home directory.ls(list): Lists all files and directories in the current directory. Usels -lfor detailed information andls -ato show hidden files.
2. File management
touch <filename>: Creates a new empty file named<filename>.mkdir <directory>(make directory): Creates a new directory named<directory>.cp <source> <destination>(copy): Copies files or directories from<source>to<destination>.mv <source> <destination>(move): Moves files or directories from<source>to<destination>. Also used for renaming.rm <filename>(remove): Deletes a file. Userm -r <directory>to delete a directory and all its contents.
3. System monitoring and management
top: Displays an ongoing view of process activity. It provides a dynamic real-time view of a running system. Equivalent tohtopon Linux for an enhanced view.df(disk free): Shows the amount of disk space available on the file system containing each file name argument.du(disk usage): Displays the disk usage for the directory you specify. By usingdu -sh, you can see a summary of the disk space used by each directory.kill <PID>(kill process): Terminates the process with the specified Process ID (PID).
4. Networking
ping <hostname>: Checks the network connection to a host. It sends ICMP ECHO_REQUEST packets to network hosts.netstat: Displays network connections, routing tables, interface statistics, masquerade connections, and multicast memberships.ifconfig: Used to configure the kernel-resident network interfaces. It is used at boot time to set up interfaces as necessary.curl <URL>: Transfers data from or to a server, using one of the supported protocols (HTTP, HTTPS, FTP, FTPS, SCP, SFTP, TFTP, DICT, TELNET, LDAP or FILE).
Advanced commands
grep <pattern> <file>: Searches for the pattern in the file and outputs the matching lines. Very useful for searching through logs or files.chmod <permissions> <file>: Changes the file mode (permissions) of a file or directory.chown <user>:<group> <file>: Changes the owner and group of a file or directory.sudo <command>: Executes a command with superuser privileges, necessary for commands that require administrative permissions.
Tips for using the terminal
- Combine commands: Use pipes (
|) and redirections (>,>>) to combine multiple commands. - Scripting: Bash scripting can automate repetitive tasks and combine many commands into one script.
- Customize your environment: Use
.bashrcor.bash_profileto customize your shell, setting up aliases and shortcuts for common commands.