A Digital Age Deserves A Digital Leader

5 UNIX-y Things To Do With Your Mac

5 UNIX-y Things To Do With Your Mac

Postby gries818 » Sun Jan 02, 2011 2:52 am

5 UNIX-y Things To Do With Your Mac
Last Updated: 1.1.11

Every technical user has heard it: the simplicity of OS X is only possible because Apple has "dumbed down" the OS. If you bother to take a closer look at OS X, this old adage just does hold up. That's because aside from a very customizable interface, OS X is built upon UNIX. Even as a major Windows "geek", I hadn't heard of UNIX until I began to seriously look at alternatives to Windows; in short UNIX is a traditional operating system that is arguably the most influential in the world. Likely, you interact with UNIX derived OS'es everyday without even realizing it... servers, computer platforms, mp3 players, smartphones, etc.

As an OS X user, having access to a UNIX based system enables you to do all sorts of things that I personally find much more difficult (and sometimes expensive) on Windows. Here are 10 UNIX-y things you can do with your Mac (this list is ordered, knowledge of the items at the top of the list will help with items later on).

Terminal.app
Users can't really take advantage of the incredible power of a UNIX system without having access to a command line. Mac users can access the "bash shell" though Terminal.app (/Applications/Utilities/Terminal.app). Old Windows users may find the Terminal interface similar to CMD.exe, but will notice that many DOS commands don't work. A large (but not exhaustive) list of commands can be found here (OS X has a slightly different set of commands than Linux, so not every command will work exactly the same), but here is the simple list of basic commands you will need to perform basic navigation:

  • ~ = the tilde is not a command, but an important shortcut to the user's home folder that can be used with any command. Therefore ~/ represents /Users/(your username)/.
  • cd = changes directory: cd ~/Desktop changes the the current location from /Users/(your username)/ (default) to /Users/(your username). You can change to any directory available on your computer.
  • cp = copies a file: cp source_file.doc copied_file.doc
  • mv = moves a file, also can be used for renaming: mv original_file.doc newFolder/original.doc OR mv original_filename.doc new_filename.
  • rm = removes a file, must use the -r option to remove a folder: rm unwanted_file.doc OR rm -r unwanted_folder.
  • mkdir = creates new directory: mkdir new_folder.
  • sudo = allows the execution of commands as the "root" user (BE CAREFUL WITH THIS ONE): sudo rm unwanted_file.doc
  • clear = clears the Terminal window.
  • ls = List contents of the current directory.
  • nano = Opens a simple text editor on an existing file or creates a new file: nano existing.txt OR nano new_file.txt

With that, you should have a basic understanding about how to begin using Terminal for basic tasks:
Image

Scripting
Windows power users will likely be familiar with the batch files; as an OS X user, you have a number of different choices for scripting languages. Shell scripts are probably most similar to batch files and work in much the same way. This simple script (script.sh) will navigate to your desktop and create a new folder called folderFromScript.

Code: Select all
#!/bin/bash
cd ~/Desktop
mkdir folderFromScript


Shell scripts can be run from the Terminal like this:

Code: Select all
sh script.sh


Users of OS X also the ability to create scripts in Python, Ruby, Perl, or tcl.

Program in Compiled Languages with GCC
Microsoft offers free Express editions of Visual Studio (Visual Basic, C#, C++), the best way to make applications for the Windows platform. Apple also offers a similar application for creating applications for Mac OS X (and notably iOS), XCode, but unlike Microsoft's Visual Studio, the entire XCode is free HERE. XCode's syntax highlighting syntax features a number of languages, notably C, C++, and Objective-C (the language many applications for OS X and iOS are written in). At the heart of XCode lies the GCC compiler, a powerful and popular compiler for UNIX-based systems.

As an OS X user, you therefore have two choices when creating your own programs: you can use XCode and compile your application in XCode OR you can use GCC from the Terminal. I'm not a power developer: most of the small programs I've written while experimenting with different languages are only one or two files large, so I have the most experience using GCC to compile my work (it feels more UNIX-y anyway :) ).

There isn't much to compiling a simple program with GCC, larger and more complex projects will like need to specify different command line parameters than I have here, but I'm not aiming to give a complete tutorial of how to use GCC here. The -o option used in all of the below examples sets the outgoing filename for the compiled program, omitting this parameter will cause the compiler to output an a.out file. You will also need to write programs in languages that GCC supports.

A simple C example:

test.c
Code: Select all
#import <stdio.h>

int main()
{
   printf("Hello World\n");
   return 0;
}


Compile with GCC and run
Code: Select all
gcc test.c -o testc
./testc


A simple C++ example

test.cpp
Code: Select all
#include <iostream>

using namespace std;

int main()
{
   cout << "Hello Word" << endl;
   return 0;
}


Compile with GCC and run
Code: Select all
g++ test.cpp -o testcpp
./testcpp


A simple Objective-C example

test.m
Code: Select all
#include <Foundation/Foundation.h>
#include <stdio.h>

int main()
{
   printf("Hello World\n");
   return 0;
}


Compile with GCC and run
Code: Select all
gcc test.m -o testobjc -lobjc
./testobjc


Note: The Apple version of GCC compiles executable files in 64 bits by default so if you so desire, use the -m32 flag to force the compiler to use 32 bit mode instead.

Compile and Install Program from Source
If you installed XCode as an instructed in the previous item, GCC can also be used to compile and install full applications from source. This is especially useful with Linux and Unix applications, which often have binary installers for Linux distributions (commonly .deb or .rpm) but sometimes don't have them for OS X (commonly .dmg).

For the purposes of this example, I will give you instructions for installing wget, a useful piece of software that can be used to directly download files to your computer from Terminal. If you are familiar with Linux, you may have used wget before, but for some reason Apple doesn't include wget with OS X.

First, I recommend creating a src folder in your Home directory (~/):
Code: Select all
mkdir ~/src


Now you should download wget from HERE. This is the newest version as of the creation of the guide, but these instructions should work for newer versions of wget (though the program is so simple, I'm not sure what functionality new versions will provide). Your browser probably downloaded the file to ~/Downloads, so you can move the file to your src directory:
Code: Select all
mv ~/Downloads/wget-1.12.tar.gz ~/src/wget-1.12.tar.gz


You should now unpack the tar.gz file (sometimes source files are packaged with tar.bz2, so different command line parameters are needed if that is the case) and move in to the newly created source directory:
Code: Select all
tar xvjf wget-1.12.tar.gz
rm wget-1.12.tar.gz
cd wget-1.12


Now in source directory, you have to provide the installation scripts with information about your computer so it compiles correctly for Mac OS X. This is simple enough to do:

Code: Select all
./configure


The configuration script will now check to see what relevant packages are already installed on your computer. You will notice that some of the packages exist on your Mac and some don't - you shouldn't worry about the ones that don't unless the configure script terminates with an error. If the ./configure script finishes successfully, you can procede to compile the program:
Code: Select all
sudo make


Linux users already familiar with compiling programs from source will notice that on Mac OS X, make apparently needs to run as root. I haven't ever been able to compile or install programs without doing this, but this may not be required for every application or every version of OS X. If the make finishes without errors (warnings are ok and are, in fact, common), you can execute the final command to install wget on your computer:
Code: Select all
sudo make install


After this command finishes, you can now test wget with these commands:
Code: Select all
wget --help
wget http://img186.imageshack.us/img186/4153/globenetworked.png


Note: Because Apple's version of GCC automatically creates 64 bit executables, certain packages may fail to build correctly. In that case, you may need to set export certain environment variables to force the GCC to temporarily compile the package in 32 bit mode. The correct flags to use depends on the application in question (check the documentation), but these two commands may be useful:

Code: Select all
export CFLAGS="-m32"
export CXXFLAGS="-m32"


Host a Website with Apache
Mac OS X ships with the popular web server Apache and the popular web scripting language PHP already set up and ready to go. To use web server, simply open up the System Preferences, navigate to the Sharing panel and check the box for web sharing:

Image

Your website should go in /Library/WebServer/Documents, which you can navigate to by going to Finder > Go > Go to Folder. You can test out your site with a simple PHP file which will give you information about your PHP configuration (save as phpinfo.php):

Code: Select all
<?php
phpinfo();
?>


If saved in /Library/WebServer/Documents, this new page can be accessed from http://localhost/phpinfo.php in your web browser.

--------------
This quick guide obviously isn't intended to be a thorough review of the topics covered, but should rather serve as a starting point for users wanting to get into the more technical offerings of Mac OS X. When I first got my Mac (and even before when I was playing with Linux), I had to look around the interent to find out how to do even the simplest of things covered in here, so hopefully this guide will save you some time in tracking this stuff down on your own.

This guide is also not intended to list all of the possible UNIX-y things that can be done with OS X.
Software Development
User avatar
Posts: 3991
Joined: Wed Jul 07, 2004 6:28 pm

Re: 5 UNIX-y Things To Do With Your Mac

Postby kanaloa » Sun Jan 02, 2011 4:12 am

^*^ Very resourceful. Thanks for posting this.
"Greatness is not a function of circumstance. Greatness, it turns out, is largely a matter of conscious choice, and discipline." - Jim Collins
President
User avatar
Posts: 11795
Joined: Sun Mar 10, 2002 1:18 am
Location: Columbia, SC
Real Name: John Derrick

Return to Other Operating Systems

Who is online

Users browsing this forum: No registered users and 1 guest