If you want to learn how to use the terminal command line on MacOS, then this article is for you. It'll teach you what you need to know about the Terminal in order to make it your own. Let's get started:
Preliminary Note: The Terminal is an application that is built into all Mac OSX systems and allows users access to a command line interface. This allows quick and efficient navigation around the computer, while also providing the ability to use operating system commands necessary for a variety of tasks. This article will be instructive and informative, but also provide some tips and tricks to make your time in Terminal that much more enjoyable.
Terminal Basics: First things first, let's learn about the basics of Terminal before we get started on any actual operations or commands. Terminal is an application that is available through the Launchpad application located in the dock. It can also be opened through the Utilities folder in the Applications list, but a quicker and more convenient way to access it is through the Launchpad. If you have never launched Terminal before, then the first time you launch it, you'll be presented with a dialog box that asks for your username and password. This is simply standard operating procedure for all applications on Mac OSX systems under Admin privileges. You will not need to type a password the first time you launch it.
Login: Once logged in, you will be presented with many different tabs that represent various different parts of Terminal. Each tab can be opened or closed by clicking on its title bar at the top of the Terminal window. Some of these tabs include: File, History, and Help. Default settings are enabled in all tabs, but it is important to make sure that "Enable root login" is selected (this gives access to all files). All applications on Mac OSX are administered under the root account (or Administrator account). This gives the system administrator full control over the system.
A more detailed look at the Terminal title bar: The top right area of Terminal shows a series of small icons that represent different features of Terminal. These include a button for opening a new terminal window, an icon representing your current terminal window, an icon to show your active directory, and two buttons for switching between tabs and window. The icon that represents your terminal window can be clicked on and dragged with your mouse, much like windows on a normal computer.
Navigating: Once you have logged into the root account, you will be able to navigate the file structure of Mac OSX. You will find that all files are stored in the directory /. Here, you'll see several directories such as Applications, System, Library and Users. To navigate through the files that are within these directories, you will use the same navigation keys that you use on your Windows or Linux computers. For example, to move to the /Applications directory, simply type cd /Applications and then press enter. To move back into the system directory (the root of all files), just type cd System.
Navigating: If you want to navigate to other directories within your computer then you can use commands such as cd (the command "change directory") or ls (short for listing). These commands are built into Terminal meaning you do not need to type them in italics (in other words, don't put cd or ls in quotation marks).
For example, to move to the directory my documents from your current directory, you would type in cd Documents. If you then wanted to move back one directory level, you could type cd .. and press enter. This is a great way of quickly moving up and down directories.
To see all the files within your current directory, you can type ls and press enter. This will display a list of all the files within your current directory. If you want to see more information about a particular file (for example, its size), then you can type in ls -la and press enter (this is an acronym for file permissions, access times and so on).
Jumping to the top of a directory: To quickly reach the top of the directory tree, press your up arrow key. This will move you to the very first level of files.
Working with text: Some text files consist of plain old ASCII (A-Z and a-z). If you want to display these text files you can change the type of file by typing in the command ls -t (the "text" extension). You can also type in cat (for concatenate) or less (for view). For example:
ls -t | cat
increases the number of lines visible by the cat command. If you wish to examine the contents of a file, but not display them on the screen, you can use less. This will keep displaying as many lines as it has been told to. If you wish to quit, press your q key twice.
If you wish to display the output of less with more coloured lines, use the syntax :more instead. In this case it will quit on your typing. Or you can press q twice.
Saving and Printing: The command called "pwd" (print working directory) will display the current directory in which you are working. For example, if your terminal window is displaying /System/Library and you want to print a list of everything, you can type pwd or ls (see sections "Basic Commands" and "The File System"). In this case a long list of every item in the folder is displayed.
If you want to save the output of a command (for example, pwd) you can simply type in the command name followed by a ">" (that's ">") and then press enter. For example, you can type in > ~/Desktop/pwd. The next time you open Terminal (if not done already), this will display on your Desktop. As an alternative to printing a listing, you can use Echo to "send output" to the terminal window where it is displayed as text with an asterisk symbol (*). For example, to display your current directory and the file pwd, you can type Echo pwd* into Terminal.
If you want to save a file as its full name (and not its file name extension), you can use the command called "cp." For example, if your terminal window is displaying a list of all files in the /System folder and you want to copy them all to another location, then you can type cp *.txt *.docx *.zip into Terminal. You can then type cp -r myfolder dirname. In this way, you can copy an entire directory from one location to another.
In addition to Echo and cp we can also specify what is the output of a command or line by using > (outputs to the terminal window) or >> (outputs to a file). For example:
Command_1 > out.txt
will output the first command in the terminal window and then it will stop. With this command lines are automatically centered in the output file. To avoid it, use the command Command_2.
Command_2 >> out.txt 2>&1
will output the second command to the same file, but this time lines will not be centered. The last character of the line is always "carriage return" (\r) so that in case you accidentally enter more than one command you will know which was at first. You can also precede every line with "!" or "@" symbol (this is very useful to write pure commands). For example, you can type:
cp -r ~/text-files/*.txt "!some_folder/*.txt"
The last line will create a file called alltextfiles.txt (including the extensions), which contains all files from the directory text-files. The next line will copy all files from some_folder to alltextfiles.txt one by one specified by the first path, in case you forgot to add any extension.
Creating and Opening Files: Opening a file is equivalent to creating it. If you want to create a file called "todo.txt", you can type echo "Hello World" > todo.txt in Terminal. If you want to open a specific file, you can type in its name followed by an "at" sign (@) and then press enter. For example, if you wanted to open the text-files folder on your desktop, you could type in cd ~/Desktop/text-files @and then press enter.
In addition, you can create a file from the output of a command. If you often use a text editor to write your documents, then you can save some time by typing in the following commands:
Press the space bar once (this makes it easier for others to read). Type any text and press Ctrl+X. Press enter. Then, again, type Ctrl+X. The text you typed will be displayed. Now press enter and your text will appear in the file todo.txt on your desktop.