Unix on Mac: Command Line Basics

I started using Unix on my Mac about a year ago. I am still a novice at using the command line but I have come to realize how powerful and useful this interface can be. To learn how to use the command line I watched two videos from the PeepCode screencast website – videos number 34, command line basics, and 37, advanced command line. Below is just a short overview of the most important concepts and commands.

Unix
Unix is multitasking and multiuser operating system that is based on a hierarchical data structure. Now a days, most computers can handle a large number of tasks and users; however, when Unix was originally developed this was not the case. One interesting thing about the design of Unix is that the system handles everything as though it were a file. This means that folders and executable files can be copied, read, moved, deleted and processed like a text file. Mac’s version of Unix is called Darwin (many different versions of Unix exist).

The Terminal Utility
To access Unix on OS X there is an application called Terminal that comes standard on all Macs. This command line app provides access to Unix via a text based interface. The benefit of using Terminal is that you have access to all of the power of Unix; the downside is that the interactions are all text-based and there is little feedback provided when commands are executed.

So now let’s dive into the terminal interface and go over many common commands. Here is a screenshot of my Terminal window. The first thing worth noticing is that the command prompt contains three important pieces of information: (1) the system name followed by a colon; the (2) the current directory followed by a space; last, (3) the current user name followed by a dollar sign. Here is an example from the screenshot bellow: “Julio-Terras-MacBook-Pro:Farm-Bridge julioterra$”. As you can imagine, the information contained in these command prompts is quite useful.

Unix Commands
It is useful to think of Unix commands as scripts or applications. The structure of most unix commands is as follows: [command] [modifier] [source] [destination]. The first two parts of this structure hold true for all commands. Modifiers are always preceded by a dash (-). The third and fourth elements are only used for relevant commands. Here is a list of the most common commands along with relevant modifiers – all of these commands are case sensitive (Here is a link to a useful wikipedia list of Unix commands).

One important characteristic of Unix systems is that when a command is successfully executed then the system does not return any information (they usually only return error messages unless the user requests a ‘verbose’ output, more on this later).

Now here is a list of common Unix commands:

  • “help” – the help command returns a list of the most common commands available.
  • “pwd” – returns the full path of the current directory
  • “cd” [path] – changes the current directory to the specified path. There are some special characters that can be used to navigate directories: “~” always refers to the root directory; “.” refers to the current directory; “..” refers to the parent directory.
  • “ls” [modifier] [path] – returns a list of the files in a given directory. The path argument is optional, if no path is provided then the command returns the files from the current directory. Common modifiers include: “l” returns more detailed information about each file; “a” returns all files in a directory, including the hidden ones; “h” when combined with “l” returns the same info as “l” by itself but it also provides more details regarding file sizes.
  • “chmod” [modifier] [source] – changes the access privileges associated to a file or directory. To understand the modifiers for this command you need to understand how access privileges are managed in Unix. There are three separate sets of access privileges that are available for any given file or directory: read (“r”), write (“w”), and execute (“e”). These privileges can be set for the user (“u”), group (“g”), others (“o”) or all (“a”). The character “+” is used to add privileges, while “-” is used to remove privileges.
  • “cat” [source] – known as the concatenate command it reads a file and outputs the content to the terminal window.
  • “cp” [modifier] [source] [destination] – copies the file or directory identified as the source to a location and name identified as the destination. It is important to note that if another file exists with the same name at the destination then that file will be overwritten. List of useful modifiers: “i” to prompt user before system overwrites another file at destination; “r” means recursive and is used to copy a directory along with all of its contents.
  • “mv” [modifier] [source] [destination] – command to move a file or a directory from the source to the destination. This command is also used to change the name of a file. Useful modifiers include: “i” to prompt user before system overwrites another file at destination; “v”, which stands for verbose, to inform user of the activities that system is doing in carrying out this task; “n” to ensure that the system does not overwrite any files in the process.
  • “rm” [modifiers] [source] – command removes files from the source location. Useful modifiers include: “i” to prompt user before system removes any files specified; “r” to instruct the system to remove all files within a directory (otherwise, the “rm” command is only able to delete empty directories).
  • “mkdir” and “rmdir” [path] – these commands can be used to create and delete a directory respectively.

To get help with a specific command you can often use one of the following modifiers: “–help”, “-help”, or “-h”. Unfortunately, the help feature is implemented in an inconsistent manner across commands so you may need to try all three options, and sometimes none of them will work.

The last important item that I want to cover is the concept of “pipe”. Pipe is a functionality in Unix that enables a user to pass the output of one command to be processed by another command. The “|” character is used to identify a pipe linkage. One command that is often used with a pipe is the “more” command. This command makes it so that a the system only outputs on screen’s worth of content at a time (the user needs to press return or the space bar to request that the system display more content). Here is how a pipe could work: “cat filename.txt | more”

That’s all for today.

Tags: , ,

One Response to “Unix on Mac: Command Line Basics”

  1. [...]                                    « Unix on Mac: Command Line Basics [...]

Leave a Reply