Learning the Terminal

Thu, Feb 09, 2017

Understanding the Command Line Interface

The Terminal program on the Mac is a window into the command-line interface (CLI). This is the ability to interface with your computer using text commands, much the way it was done on Unix/Linux computers.

Terminal window

The command-line interface is also known as the Terminal, Shell or Console. The Mac Terminal is actually a program called Bash, which is it is sometimes referred to (or a combination of words, like Bash shell).

Listing Files in the Terminal

The first command we will use is the ls command, which stands for list, as in “list the current directory.”” When you launch Terminal, you generally start out in your home folder.

~$ ls

Listing in the terminal window

After typing in the ls command, you will see the folders that are in your home directory listed.

The ls command can also be used with certain options called flags, typically with a dash prefix. For example, there is also the ls -a command which not only displays the main files, but any hidden files. Hidden files typically begin with a dot . which make them hidden on a Mac.

~$ ls -a

Listing hidden files in the terminal window

In this example, we see a lot of extra hidden files and folders we didn’t see before. (Your computer will likely show different files and folders that above.)

There is another flag -l which will present the ls command as a long list with lots of extra data about the files, such as the permissions and size of each file. We can also combine both the -a and -l like so ls -al

~$ ls -l

drwxr-xr-x    3 admin  staff    102 Jul  4  2013 Applications
drwx------+   0 admin  staff   1394 Nov 11 22:24 Desktop
drwx------+  15 admin  staff    510 Aug  2 16:00 Documents
drwx------+  86 admin  staff   2924 Nov  2 16:06 Downloads
drwx------@  63 admin  staff   2142 Aug  2 16:16 Library
drwx------+   6 admin  staff    204 Aug 26 16:00 Movies
drwx------+   6 admin  staff    204 Aug 16 16:00 Music
drwx------+   4 admin  staff    272 Sep 18 14:05 Pictures
drwxr-xr-x+   4 admin  staff    136 Jun  8  2013 Public
drwxr-xr-x    0 admin  staff    138 Sep  3 17:00 Sites

Linux File Permission Notation

Let’s take one of the lines from the long list above and de-construct each part of it:

File Type

drwxr-xr-x+   4 admin  staff    136 Jun  8  2013 Public

The very first letter “d” (shown in red above) refers to what type of file it is.

  • `d` means this is a directory (folder)
  • `l` means this is a link, also known as a symbolic link, or alias
  • `-` means this is a normal file


drwxr-xr-x+   4 admin  staff    136 Jun  8  2013 Public

The next part specifies the permissions of this file, and who can access it. It is comprised of three letters R-W-X. It’s the part shown in red below:

  • r stands for read permissions
  • w stands for write permissions
  • x stands for execute permissions

Permission diagram

  • Owner typically refers to the user who made the file (the one logged in).
  • Group refers to a certain class of users arranged into a group, typically in enterprise environments (or anyone else you want to grant access to).
  • World/Everyone is anyone else who has the ability to access this folder or file, will have these permissions.

Here are a few examples:

  • -rwx------ The owner can read, write or execute. But the group and world have no access to this file.
  • -r-xr-xr-x Owner, group and world can all read the file, and execute it, but not change it in any way.
  • -rwxr----- Owner can read, write and execute, while the group can only read the file.

Specifying Permissions as Numbers (Octal)

Permissions can also be represented by numbers. This is called octal notation.

  • 0 no permission, same as a dash
  • 1 execute only
  • 2 write only
  • 3 write and execute (1+2)
  • 4 read only
  • 5 read and execute (4+1)
  • 6 read and write (4+2)
  • 7 read, write and execute (4+2+1)

Each class of permission (owner, group, world) is represented by one number. In combination, these three numbers can translate the letters R W and X for letter permissions.

Symbolic Notation Octal Notation English
---------- 000 no permissions
---x--x--x 111 execute
--w--w--w- 222 write
--wx-wx-wx 333 write & execute
-r--r--r-- 444 read
-r-xr-xr-x 555 read & execute
-rw-rw-rw- 666 read & write
-rwxrwxrwx 777 read, write, & execute

Extended permissions

drwxr-xr-x+   4 admin  staff    136 Jun  8  2013 Public

The last symbol, which only shows up sometimes as a + or @ refers to special extended permissions, usually applied by the Mac operating system.

drwxr-xr-x+   4 admin  staff    136 Jun  8  2013 Public

The next number specifies how many other folders or linked files are inside this directory.

Owner and Group names

drwxr-xr-x+   4 admin  staff    136 Jun  8  2013 Public

The next part specifies who owns this file (admin), and what group (staff) also may have access to this file/folder, depending on the permission set. In the permissions segment drwxr-wr-x, the rwx refers to admin, since admin is the owner, while the r-w refers to the staff group.

File size and date

drwxr-xr-x+   4 admin  staff    136 Jun  8  2013 Public

The next segments show you the size of this file in bytes, and the date it was modified.

Name of file or folder

drwxr-xr-x+   4 admin  staff    136 Jun  8  2013 Public

Typing the cd command, means to change directory.

~ $ cd Desktop

The dot . means the current directory.

The double dot .. means the parent directory.

The tilde ~ always points to your home folder.

Desktop $ cd ~

The above example would change directory to the home folder regardless of where you are.

Regular Expressions (RegEx)

Regular Expressions (RegEx for short) is a way to search for strings — characters like words, numbers or symbols — within a document. It uses special syntax which matches various types of characters.

In a typical search function you can look for a particular word in your document. But, what if you wanted to do a more sophisticated search? For example, maybe you wanted to find words followed by a number, and only appearing at the end of a sentence. Or maybe you want to search a database for e-mail addresses, or specially formatted phone numbers. These would be impossible to do with typical search functions; RegEx to the rescue!

There are different ways for finding strings in a document. Let’s start by using a program like Sublime Text 3.

Open a new window, and then pressing Command F. This will open a special search bar.

Sublime search bar

Make sure to click the button at the left signified by a .* to turn on RegEx mode. This will ensure that all searches are interpreted by RegEx.

Dummy Data

Below is some dummy data we’ll use to test out our RegEx code. It is a .csv file, so it follows a standard structure that we can use for searching and replacing.

Copy and paste the data below into Sublime Text.

Courtney Brown,Mauris.vel.turpis@vel.org,Cameroon,-43.07209,-105.75915
Halla Larsen,vel@nascetur.ca,Canada,-12.90675,89.49071
Macy Carey,mauris@nonhendrerit.co.uk,South Africa,-30.81666,125.02456
Belle Serrano,est.mauris.rhoncus@felis.net,Burkina Faso,-32.09538,18.41946
Yen Spears,ornare@tellus.co.uk,Nigeria,-46.77464,-128.13282
Jakeem Gibbs,metus@turpis.edu,Colombia,79.50706,-87.32717
Katell Avery,imperdiet.nec.leo@sapien.org,Maldives,40.65149,-62.05103
Lynn Tyson,blandit.at@urnaVivamus.co.uk,Dominican Republic,-42.27195,80.0625
Mannix Moore,ante@rutrum.co.uk,Mexico,25.51317,78.70432
Quail Noble,ante.Vivamus@iaculisaliquetdiam.ca,Pitcairn Islands,68.80078,-118.66456
Hanae Moody,lectus@iaculisaliquetdiam.com,India,41.05686,-76.28617
Samuel Byers,Vivamus@sodalesnisi.org,Solomon Islands,51.91773,56.70633
Unity Butler,eu.enim.Etiam@sed.co.uk,Indonesia,67.25292,-133.36082
Amber Burnett,fermentum.arcu.Vestibulum@blanditcongue.edu,Nicaragua,-19.47821,-145.52212
Mary Reyes,lacinia.mattis.Integer@orciUtsemper.ca,Paraguay,10.91913,167.05769
Libby Crane,lacinia@vitae.co.uk,Iraq,16.77948,-32.15076
Jeanette Bond,Donec.consectetuer.mauris@malesuadavelvenenatis.org,Romania,8.10485,169.19397
Duncan Dawson,non.luctus.sit@pharetra.co.uk,Mexico,49.76061,-148.66045
Ishmael Yang,justo.faucibus.lectus@vel.org,Bangladesh,-39.82216,96.54887
Wayne Villarreal,penatibus.et.magnis@sem.edu,Gambia,-31.61865,-47.59441
Sarah Chen,neque.In.ornare@idblandit.ca,Benin,12.56237,-11.0332
Mariko Houston,nunc.ac@purusMaecenas.org,Micronesia,87.59088,-170.46061
Odette Battle,Ut.tincidunt.orci@metusIn.edu,Jamaica,75.82169,158.82074
Donovan Solis,mauris.ut.mi@ipsum.com,Cyprus,82.66313,-1.74051
Allistair Walsh,cubilia@dignissimpharetra.ca,Lithuania,69.27198,-148.00384
Scarlet Alston,aliquam.eros@ut.com,Bahrain,7.46426,74.20319
Kiayada Arnold,Pellentesque@risus.org,Uruguay,-76.58459,-87.51434
Axel Gross,ipsum@Praesenteu.ca,Germany,-69.15157,-1.1974

As the following comic implies, RegEx can be a very tricky system to master and often results in lots of errors, spurious results, or invalid data. Use with caution. Still, it’s an important system to know about.

Follow the Regex One Interactive Tutorial.

XKCD comic