Skip to content

Terminal Intro

The Terminal application (Applications/Utilities/Terminal.app) on OSX is a powerful tool for users to gain greater control over their working environment. Since OSX is based on UNIX, most UNIX commands will work here. However, Apple also added custom commands that leverage the power of OSX for the command line (more about that in a later post).

While I was aware of the “Command Line” for quite some time, I have never had the need or time to delve into this daunting and sometimes scary environment. This changed when OSX (10.7 ?) started to hide the “Library” folder from us and I needed to access it. So I googled and found this:

chflags nohidden ~/Library/

While I didn’t completely comprehend what was going on, it was easy enough to see that after the command was run, the system would set the “flag” “nohidden” to the “~/Library/” folder. This got me curious but work took me into different directions and the Terminal remained a mystery. When I had issues with corrupted files, my supervisor would bring out Terminal and like magic would know within minutes why the file did not behave and how to fix it. I watched in awe, trying to remember what commands were used: cd, ls, wc, grep, du, cat, >, <, |. All gibberish at the time.

Open the Terminal app:

You can find the Terminal app in your “Applications” folder. There it is located in a folder called “Utilities”.

terminal location

Your default terminal window will look something like the following:

default terminal

Your colours might be different, but you can easily adjust that with the preferences.

If you are running OSX, by default you will be using “BASH”, which is short for “Bourne again Shell” (https://en.wikipedia.org/wiki/Bash_(Unix_shell)). I will be assuming Bash syntax for all discussions.

First Commands:

Two of the most basic commands for traversing your filesystem are: cd and ls. They allow you to navigate and display all files and folders on your hard drive (as well as external hard drives, CDs DVDs, etc.) within the Terminal.

cd stands for “change directory

ls for “list

Let’s try. Type ls. This will list all the content of your current location, in my case:

lscommand

As you can see, using the ls command we get a readout of the current directory.   Let’s change directory by typing: cd Desktop

cdDesktopNotice how we have changed directories. A nice feature to use is autocompletion when typing paths or filenames. Simply enter the first two or three letters and hit <tab>. Terminal will attempt to autocomplete the entry for you. Type ls again to see the content of your desktop:

lsDesktop

After moving around a lot, it is easy to forget where exactly we are in the file system. The readout on the Terminal only shows the current Folder, but not where that folder is located. For this we use the pwd command or “present working directory“. This will print out the absolute path of where we are currently working.

pwdcommandListing your folder content is great, but by default there is not much useful information that is displayed. To personalize the output a bit more we can add “options” to our ls command. Typically options are single letters and are preceded by a dash “-“. Some of the more useful ones for ls are:

ls -l     long” version of each entry: file permissions, user, size, date, name, etc.ls-lcommand

ls -h     makes the long output more “human readable” by converting file sizes to kb, mb, etc.

ls-lhcommand

ls -a     displays “all” files and folders, even hidden ones (remember the hidden Library folder? Now you can peek into it). Hidden files and folders have the ‘.‘ before the filename.

ls-acommand

ls -p     is very useful and adds a trailing “/” to the end of directories so they can be distinguished from files.

ls-pcommand

You can also combine them (almost) arbitrarily:

ls -lhap

Since the Finder is still a very useful tool for many things, there are two nice shortcuts you can use with terminal in conjunction with the Finder:

Since we always need a file path for the cd command, things can get quite cumbersome if you (like me) prefer many nested folders for an organizational structure. So if you need to know the absolute path of anything, just drag and drop a file or folder on the terminal and voila: instant absolute path. (Also works for external devices!).

dragndropdropIt

Which will result in an absolute path I would never want to type:

longPathWhat if you have navigated deep into your system’s file-structure using Terminal and now you would like to see where you are in the Finder? Easy, use the following command: “open .“(open space period). The period is a shortcut for your current location. So whatever folder you are currently in will “open” in the finder. You can also use the “open” command to open files. Go ahead give it a try.

A typical case where I use these two are when I am browsing and viewing journal articles (PDFs) online. OSX has this nice function where you can open the PDF in Preview. Checking the location of the file, we see that it is currently stored in a deeply nested folder structure.moveFileotherLocation

Curious as I am, I want to go see what’s going on in these folders so I drag it into the terminal after writing cd (Yes, you could have used the Finder “Go to Folder…” feature, but what’s the fun in that?). Now I can look around and get an idea of what is saved there.finderExample But since I will not be doing any file or folder manipulations, it might be more convenient to use the Finder: open . to the rescue.

openFinder

openedFinder

So go ahead and look through your files and folders. Next time we will look at some simple commands so we can view, manipulate, create and remove them.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.