11 Useful terminal commands on mac

In this article I will be talking about the most useful terminal commands on mac. These commands are the following with some examples:

  1. history – history
  2. grep – history | grep cp
  3. mkdir – mkdir images
  4. nano – nano story.txt
  5. cd – cd images/
  6. rm – rm story.txt
  7. open – open .
  8. top – top
  9. lsof – lsof -i TCP:22
  10. clear – clear
  11. ls – ls -a

I will also be explaining how to chain commands together and how to pipe outputs using the | operator.



1. history

Useful command to see all your previous commands used on the terminal.

Example: history

The command above will output a history of all the commands you used on terminal, with a number associated to the command.

Example: history | grep history

The command above will find the usage of the word ‘history’ in your terminal command history, you can replace history with whatever you are searching for.

Example: history | tail -20

The example above will display the last 20 commands you entered on terminal.

History with pipe that only outputs the last 5 commands

Example: !4553

The example above uses the number of the command given in the history output and writes the command for you so you do not have to copy and paste the command or have to rewrite it. Replace 4553 with whatever the number of the command you want to reuse from the history output.

2. grep

Search’s for all the lines or lines in all files in a dir that contain the pattern given in the command. Syntax grep [search_pattern] [file or dir].

Example: grep "once" story.txt

The command above will search for the word “once” in the file story.text and then output the line containing it.

using grep command on mac
using grep command on mac

Example: grep -r "once" /stories

The command above will search all the files in the specified directory recursively for the search pattern “once”.

3. mkdir

This command is used to make/create directories.

Example: mkdir stories/

The command above will create a directory called stories in the ~ root directory. This command will create the directory in whichever directory you are in.

using mkdir command on mac
using mkdir command on mac

4. nano

This command is used to create, view and edit files. The syntax is nano [filename].

Example: nano story.txt

The command above will check if story.txt exists, if the file exists it will allow you to edit and view the contents. If the file does not exist it will create a new file and allow you to edit and view it.

using nano command on mac
using nano command on mac

5. cd

This command is used to change directories. The syntax is cd [directory].

Example: cd stories

The command above changes the current directory to /stories.

using cd command on mac
using cd command on mac

Example: cd ..

The command above will change directories to one higher level. So if I had used it above it would go back to ~.

6. rm

This command can be used to remove files and directories, even non empty directories. The syntax is rm [file or directory].

Example: rm story.txt

The command above would delete the story.txt file.

using rm command on mac
using rm command on mac

Example: rm -rf stories

The command above deletes all files and folders contained in the stories directory.

7. open

This command is used to open files or directories in the finder window.

Example: open .

The command above opens the current directory /stories in the finder window.

using open command on mac
using open command on mac

8. top

This command allows you to view all the processes running on your mac, as well as CPU usage and other stats.

Example: top

using top command on mac
using top command on mac

9. lsof

This command allows you to view ports in use by services on your mac.

Example: lsof -i :6942

The command above checks if a specific port is in use and by what. You can replace 6942 with the port you want to check.

using lsof command on mac
using lsof command on mac

Example: kill -9 766

The command above will kill the process running associated with the PID, thus freeing the port in use. Replace 766 with the PID you want to kill.

10. clear

This command clears the terminal window of any output that was displayed. An alternative to this command is pressing CMD + K.

Example: clear

11. ls

This command lists all files and directories in the current path.

Example: ls

using ls command on mac
using ls command on mac

Example: ls -a

The command above lists all hidden files also.


Chaining terminal commands on mac

The following is a snippet from 0nn0’s github page.

chaining terminal commands on mac
chaining terminal commands on mac

Piping terminal commands on mac

The following is a snippet from 0nn0’s github page.

piping terminal commands on mac
piping terminal commands on mac

Leave a Reply

Your email address will not be published. Required fields are marked *