How to Use Terminal on Mac
The Terminal provides a command line interface to control the UNIX based operating system that lurks below OS X.
While the Command Line Interface (CLI) can seem terrifying at first, it can give we a better understanding of how your Mac works while letting you perform tasks much quicker.
What is a Terminal?
Terminal is an interface in which you can type and execute text based commands. In short, it’s an application where you type commands to do things instead of using Finder. Terminal is inside Utilities Folder, inside Applications.
Terminal allows us do lots of action quicker than it takes with Graphical User Interface (GUI). To use Terminal, type ⌘ + space and type “Terminal”. This is how terminal looks like on Mac.
Core Terminal Commands
Terminal makes everything quicker. Here are some core commands to get we started and comfortable with Terminal.
Before we go over the commands, we can see that inside the windows, it starts with the name of the Mac Keshavs-MBP:, followed by the name of the current user NarulaKeshav. The thing in the middle, Pictures is the folder you’re currently inside of, in this case.
To find which directory we are currently inside of, we run the pwd command.
The pwd stands for “Present Working Directory”. As we can see, I am inside users/NarulaKeshav. But how can we list files inside the NarulaKeshav directory?
List Files in Directory
ls command list files and directories inside of the present directory.
Now we know how to list files in a directory. But there are more files that are inside this directory that are hidden. To see those files, we use the command ls -a. The letter with a hyphen is called a flag. We used the flag -a which lists ‘all’ the files.
Alright, that’s cool but how exactly do we change or move into a directory? For example, what would I do if I wanted to go inside documents?
Clear Terminal Window
Before we use a command that lets us move inside a directory, there’s just too many things going on inside terminal window. To ‘clear’ the terminal window, we use the command clear. We can also use ctrl + L and ⌘ + K to clear the screen.
Moving and Changing Directories
To navigate inside a directory, we can type the command cd [dir_name]**. For example, if I wanted to get inside of Documents, I would type:
** Do not take the […] brackets literally.
If we wanted to move inside a folder which is inside documents from the previous directory, we can type the direct path, cd documents/directory_name.
Moving into Directory with Spaces
We can now move inside a directory such as Documents, Downloads, Desktop. But what if the name of the directory contains a space? Like Rails Project? To navigate inside a directory containing space, all we need to do is add \ before the space.
cd "Sample\ Directory"
Or you can put “” around the directory name. For example:
cd "Sample Directory"
Moving Back a Directory
But now we’re stuck inside some directory inside documents, but we want to move back to documents. To do that, we use the cd command, followed by ..
If we wanted to move two directories back, we type cd../… If we wanted to navigate back to our Home directory, we can simply type cd & it’ll put us inside our home directory.
If we wanted to go back to our Root directory, we can type the command:
But now we want to make a new folder inside documents directory. How’d we do that?
Creating Directories and Files
To create a new directory, we use the command:
The command mkdir stands for ‘Make Directory’.
Great we just made a new directory, but what if we wanted to create a file? Like an HTML file? To do that, we type the command:
Opening a File
To open the file we just created, we can use the command:
Great! Now we can create a directory, create a file, and open a file, but what if we wanted to remove it? How’d we do that?
Removing Directories & Files
To remove a regular file, we can type the command:
Nice. If we wanted to remove a whole directory with content inside of it, we can use the command:
rm -f [dir_name]
If we wanted to remove a directory that had no content or files inside of it, we can just type the command:
If we want to make a copy of a file or an image, we can use the command:
cp [file] [new_file]
where [file] is the name of the current file and [new_file] is the name of the copied file.
Okay, but what if I wanted to copy this file to a different directory? To do that, we have to use the command:
cp [file_name] [directory/path]
Since I was inside a patterns directory, which is located documents/projects/patterns, I moved two directories back with ../.. which changed my directory to documents, and then I copied food.png inside a directory called media. Just make sure that the path is correct or else it wouldn’t work.
Moving & Renaming Files
The next command is kind of tricky because it can be used for both moving the file to a new directory or renaming it. To rename a file, we use the command:
mv [file_name] [file_new_name]
Great. We just changed gdfg.png to picture.png. Let’s move picture.png inside the pictures directory. To do that, type the command:
mv -v [file_name] [dir_name]
Great! As we can see, we’ve successfully moved picture.png inside pictures directory.
Few Terminal Shortcuts
There are many shortcuts we can use to do something even quicker. Here are few of my favorites.
- To go to the beginning of the line we are currently typing on, type Ctrl + A.
- To go to the end of the line we are currently typing on, type Ctrl + E.
- If we want to clear the entire line, use Ctrl + U.
- To re-use the command we previously typed, use the ↑ key.
- To auto-complete files and folder names, we can press tab half-way through.
- To kill whatever you’re running, like a server, type Ctrl + C.
- To view history of commands we used, type history n, where n is the number of commands we want to see.
- To see one-line description of a command, type whatis [command].
Now you’re an official Hacker! Feel free to play around with Terminal and it’s commands and test out the core commands. It takes time to get used to Terminal, but it’s unbelievable to see what you can do with it. If you have a question or see an error, leave a comment below.