Remake: Mac Terminal Tutorial
Working with Files and Directories
Welcome back to FreshMacApps Tutorials. We have decided to update and improve some of our earlier tutorials with additional information and some useful insights that will help you get the most out of your Mac.We believe that learning how to use OS X through the Terminal interface can open up new opportunities for Mac users. We hope this will empower users to explore the world of programming and increase their understanding of how the underlying technology operates. Learning the command line vastly increases your ability to accomplish your goals. Overall, it is a more powerful way to conduct your business. Additionally, we hope that these tutorials for the command-line (another term for the Terminal application) pique your interest in coding and lay a foundation for an understanding of the world of computer code. These tutorials are basic, but they are the first steps into a larger world of programming to explore.
Getting Started With The Command Line
To start off with, open up command-line and navigate to a folder where you can create and delete files without creating too much of a mess. I recommend using the Documents folder. A quick way that you can navigate to your Documents folder is:
The cd stands for “change directory”. You will see the term directory a lot in the programming world. For the most part, terms like “folder” are tools to help humans adjust to the concepts of modern computing. Once you enter the world of programming, these crutches will fall away into a sea of technical jargon. However, for now all you need to know is that, in most cases, the term “folder” is interchangeable with “directory”.
Keeping your workspace organized is especially vital when working with the Terminal. Before we get too far into this tutorial, I will show you two ways to clear your console screen in case things get a bit cluttered. For the first method you type in:
What the clear function does is simply to push all the previous clutter up and offscreen. The end effect is to create a space on your console so that you may work. If you were to scroll up, you would see your previous work sitting right offscreen. The other method utilizes a keyboard shortcut to achieve the same end. To use it, hit the Control key then the L key. Many users prefer using the keyboard shortcut rather than type out “clear“. They essentially do the same thing, so I recommend that you use whichever method you feel the most comfortable with, i.e., the one that you can remember offhand.
Creating Directories Within OS X
As I explained previously, when you create a directory, what you are actually creating is a folder. Directories are subsections on your hard drive where you store files. Having directories makes the whole process of finding what you need a lot easier. For instance, you could have a directory for files that you made using a word processor, or ones made using a graphics editing program. Now, when you search for the essay you need, you can skip all the graphics files. We need to create a directory (folder) where we can create and delete files.To do that, we will use the mkdir command like so:
If you type in ls, you should see the newly-created directory alongside your other files in the Documents folder. You are ready to take the next step by creating your first files.
The touch command is a method for creating files with the Mac OS X command line utility. What touch does when called upon is create an empty shell file encoded to the user-specified file format. To work with the touch command, you will need to navigate to your Practice directory in the Terminal and input the following:
From the Practice directory enter this code:
If you type in ls you should see file.txt as the only file populating your Practice folder.
Since we created a first file, it is the appropriate time to learn how to move that file from the Practice directory and to another folder (directory). Create a new directory called PracticeFile within the same directory that holds the Practice folder. Enter the following command into your Terminal console:
mv file.txt PracticeFile
Once you have completed that, navigate to PracticeFile using the cd command and then use ls to list all the files in the PracticeFile directory. You should see file.txt.
If you are going to create files, you will most likely find the need for a way to dispose of them. You can delete the file.txt that you created earlier, using the rm command. In your Terminal type in:
If you were to list the contents of PracticeFile, you should now see that it is empty. Now to delete everything you will need to navigate back to Documents. You can do so by typing the following command into your command line console:
Using .. will navigate you backwards in the file tree by one directory. This is significantly quicker than typing out an entire file path. Now to delete the Practice directory and its contents, type in:
rm -r Practice
The -r is a flag (we will talk about flags in a bit) that stands for recursively. If you just use rm without the -r flag, the system will throw an error at you saying “Practice is a directory and you cannot delete it like a file” or to that effect. If we want the rm program to be able to delete the directory we will have to modify its behavior, with a flag, to use something called recursion. The concept of recursion in computer programming means that a program is able to call itself if certain sets of parameters have been met. Using rm recursively enables it to cycle through the contents of a directory and delete data over and over until there is nothing left. After using rm -r, every file or directory that we created so far should be deleted. Using the ls command you will see that the Practice directory has been removed from Documents.
Searching with Grep
The ability to search Mac OS X files using the command line will give you a tremendous boost in efficiency. One of the most basic, yet powerful ways to search through files is with the grep command. The grep command is a Terminal utility that sifts through text looking for a pattern that the user has specified. If grep matches the pattern, it prints the lines containing the match out to the console. Oh, and it first developed in nearly four decades ago. Right about now you might get to wondering: “Well that is friggin fantastic but what’s so special about this command? What is the relevance?” The grep command is deceivingly simplistic, which masks the power it can unleash. I am going to show you a few minor, non-mind blowing ways that grep may be used.
Take the three nonsensical lines written below this paragraph, and copy them into a text file named grepped.txt and put that text file onto your Desktop.
How now brown cow She sells sea shells Dad was very sad
Open up the Terminal application and navigate to your Desktop using the cd command. From the Desktop enter the following into your console:
grep sells grepped.txt
You should receive feedback that looks like
host:Desktop User_Account$ grep sells grepped.txt She sells sea shells
Grep will by default use case sensitivity when searching for the pattern. If you type in the following command you will notice that the Terminal does not return anything.
grep Sells grepped.txt
To have grep search with case insensitivity you can use a flag to alter the behavior of the program. Flags are an essential part of using the command line, and the following explanation should be in no way be interpreted as complete. Flags are single characters either alphabetical or numerical that are prefaced by a dash that modify the behavior of the Unix command that it is attached to. Occasionally a flag can end up being a word. For instance, you will run across commands while using Unix that do not recognize the -h help flag; instead it will require –help.
Concept of flags in Unix is an essential piece of the programming puzzle. I recommend that you find some resources on flags so you can read up on the topic. Understanding where flags fit into Unix is critical to using the Terminal application. Okay, now back to grep. To make grep case insensitive put a -i flag after the command like so:
grep -i Sells grepped.txt
The Terminal should now return to you something along the lines of:
She sells sea shells
That about does it for this tutorial. It is beneficial to remember that by diving into the Terminal you are eliminating one less intermediary between you and the CPU. It is like the difference between knowing your boss and knowing somebody who knows your boss. With fewer intermediaries, you can exert greater influence on events. Let us know what you think by leaving a comment below, or if you have questions send us a message.