Learning the Terminal

Thu, Nov 13, 2014



Complete the in-class tutorials on your own.

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, like pressing Command F on Sublime Text, you can search 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!

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