How to Use Terminal: The Basics

Glowing monitors filled with lines of scrolling green text aren’t limited to The Matrix ; most IT gurus and power users prefer working with the command line over clunky graphical user interfaces because the CLI allows the user to operate directly with the system.

While the command line can seem terrifying at first, starting at the basics will allow you to see that it’s not much different from the graphical world you are used to. Learning how to use Terminal will give you a better understanding of how your Mac works under the hood, and give you the skills needed to troubleshoot any issue.

Choosing a Terminal Emulator

Before you take the red pill and jump into the complex world of bins and bashes, you’ll need to choose a terminal emulator. Like web browsers, various emulators have different features and interfaces. There aren’t many to choose from on OS X, but the default Terminal app will get the job done in a pinch. It offers horizontal pane splitting for viewing multiple inputs simultaneously, tabs, and a handful of built-in color schemes for making yourself at home.

If you want a bit more power, iTerm2 is a free emulator that includes more options than you’ll ever need. The app has all the features that Terminal.app offers, as well as enhanced pane splitting, customizable profiles, and a whole lot more that you don’t need to worry about quite yet.

Either will suffice for the purpose of this how-to, but I prefer to use iTerm2 for its additional features.

iTerm2: an alternative emulator

Navigating the filesystem

The most frequent commands you will use are ls (list) and cd (change directory). They are used for listing the contents of a directory and moving from one directory to another, just like Finder is used for browsing folders.

When you first launch a terminal, you’ll be plopped down in your home directory, a.k.a. “/Users/Name/”. To get an idea of how this relates to the GUI equivalent, open a new Finder window and select your name in the left hand column. You should see a few folders, including “Applications”, “Desktop”, and “Documents”.

Now, on the left hand column, select your hard drive in “Devices”. This location is the lowest level in the filesystem, called “root” (in command line terms, simply “/”). The root directory contains all the files that allow the operating system to operate, so it’s best not to meddle around here or you could cause all sorts of mayhem.

Listing Directories

In your fresh terminal window, type ls to list the files in your home directory. You should see “Documents”, “Music”, “Movies”, “Downloads”, and other directories that are created by default by OS X. If you type “ls -a”, it will activate the “all” flag to list everything—including files and folders that are hidden.

Listing the contents of your home folder in Terminal

Moving Around

To jump into one of these directories, type “cd ./Foldername”. The cd command tells the computer that you want to change directories, and the “./” indicates that you want to move forward relative to your current location. Alternatives include specifying the directory using an absolute path (a path that remains the same no matter where you are at the moment). This can be done by typing the full directory path from root all the way up. For example, “/Users/Name/Documents/”.

Tip: as you are typing, hit the tab key to auto-complete (ex. “cd /Users/Name/Doc[tab]” will expand to “/Users/Name/Documents”). If your flanges are tired from all this

typing, you can also drag a folder from Finder onto the terminal window and its path will be pasted on to the current line.

You’ve now done something useful. The first thing you usually want to do in your new directory is look around. Type ls to list the contents. Type “cd. ” to return to the directory above you (and re-read the above paragraph to make your way back!). Try moving around more to familiarize yourself with the cd command.

Basic File Operations

Now it’s time to perform some basic file operations like cut, copy, and delete. It’s probably best to make a test file so you don’t break anything important, so open TextEdit and create a file named “TestFile.txt” inside a new folder called “Test”. We’ll learn how to do this using the command line in the next How To.

Listing the file you just created

Copying

To copy a file or directory, you will use the cp command. Type “cp TestFile.txt TestFile-Copy.txt” to duplicate the file. This will create a new copy of the file in the current directory, but if you want to copy the file into a different directory, use “cp TestFile.txt /Some/Folder/”.

Tip: If you are copying a directory, you will need to specify the “recursive” argument to copy the directory along with its contents (cp -r). You can now use cd and ls to view your creation, or cheat by opening a new Finder window.

Moving

The mv command can be used the same way as cp, but it will move the file instead of copying it. mv can also be used to rename a file by doing “mv TestFile.txt TestFile-Renamed.txt”.

Renaming a file using the mv command

Deleting

The rm (remove) command is used to delete files and directories. Of all the various commands at your disposal, rm is by far the most ruthless and least forgiving.

Only rm a file that you are positive that you want to obliterate, and if anyone tells you to run “rm -rf /” do not do it. The “-rf” argument stands for “recursively” and “forcefully”, so the prompt will not ask for confirmation. If you’ve been paying attention, you’ll know that the “/” directory is the root of your operating system. So, if you add all these options up, this command will “forcefully and recursively remove every file on your hard drive”. Not a good thing.

Now, crawl back out from under your desk and carefully remove your test file using “rm TestFile-Renamed.txt”.

Tip: you can alternatively use “rm -i” to “interactively” delete the file so the prompt will ask for confirmation before executing.

To delete the folder, use “cd. ” as you learned earlier to back out of the Test directory (you can’t delete a directory you are inside of!) and then use “rm -rf Test” to nuke it. Poof! The folder and all of its contents are now gone.

Deleting a file using the rm command

Conclusion

Now that you’re a certified hacker, feel free to poke around the terminal a bit more and test out different arguments for basic commands. Every CLI tool has a manual page linked to it that can be accessed using the man command (ex. “man ls” or even “man man”). It takes a lifetime of practice to master the command line, so get cracking! Check back soon with part two of this series for more Terminal tips!

Source: mac.appstorm.net

Category: Forex

Similar articles: