Mac OS X: edit .plist files from CLI

A number of methods are available that involve “tools” either requiring installatins or some form of previous knowledge about the .plist file contents.
As everything we need is already embedded within the OS (tested with 10.8.5), I’ll just go ahead and describe my manual method:

  1. convert the .plist file (or a copy!) from binary1 (default/original format) to the format of your preference (xml1 or json)
  2. plutil -convert xml1 <filename>
  3. edit the file
  4. vi <filename> :)
  5. convert the file back to binary1 format
  6. plutil -convert binary1 <filename>

List all files in a directory, order by date

Nothing original, the Internet is full of good recommendations, but I had to come up with my own answer:

  1. cd to the upper-level directory
  2. run:  find . -type f -print0 | xargs -0 ls -rtl | awk '{print $6 " " $7 " " $8 " " $9}'

Explanation:

  • find recursively looks for regular files starting from the current directory, output is a long uninterrupted list
  • xargs is used to pass the above output to ls which will display in reverse order, by time, in long format
  • awk will select only the relevant fields

Compare hex files in Mac OS X (strings, hexdump)

diff or sdiff built-in utilities make it easy to compare text files:
example file txt1.txt

line #1
line #2
line #3

example file txt2.txt

line #1
line #2
line #4 <<<===this is where difference lies from the above file

Both diff and sdiff will give us a precise indication of which differences exist and where they are:

$ diff txt1.txt txt2.txt 
3c3
< line #3 <===this is the contents of the first argument ("<", left file)
---
> line #4 <===this is the contents of the second argument (">", right file)

$ sdiff txt1.txt txt2.txt
line #1                         line #1
line #2                         line #2
line                       |    line #4 <===files are shown side-by-side
                                            and differences highlighted

Now what would occur with two different binary files is:

$ diff bin1.bin bin2.bin 
Binary files bin1.bin and bin2.bin differ

Evidently that’s not enough for us…
Well, there’s built-in tools that would be useful in highlighting some discrepancies, namely strings and hexdump. Let’s take a look at both of them with some examples.

1. strings command is able to find any “printable” objects in a file:

$ strings bin1.bin 
_DYNAMIC
_GLOBAL_OFFSET_TABLE_
__gmon_start__
_init
...

You might notice this can be a long output, hmmm… What about the following?

$ strings bin1.bin | grep -i version
version 5.0.0.5, build 0

Whereas:

$ strings bin2.bin | grep -i version
version 4.1.0.7, build 0

Of course “version” is just an example but quite relevant IMHO. Up to you to identify a string that would be meaningful for a comparison.

2. even more detailed, hexdump command, as its name implies, can show you the entire contents of the file along with the hex-to-ASCII translation, for instance:

$ hexdump -C bin1.bin 
00000000  7f 45 4c 46 01 01 01 00  00 00 00 00 00 00 00 00  |.ELF............|
00000010  03 00 03 00 01 00 00 00  e0 1d 00 00 34 00 00 00  |............4...|
00000020  b8 88 00 00 00 00 00 00  34 00 20 00 04 00 28 00  |........4. ...(.|
00000030  21 00 1e 00 01 00 00 00  00 00 00 00 00 00 00 00  |!...............|
...

In this case the suggestion is to redirect the output to a text file that can be examined with diff or sdiff, or your favourite tools, later on.

Start an FTP (or TFTP or SFTP) server on Mac OS X (10.7+)

FTP

Since upgrading to Lion (10.7) the option to enable the FTP server has disappeared for, I assume, security reasons.

Nonetheless, an FTP server is still available to the user. Let’s see how it can be started from a CLI one-liner:

$ sudo -s launchctl load -w /System/Library/LaunchDaemons/ftp.plist

You can immediately confirm that by trying:

$ ftp localhost

NOTE: files will be served from the user’s home directory.

To stop the FTP server just issue:

$ sudo -s launchctl unload -w /System/Library/LaunchDaemons/ftp.plist

TFTP

To start/stop a TFTP server replace ftp.plist with tftp.plist.
The source directory in this case will be /private/tftpboot.
NOTE: the files must be readable.

SFTP

As fas as SFTP (S=secure) goes instead, this can be enabled through System Preferences’ Sharing application. It’s not explicitly mentioned but if Remote Login (SSH) is on then the computer’s IP address will also respond to SFTP/SCP requests.

For the tech-savvy, yes, this is better referenced to as FTP over SSH.

Source: http://osxdaily.com/2011/09/29/start-an-ftp-or-sftp-server-in-mac-os-x-lion/

Start an Apache Web Server in Mac OS X Mountain Lion

In OS X Mountain Lion the Web Sharing preference panel has been removed. The Apache web server remains bundled with Mac OS X though, but you’ll need to turn to the command line to enable the web server.

  1. Create and edit the following file /etc/apache2/users/USERNAME.conf (replace “USERNAME” with the actual account)
  2. Its contents should be as follows:
    <Directory "/Users/USERNAME/Sites/">
    Options Indexes Multiviews
    AllowOverride AuthConfig Limit
    Order allow,deny
    Allow from all
    </Directory>
  3. start the actual server: sudo apachectl start
  4. accessing http://127.0.0.1/ will simply display It works!
  5. files stored under /Users/USERNAME/Sites/ will be accessible from http://127.0.0.1/~USERNAME
  6. stop/restart your Apache web server with sudo apachectl stop|restart

Source: http://osxdaily.com/2012/09/02/start-apache-web-server-mac-os-x/

More info here: http://reviews.cnet.com/8301-13727_7-57481978-263/how-to-enable-web-sharing-in-os-x-mountain-lion/

and here: http://coolestguyplanettech.com/downtown/install-and-configure-apache-mysql-php-and-phpmyadmin-osx-108-mountain-lion