mac

Linux/UNIX/OS X: How to find and combine multiple files

This explains how to use a UNIX-like command line (including Linux and OS X) and the find command to search through a subdirectory and find all of the files with a certain extension, and then combine those all into one file. Surprisingly, this isn't a difficult task. It can be accomplished with one command on the command line:

$ find ./src -name '*.txt' -exec cat '{}' \; > test.txt

The above looks through everything in the ./src directory (including all subdirectories) for any files with the .txt extension. Each file it finds, it adds to test.txt. So at the end of the command's run, all of the text files will be combined together into text.txt. You can use this strategy to easily combine lots of files into one.

Using find, it's easy to customize the command above to do all kinds of things with files. I gave a few examples in an earlier post about the UNIX find command.

OS X: Installing MongoDB and the PHP Mongo Driver

MongoDB is a full-featured object database. Since it is fast, versatile, and schema-less, you can develop a very complex data storage layer without an ORM, and without any tedious coding. For this reason, I have been investigating MongoDB as a storage layer for PHP. Here's how to set up an environment on OS X Snow Leopard.

In this blog we'll do the following:

  • Install MongoDB
  • Add some initial data to MongoDB
  • Install the PHP PECL driver for MongoDB
  • Write a short PHP Script that uses MongoDB
  • Shut down the MongoDB server

OS X: Using curl instead of wget

OS X does not come with wget, a command-line tool for retrieving websites. For a while, I grumbled about this. I knew that curl was installed, but I hadn't ever used curl from the command line. But once I tried it out, I realized that for my needs, curl is just as good as wget... and I don't have to install anything extra to get it.

Here's how to use curl to fetch a remote URL:

$ curl -OL h ttp://spine-health.com/index.php
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 34646    0 34646    0     0  15314      0 --:--:--  0:00:02 --:--:-- 17767

This will download the remote file and store it locally in index.php. If you leave off the -O, it will write the file to standard output (your terminal, usually).

curl: Remote file name has no length!

Fetching from a URL's root can behave differently. If you perform the same command as above, but pointing to the base URL, you will get an error:

$ curl -OL h ttp://spine-health.com/
curl: Remote file name has no length!
curl: try 'curl --help' or 'curl --manual' for more information

What's going on here? The error is not terribly informative on this point. The problem is that curl doesn't know where to write the output file. A better command here is something like this:

$ curl -L h ttp://spine-health.com/ > out.html

This time, the retrieved data will be written to the out.html file.

Like wget, curl has many options. You can read the man page for details, or you can get a quick summary with the usual curl --help command.

Phing: Fixing Output on OS X

Using Phing on a Mac OS X console sometimes has a strange result: While the Phing script runs correctly, the console displays nothing. The reason for this is that the ANSI color codes used by some versions of Phing are not supported by OS X.

The solution is simple. Change the output logger.

$ phing -logger phing.listener.DefaultLogger ftest

This will use a non-colorized logger.

OS X Security Warning on Email Attachments: Getting rid of them

A colleague of mine recently pointed out that when you unzip an archive of scripts received as an email attachment, every time you open a file, you get a warning like this:

“csvimport.inc.php” is a script application which was attached to a mail message. Are you sure you want to open it?

When you have dozens of scripts to look at, clicking through this warning each time is irritating, to say the least. A little examination of the xattr flags on these files, though, revealed the problem:

$ python 
Python 2.5.1 (r251:54863, Feb  6 2009, 19:02:12) 
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import xattr
>>> xattr.listxattr('config.inc.php')
(u'com.apple.quarantine',)
>>> 

There is a flag on such files called 'com.apple.quarantine'. This flag needs to be cleared.

Originally, I was going to write a script to do this, but a quick google obviated that need. Mark Liyanage has done this already. He shows two different ways to clear the flag.

TechStartups: The Demise of Web 2.0

I spent last weekend out in Santa Clara at the Google SOC Mentor Summit. The conference itself was fantastic, but at one point a lightbulb went on in my head, and I sat down and wrote an article. Head on over to Tech Startups and read about The Demise of Web 2.0.
TechStartups: The Demise of Web 2.0TechStartups: The Demise of Web 2.0

Using the UNIX find Command

Today I needed to run a simple script against thousands of identically named files nested in a huge directory structure. What I needed was a quick way to recurse through all of the directories, ignore all of the files I didn't care about, but run a specified command on any files with a particular name.

While this sounds like the sort of thing that will require a couple dozen lines of shell scripting, it can actually be accomplished on a single line with the command find. find has been around for decades, and can be found on almost any UNIX-like file system. It is a simple to used for searching for files within the UNIX directory hierarchy. Honestly, there's not much more to it. Like all good UNIX tools, it does one thing well.

Vim Colorschemes

Here's a small tip for burgeoning Vim (VI Improved) users. Vim supports various colorschemes in its colorized editing mode. If you don't like the default syntax highlighting colorscheme that Vim uses, you can change it easily by running a command like this (within Vim):

:colorscheme koehler

The :colorscheme command takes one argument: the name of the colorscheme. Above, I am setting the syntax highlighting colorscheme to the koehler scheme. At any point, I can change it back to the default by running:

:colorscheme default

Mac: Using the Visor terminal utility

A week ago, I was introduced to Visor, a utility for turning the Mac OS X console into a "Quake-style" console. What does that mean? In a nutshell, it means that the terminal runs not as a normal window, but as a boderless window attached to the top of the screen. Hit a button and the console slides down. Hit the button again (or unfocus from the window) and up it slides. It's a convenient way of keeping a terminal accessible at all times. (And, yes, it supports multiple tabs.)
Visor: "Official" screenshotVisor: "Official" screenshot

Visor is written in Ruby, and is easy to install (though the installation process is a little more, ahem, manual than your typical Mac OS application.

Tip: By default, Visor slides away when the window loses focus. Sometimes, though, it is desirable to keep the visor down for short (or perhaps long) periods while working in another window. To do this, expand visor and then click on the visor terminal icon on the right side of your menu bar. Select "pin" and the visor will stay open. To set it back to its regular behavior, select "unpin".

Thanks to sdboyer for turning me on to Visor.

Mac: Tabbing through all form fields

By default, the Mac OS X behavior for tabbing through form fields is different than that of Windows and Linux. Instead of tabbing through all fields in a form, the default Mac behavior is to skip only between lists, text fields and text areas. Buttons and checkboxes are skipped. However, this behavior is configurable.

To configure OS X to tab through all fields, go to System Settings and navigate to Keyboard and Mouse. From there, click on the Keyboard Shortcuts at the top.

Syndicate content