The iPhone Development Story

Wow, what an illuminating story! You can find the original here…
http://www.mikeash.com/?page=pyblog/the-iphone-development-story.html

I just got my first application in the iTunes App Store, and I wanted to tell the story of what it’s like to publish one, from start to finish. The app, NetAwake, is an independent project I wrote with my friend Joshua. It makes an interesting story, I think, because unlike the sordid tale of some people’s struggle to get accepted into the iPhone developer program, my personal experience was perfectly normal. (As far as I know.)

But even a perfectly normal experience with the iPhone developer program is intensely weird. Compared to the simplicity of developing and distributing a Mac app, Apple’s iPhone program is extremely convoluted and strange. Here’s the story, step by step. Continue reading


How To Import Non-Commercial DVDs Into iMovie ’08

Hopefully this hasn’t already been discussed; I searched all over the web and couldn’t find a good answer, so I figured it out myself and thought I’d spread the word!

So I had a couple of DVDs of wedding footage shot by my wife’s great uncle, and I wanted to edit the footage in iMovie 08. First I tried Handbrake to create a M4V file, but no dice, iMovie wouldn’t read that format. Then I figured out this method: Continue reading


Mac Tiger 10.5 Apple Registration Screen Loop Stuck

1. Turn off your Mac. Perhaps hold the power button down until it turns off.
2. Hold the SHIFT key down and startup. Be patient as it may take awhile.
3. DO NOT LOGIN yet. Click the back arrow (Restart).
4. Your computer should start installing the hazardous upgrades.
5. Allow your computer plenty of time to install and auto-restart.
6. You should be able to login and access all of the files you through were destroyed. Continue reading


Getting PHP 5.2.x To Run on Mac Tiger 10.5

So, you decided to upgrade to Mac Tiger Workstation and your PHP service stopped working. No fear, as PHP will still work fine once you make a simple change to your Apache 2 configuration file. Previous versions of Mac OS 10.4 and below were using Apache 1 series, not Apache 2 series. You need only un-comment a single line in the Apache httpd.conf file. Knowing where it is and how to modify it is not something most developers bother with. Assuming you have the permissions to make the needed change, your PHP service will be up in mere moments. Continue reading


UNIX Intro to Single-user mode

To get to single-user mode, hold down the command (i.e. cloverleaf or Apple)
and “s” keys as the system begins to boot. This will drop you into a command
line interface before the system has fully started up, giving you the chance
to check/repair things before much of the normal OS X environment starts up.

Single-user mode starts you with an extremely minimal environment — not
only is the normal graphical interface not running, neither are most of
the normal system daemons (init
and mach-init are the only ones), and the boot disk isn’t even fully mounted!
Usually, you want to bring at least a little more of OS X up in order to
get anything useful done. Exactly how much depends on what you want to do,
but usually you want to either just mount the boot volume
for write access
, or do that and then start the
various system daemons and components
(e.g. networking) that make up
most of the non-graphical parts of Mac OS X.


Mounting the boot volume for write access:

fsck -y
  – Check the boot volume’s file system, and repair if necessary
(the “-y” means “Yes, go ahead and fix any problems you find”). Always
do this first. Note that this may not be able to fix all problems in a
single pass, so if it finds and fixes anything (it’ll print “***** FILE
SYSTEM WAS MODIFIED *****”), run it again, and keep running it until it
comes back with something like “The volume Macintosh HD appears to be
ok.”

Note: If the volume has journalling enabled, you should get a message like

fsck_hfs: Volume is journaled. No checking performed.
fsck_hfs: Use the -f option to force checking.

In this case it should be reasonably safe to go ahead and mount the volume,
since the journal should have taken care of any necessary repairs. But I’m
paranoid, so I tend to recommend using fsck -yf to force a full
check.

See Apple’s TIL article
#106214
for more info.

mount -uw /
  – Remount the boot volume,
enabling write access. You need to do this before you can change anything on
disk, but always run fsck first.

Note: if this command works, it won’t give you any output (other than another
shell prompt). If it prints something like:
 root_device on / (local, read-only)
 devfs on /dev (local)


then you probably mistyped the command.


Starting system daemons and components:

Depending on what you need to accomplish in single-user mode, you may (or
may not) need to start up more of the operating system’s normal components
(access to disks other than the boot volume, networking, etc). This part of
the process works a bit differently depending on what version of Mac OS X
you’re using. Under 10.0 through 10.2, the
SystemStarter
command is all it takes. Under 10.3, you need
to register the Mach services, then launch
netinfod manually, then finally invoke
SystemStarter to finish the job.
Under 10.4, it gets easy again: you can use /etc/rc to
do the work for you.

After running any of these commands, do not attempt to continue the boot
with the exit command. If you aren’t sure what
version you have, use sw_vers to find out.

Starting daemons under 10.4:

sh /etc/rc
  – Under version 10.4, the normal startup script can be run by hand,
and it’ll do the necessary work of getting the system (mostly) up and
running, but not exit single-user mode or start the GUI.

Starting daemons under 10.3:

/usr/libexec/register_mach_bootstrap_servers
/etc/mach_init.d

  – This registers a bunch of programs to be triggered by mach-init,
which take care of setting up networking, directory services, disk arbitration,
etc. — see the config files in /etc/mach_init.d/ and

Apple’s web page on the boot process

(look for the section on Bootstrap Daemons) for more information.

Note: after this command finishes, several of the things it triggers will
print messages on the screen, possibly mixing in with whatever command you’re
trying to type, and making things generally confusing. You can force it
to reprint the command you’re working on by typing ^p^n (that is, hold down
the control key while pressing “p” and then “n”). Also, one of the messages
may imply that lookupd has failed; don’t worry, it seems to get restarted
normally.

(cd /var/db/netinfo; netinfod -s local)
  – Start up a netinfo server to handle the local domain (i.e. users,
groups, etc defined on this computer).

SystemStarter
  – Start up more of the system, including networking, NFS, and
many background daemons (basically, it runs all of the StartupItems). It does
not, however, start the Aqua interface; it leaves you with the single-user
command line when it’s done.

Note: this command prints out even more stuff than
/usr/libexec/register_mach_bootstrap_servers did. Don’t even try
to get a word in edgewise; I just wait until it finishes (it’ll print
“Startup complete.”), then press return to get a new command prompt.

Starting daemons under 10.0-10.2:

SystemStarter
  – Up through 10.2, this command would take care of starting pretty
much everything (except the GUI)… including everything that
register_mach_bootstrap_servers
and netinfod are needed for under 10.3.


Other Commands useful in Single-user mode:

pico
  – Edit text files (see the main entry for ).
The second line from the top of the screen may contain gibberish, and the keyboard
arrow-keys may not work (use control-characters
instead) but other than that it works normally. vi and emacs
are also available in single-user mode, although vi seems to have
problems with screen updating under at least some versions of OS X.

nicl -raw /var/db/netinfo/local.nidb
  – Edit the local Netinfo database (users & groups, etc) without going
through the netinfo daemon. Handy if the system can’t boot properly because
of a Netinfo configuration problem.

Note: do not use this form of the nicl command after starting the netinfod
server (either manually or with
SystemStarter).

Examples:

nicl -raw /var/db/netinfo/local.nidb

  create disabled

  move mounts disabled
  quit
Disable the NFS “mounts” directory (by creating a “disabled” directory and
moving mounts into it), which can bypass a hang at startup if the computer
tries to mount an unavailable NFS server.
nicl -raw /var/db/netinfo/local.nidb

  delete /localconfig/autologin username

  quit
Disable automatic login at boot time (normally controlled from the Login
pane in System Preferences), in order to avoid logging into a damaged account.

bless
  – Change the system’s boot settings (the “blessed” system). Useful to switch
back to OS 9 if OS X isn’t bootable, and you don’t want to juggle boot CDs.

Example:

bless -folder9 "/System Folder" -use9
Bless the OS-9 system folder named “System Folder” on the current (OS-X) boot
volume, and make OS-9 the default for booting from this volume.

tail
  – Print the last few entries in a log (or other text file). Useful to find
out what happened to get you into this mess.

Examples:

tail /var/log/system.log
print the last screenful of
entries from the main system log.
tail -1000 /var/log/system.log | more
print the last 1000
entries from the main system log, using more to
display them one screenful at a time (remember: in single-user mode, there’s
no way to scroll back like you can in a terminal window).

sw_vers
  – Check the operating system version. Useful to find out which procedure
to use to start system daemons.


Important files:

/var/db/.AppleSetupDone
  – This file, simply by existing, indicates that the computer has had
basic setup performed. If you delete this file, the Setup Assistant will
run on the next reboot, allowing you to create a new local admin account,
re-setup networking, etc.

/Library/Preferences/SystemConfiguration/
(10.3) or

/var/db/SystemConfiguration/ (10.0 – 10.2)
  – Despite its generic-sounding name, this directory is actually where
most of the computer’s network settings are stored (especially in the file
named “preferences.plist”). Moving or renaming this directory will force the
OS to build a default set of network settings on the next reboot (i.e. one
location named “Automatic”, DHCP on all ethernet & AirPort interfaces, etc).
If your network settings get hosed to the point where the computer won’t boot,
this is the easy way out.

/Library/Logs/ and

/var/log/
  – Where the log files are kept. Generally, logs relating to the core
unix (Darwin) parts of the operating system can be found in /var/log/,
while the Apple-proprietary system components log to /Library/Logs/.
When in doubt, look both places. And check out the main system log,
/var/log/system.log — all sorts of interesting events wind up there.
Note: old logs are often archived in gzip (compressed) format; to read these,
you can do something like:

zcat /var/log/system.log.1.gz | more
decompress the
most recent archived system log, and use more to display it one
screenful at a time.

Getting Out of Single-user mode:

exit
  – Continue the boot process (i.e. go to multi-user mode). Note that
if you’ve made any significant changes (or started any daemons, run
SystemStarter, or anything like that),
it’s safer to reboot instead.

reboot
  – Reboot the computer.

shutdown -h now
  – Shut the computer down.


Misc. Notes:

In single-user mode, the shell behaves a little differently than normal (more
so under earlier versions of OS X). For one thing, the default shell may be
different (zsh under 10.0-10.2, bash under 10.3); unless you’re extremely
familiar with the peculiarities of your regular shell, you probably won’t
notice this (except that the shell prompt’ll be different from normal).

Also, the keyboard may not be fully configured in
single-user mode
, so the arrow keys don’t work for command line editing (or
in text editors, either); but you can use control characters (i.e. hold down
the control key – that’s control, not command – and type a letter) instead:
 ^p – previous line, equivalent to up-arrow

 ^n – next line, equivalent to down-arrow

 ^b – back a character, equivalent to left-arrow

 ^f – forward a character, equivalent to right-arrow

 ^a – back to the beginning of the line

 ^e – forward to the end of the line


UNIX Pipelines And I/O Redirections

Many unix commands take text-like input and/or produce text-like output. It’s
sometimes useful to be able to control where the input comes from and output
goes (via redirection), or even pass the output from one command to another’s
input (via pipes). This can be used to do fairly complex things, for example
the following command will print a list of mispelled words in the file fnord.txt
(it’s sort of a primitive spellchecker):


tr 'A-Z' 'a-z' <fnord.txt | tr -cs 'a-z' '\n' | sort | uniq | comm -23 - /usr/share/dict/words

What this does is pass the contents of fnord.txt to the tr command
to translate uppercase letters to lowercase; the output from that is piped to
another tr command that turns everything except lowercase letters into
line breaks (effectively putting each word on a seperate line); that’s piped to
sort which puts the lines (words) in alphabetical order; that’s piped
to uniq, which gets rid of duplicate words; that’s piped to comm,
which compares the words to the dictionary, and prints whatever’s not in the
dictionary. (Unfortunately, this tends to include plurals, past tense verbs, words
that came into use after 1934… it’s really not a very good spellchecker.)

Most of the times pipes and redirects are used aren’t nearly that complicated.
In fact, there are fairly few idioms that get used over and over. I’ve tried to
illustrate them in the examples below.

Note: pipes and redirects are actually done by the shell interpreter you’re
running. The simple pipes and redirects described here work essentially the same
in all shells, but most shells can also perform other, more obscure, kinds of pipes
and redirects (e.g. redirecting error messages along with/instead of standard
output). For more details, read the man
page for the shell you’re using. BTW, the default shell under OS-X is
tcsh (except in single-user mode, where it’s zsh).

>
– Redirect output from a command to a file on disk. Note: if the
file already exist, it will be erased and overwritten without warning, so be
careful.

Example:

ps -ax >processes.txt
Use the
ps command to get a list of processes
running on the system, and store the output in a file named processes.txt

>>
– Append output from a command to an existing file on disk.

Example:

ps -ax >>processes.txt
Tack the current process list
onto the end of the file processes.txt

<
– Read a command’s input from a disk file, rather than the user. Be careful
not to type “>” by mistake, or you’ll erase the contents of the file
you’re trying to read from.

Example:

niload -d -r / . </var/backups/local.nidump
Use the
niload command to load data from the file /var/backups/local.nidumpinfo
into the current NetInfo domain. Without input redirection, you’d have to type
the NetInfo data into the terminal by hand (and God help you if you got the
syntax slightly wrong…)

|
– Pass the output of one command to another for further processing.

Examples:

ps -ax | grep Finder
Use the
ps command to get a list of processes
running on the system, and pass the list to
grep to search for lines containing
“Finder”. (Usually, it’ll find two: the Finder, and the processes executing
grep Finder.)
lsof | more
Use the lsof command to list all open
files in use on the system, and pass the list to
more to display it one screen at a
time (rather than just spewing the whole thing directly to the terminal).

tee
– Used in the middle of a pipeline, this command allows you to both redirect
output to a file, and pass it to further commands in the pipeline.

Examples:

ps -ax | tee processes.txt | more
Use the
ps command to get a list of processes
running on the system, store it in the file processes.txt, and also pass it to
more to display it one screen at a
time. Note that you could get the same result with the two commands:


ps -ax >processes.txt
more processes.txt


Changing File Permissions Using Unix Commands

The following
information will help you to use Unix commands to change file permissions.
If you have not already done so, you may want to review the document Unix
File Permissions
. Please note that Unix can be a very user-unfriendly
environment. For a start-to-finish tutorial on working in the Unix environment,
please see Unix for Web Designers.

Directions
for:

Single-user
sites

The steps
for correcting permissions errors on single-user sites are as follows:

  1. establish
    a secure connection (using SecureCRT or NiftyTelnet – both are available
    from Software Central) to the server
    which houses your web pages
  2. issue
    the Unix command chmod for any files or directories which need
    to be changed.
Scenario:
User mst3k is getting a permissions error when trying to view his
web site at http://www.people.virginia.edu/~mst3k.

After
logging into blue.unix.virginia.edu with SecureCRT, mst3k issues
the following on the Unix command line:

cd public_html

to go to
the directory where his web page is stored. He then views the permissions
by issuing the command:

ls -l
home.html

and sees
the following output:

-rw-r—–
1 mst3k staff 724 Jan 17 2001 home.html

Permissions
are organized into three major groupings. In this example the permissions
for user are rw-, the permissions for group
are r–, and the permissions for other are .

This file
is not viewable on the web because “other” does not have
read permissions. To correct the problem, mst3k issues the command:

chmod
o+r home.html

To verify
that the command produced the desired results, mst3k again issues the
command:

ls -l
home.html

and sees
the following output:

-rw-r–r–
1 mst3k staff 724 Jan 17 2001 home.html

Provided
that the permissions of the parent directories are correct, the
page should now be viewable on the web.

The following
chart outlines the recommended permission settings for the files and directories
in single-user web sites:

Recommended
Permissions for single-user web sites

File
Permissions

User

Group

Other

read

read

read

write

rw-r–r–

 

Directory
Permissions

User

Group

Other

read

write

execute

execute

execute

rwx–x–x


Group-maintained
sites

The steps
for correcting permissions errors on group-maintained sites are as follows:

  1. establish
    a secure connection (using SecureCRT or NiftyTelnet – both are available
    from Software Central) to the server
    which houses your web pages
  2. Verify
    that the group ownership of your files is correct
  3. issue
    the Unix command chmod for any files or directories which need
    to be changed.
Scenario:
User mst3k is getting a permissions error when trying to edit the
file home.html in the group-maintained site http://www.virginia.edu/somesite

After
logging into minerva.acc.virginia.edu with SecureCRT, mst3k issues
the following on the Unix command line:

cd
/www/doc/somesite

to
go to the directory where the web page is stored. He then views
the permissions by issuing the command:

ls
-l home.html

and
sees the following output:

-rw-r–r–
1 abc2d staff 724 Jan 17 2001 home.html

There
are two problems with this file: (1) The group ownership
is not correct; (2) group write permissions have not been
set.

To
correct this problem, mst3k goes to find the owner of the file –
abc2d. Only the owner of the file may change the group ownership
or permissions.

abc2d
then logs into her account and repeats the steps above. To change
the group ownership, she issues the command:

chgrp
somegrp home.html

where
somegrp is the group that was designated for this web site.
She then adds group write permissions by issuing the command:

chmod
g+w home.html

Finally,
she verifies that the changes were made by issuing the command:

ls
-l home.html

and
sees the following output:

-rw-rw-r–
1 abc2d somegrp 724 Jan 17 2001 home.html

Provided
that mst3k is a member of the group somegrp, he will now be able
to edit the file.

The following
chart outlines the recommended permission settings for the files and directories
in group-maintained web sites:

Recommended
Permissions for group-maintained web sites

File
Permissions

User

Group

Other

read

read

read

write

write

rw-rw-r

 

Directory
Permissions

User

Group

Other

read

read

write

write

execute

execute

execute

rwxrwx–x


Miscellaneous UNIX Commands

man
– display online documentation (“manual pages”) for a command.

Example:

man ls
displays detailed documentation on the ls command

apropos
– list the manual pages relating to a particular keyword.

Example:

apropos file
list manual pages that mention “file” in their
summary line.

find
– Scan a directory structure for files matching certain criteria, and
either print their names or do nearly anything else with them. Similar to
Sherlock, but very unixlike (i.e. terse, cryptic, case sensitive, and extremely
powerful if you understand it well enough).

Examples:

find / -name foo
Search the entire file structure
(including all mounted volumes) for files named exactly “foo”, and print
their paths.

Note: under some conditions (especially involving complex NetInfo networks)
this may search infinitely amongst various network volumes. Use the
-x option to avoid this (see next example).

find -x / -name foo
Search just the boot volume (the
-x prevents it from crossing to other volumes) for files named
exactly “foo”, and print their paths.
find . -name '*foo*'
Search the current directory and
all subdirectories for files whose names contain “foo”. You need to use
the quote-marks in the command, or something incomprehensible will happen.
find . -mtime -2
Search the current directory and
all subdirectories for files modified within the last 2 days. (Note: this
may also find some files with confused modification dates — i.e. files that’re
marked as having been modified in the future. -mtime -0 would find
ONLY the files with future modification dates.
find -x / -user george -print0 | xargs -0 chown eva
Search just
the boot volume for files owned by George, and assign them to Eva instead.
Note: you must be root to do this (otherwise, you’ll get a lot
of error messages).
find ~/Documents -type f -print0 | xargs -0 grep -il birthday
Search all documents (using the grep
command) in my personal Documents folder, and print a list of files containing
the string “birthday” (either upper or lowercase). (The “-type f
is there so it won’t try to search directories, or other non-document-ish things;
-i” tells grep to ignore upper/lowercase distinctions, and
-l” means print the filenames [actually paths], not the matching
contents.)

locate
– Scan a database for files with a given name, and print their names.
Faster than find, but requires that the database be built in advance
(and be up to date).

Notes:

  • In order to see all files, and have permission to build the database,
    the script to build/update the database should run as root.
  • Because the database contains a complete list of files on the disk,
    (including those in private directories), and is readable by anyone, this
    can … have security implications. If it worries you, just don’t build
    the database in the first place.
  • locate’s matching rules can be rather counterintuitive, mainly because
    it searches the entire path, not just the filename, for a match to what
    you tell it to search for. Together with the fact that if your search
    string doesn’t contain any wildcards it searches as though you specified
    wildcards at the beginning and end of the search string, this means it
    may not do exactly what you expected.

Examples:

sudo /usr/libexec/locate.updatedb
Switch to root (see
sudo) and build/update the database.
locate foo
Search the database for files with names
containing “foo”, or in folders with names containing “foo”, and print
their paths.
locate '*.jpg'
Search the database for files with names
ending in “.jpg” (but not in folders ending in “.jpg”), and print their
paths.

open
– Open a file in the GUI (essentially equivalent to double-clicking
the file in the finder).

Examples:

open ~Documents/foo
Open the file “foo” in my Documents
folder.
open -a /Applications/TextEdit.app ~/Documents/foo
Use
the application TextEdit to open “foo” (equivalent to drag-and-dropping foo
on TextEdit).

UNIX Commands: Working with Text Files

By Gordon Davisson

Copyright (c) 2002, Westwind Computing inc.

more and less
– display the contents of a text file, one screenful at a time (hit the
spacebar to get the next screen). Note that this only works well with plain
text files, not Word files, RTF’s, PDF’s, or anything else that contains
formatting information. less also allows you to go backwards
(type “b”) in the file. In either one, type “h” for more detailed help.

Examples:

more /etc/inetd.config
print the inetd.conf file to the
terminal, one screen at a time.
ps -ax | more
use the ps
command to generate a list of processes running on the system, and
pipe them to more to display them
one screen at a time.

grep
– search the contents of a text file, and print lines containing a
given word or pattern.

Examples:

grep telnet /etc/inetd.config
search the inetd.conf file,
and print all lines that contain “telnet”.
grep diskarbitrationd /var/log/system.log
search the main
system log for entries that mention the disk arbitration daemon.
ps -ax | grep netinfod
use the ps
command to generate a list of processes running on the system, then
pipe the list through grep, which will
print only those lines containing “netinfod”. Note: this will list all runing
netinfod processes, and also list the process running grep itself.

pico
– edit the contents of a text file. As with more and
less, this only really works with plain text files. Also, note that
pico doesn’t have menus or command-key (cloverleaf, Apple-key, whatever you
call it) shortcuts, or pay any attention to what you do with the mouse.
Instead, you use the arrow keys to move around, and control-key shortcuts to
do things you’d normally do with the mouse and menus. But, the control-key
shortcuts have no connection to the command-key shortcuts you’re used to;
for example, control-X means quit (and ask if you want to save changes), not
cut. There’s a list of control-key’s at the bottom of the window, and
control-G will give you more extensive help. Note, though, that the F-key
equivalents it lists for some commands don’t work.

vi and emacs
– other text editors provided with the standard OS X installation. They’re
both more powerful than pico, but also a lot harder to figure out
if you aren’t already familiar with them.

tail
– print the last few lines of a text file. This is mainly useful for
examining the last (i.e. most recent) entries in things like log files.

Examples:

tail /var/log/system.log
print the last screenful of
entries from the main system log.
tail -1000 /var/log/system.log | more
print the last 1000
entries from the main system log, using more to
display them one screenful at a time.
tail -f /var/log/system.log
print the last screenful of
entries from the main system log, then “follow” changes to file, printing
new log entries as they’re made.

Text File Format Compatibility:

As simple and straightforward as they may seem, text files still harbor an
opportunity for compatibility problems. Different operating systems have
traditionally used different ways to indicate line endings (aka line breaks).
Mac OS has traditionally used the Carriage Return character (ASCII chcracter
13, aka CR or ^M) to indicate line breaks; unix has traditionally used the
Line Feed character (ASCII 10, aka LF or ^J). Since Mac OS X derives from
both heritages, it winds up using a mix of the two in various contexts. But
most command line utilites only understand (and produce) files with unix-style
breaks.

Just to make things even more fun, there’s actually a third variant: MS-DOS
its successors use a carriage return followed by a line feed to indicate a line
break. Few Macintosh programs will generate such files, but if you need to deal
with a file that came from a PC, you’ll probably want to convert it to a more
native format on the Mac.

Fortunately, it’s fairly easy to convert the formats back and forth on the
command line. Here are some examples of how to transform files back and forth:

tr '\r' '\n' <macfile.txt >unixfile.txt
convert
the Mac-format file macfile.txt to unix format, and save the result as
unixfile.txt. tr is a program that does character substitution,
and in this case it’s simply being used to replace CR (written \r on the
command line) with LF (written \n) throughout the file.
tr '\r' '\n' <macfile.txt | grep fnord
convert the
Mac-format file macfile.txt to unix format, then use
grep to search the file for the word “fnord”.
(Note: since grep doesn’t understand Mac-style
line breaks, searching the file without converting it first would probably
not give useful results.)
tr '\n' '\r' <unixfile.txt >macfile.txt
convert
the unix-format file unixfile.txt to Mac format, and save the result as
macfile.txt.
perl -p -e 's/\r/\n/g' macfile.txt >unixfile.txt
convert
the Mac-format file macfile.txt to unix format, and save the result as
unixfile.txt. This is functionally identical to the first example, but since
perl is actually a very general programming language, it can also do some
other useful things… BTW, he -e means the program will be the next thing
on the command line ('s/\r/\n/g' – perlese for replace all \r’s with
\n’s), and the -p means do this for each line of the file.
perl -pi -e 's/\r/\n/g' textfile.txt
convert the file
textfile.txt from Mac-style (CR) line breaks to unix-style (LF), and replace
the original file with the converted version (that’s what the -i means).
perl -pi -e 's/\r\n?/\n/g' textfile.txt
convert the file
textfile.txt from Mac-style (CR) or PC-style (CRLF) line breaks to
unix-style (LF), and replace the original file.
perl -pi -e 's/\r\n?/\n/g' *.txt
convert all text files
(or rather, files with .txt extensions) in the current directory to unix-style
breaks. Note that any that were already in unix format will not be changed.
perl -pi -e 's/\n/\r/g' textfile.txt
convert the file
textfile.txt from unix-style (LF) line breaks to Mac-style (CR), and replace
the original file.

UNIX Administration Commands

ps
– List the processes running on the system

Examples:

ps
List processes belonging to the current user that
are attached to a terminal (not very useful under OS X)
ps -x
List processes belonging to the current user
whether or not they’re attached to a terminal
ps -ax
List all running processes
ps -aux
List all running processes, with additional
information about their resource useage

top
– List the top CPU-consuming processes running on the
system, along with various other system load statistics. Note: it runs continuously,
updating the stats repeatedly, until you quit it with “q”.

Examples:

top
Display a list of processes, highest-process-id (i.e.
most recent) first, updating once a second
top -us5
Display processes sorted by CPU
useage, updating every 5 seconds

kill
Kill (or send other signals to) a process

Examples:

kill 220
Terminate process #220
kill -9 220
Terminate process #220 with extreme prejudice
kill -HUP 220
Send process #220 a
hangup signal – by
convention, background processes (daemons) treat this as a cue to restart,
and reload their configuration information.

su
Set user. Allows you to
temporarily become another user (root is the default). It’ll ask for that user’s
password. Use the “exit” command to go back to normal.

Note:

  • You must be a member of the “wheel” group to su to root; under OS X 10.2,
    nobody is a member of “wheel” so this is effectively forbidden. Use
    sudo instead.

sudo
Set user and do.
Execute a single command as another user (root is the default). It will ask for
your password. Access is controlled by a configuration file and can be
made quite complex (see the man page). By default, any administrator use sudo
to perform any command as any user.

Examples:

sudo rm /private/var/db/.AppleSetupDone
Become root
just long enough to delete one file.
sudo -u george ls ~george/Documents
Become george and
list the files in his Documents directory.
sudo -s
Start a root shell (similar to su,
except that it asks for your password, rather than the root password, and doesn’t
requires admin access, not wheel membership).

lsbom
List the contents of an
installer’s bom (bill of materials) file. This can be
used to find out what files an installer will add/replace in your system
before running it. It can also be used to find out what files a past install
messed with.

Examples:

lsbom /Volumes/Developer\ Tools/Packages/DevTools.pkg/Contents/Resources/DevTools.bom
>contents.txt
List the files that will be installed by the “DevTools” package,
saving the list in a file named contents.txt.
lsbom /Library/Receipts/Essentials.pkg/Contents/Resources/Essentials.bom | more
List the files that were installed by the “Essentials” package (and
pipe it through
more to prevent overload).

lsof
List open
files on the system. Normally, it only lists files you (or
processes you own) have open; if run as root, it lists all open files.

Examples:

lsof
List all files currently open by me and my processes.
sudo lsof
List all files currently open on the entire system.
sudo lsof -i
List all open network connections on the entire system.
sudo lsof "/Volumes/FW Drive"
List all open files on the “FW Drive” volume; useful for figuring out
why you can’t eject/dismount a disk because something is using it.

ifconfig
Configure network
interfaces (e.g. ethernet ports, AirPort
cards, etc).

Notes:

  • In general, it’s better to adjust the network settings in the Network
    pane of System Preferences. ifconfig sometimes allows a little more
    control/information, but changes made this way will almost never “stick” when
    the computer is rebooted, and may get reset when the network settings change
    (e.g. if the location changes, a port gets connected or disconnected, etc).
  • Changing the network settings requires root access.
  • ifconfig refers to network ports using rather cryptic
    identifiers such as:

    en0
    The first ethernet interface
    (generally, the built-in ethernet port).
    en1, en2, etc
    Additional ethernet interface(s)
    and/or AirPort wireless network card(s), firewire, etc.
    lo0
    The local loopback pseudo-interface, which your computer
    uses to talk to itself. Don’t worry, this is not a sign of schizophrenia, it’s
    just the way unix systems work…

Examples:

ifconfig -a
List the computer’s network ports and their settings.
sudo ifconfig en0 media 100baseTX mediaopt full-duplex
Set the built-in ethernet interface to 100-megabit, full-duplex mode.
Note that this may or may not have any effect, depending on whether the driver
supports this form of configuration-forcing.
sudo ifconfig en0 alias 10.0.0.150 netmask 255.255.255.0
Attach an additional IP address (aka an alias or subinterface) to the
built-in ethernet port. Note that (at least as of OS X 10.2) this is probably
better done by simply adding another port configuration in the Network preference
pane.

diskutil
– Provides various utilities for dealing with Apple’s disk format options
(HFS+, journaling, RAID, etc). Many options require root access.

Examples:

diskutil
Display a list of diskutil’s options.
diskutil info /
List information on the boot volume.
diskutil info /Volumes/Data
List information on a mounted (non-boot) volume named “Data”.
diskutil info /dev/disk0s9
List information on partion #9 of disk #0.
sudo diskutil repairDisk /Volumes/Data
Repair the file structure on the volume “Data” (note: this unmounts the volume
during repair).
sudo diskutil repairPermissions /
Repair the file permissions on the boot volume.
sudo diskutil enableJournal /
Enable HFS+ journaling on the boot volume.
sudo diskutil disableJournal /
Disable HFS+ journaling on the boot volume.