Tuesday, 5 February 2013

Selectively and recursively copy all files of a single file type in OS X and Linux

Problem:

In OS X or Linux, how do I selectively copy only one type of file to a folder? For instance, how can I recursively copy all .mp3 files from a folder with many sub-folders into a single folder?

Solution:

Note: this solution assumes you have basic knowledge of using the Terminal and know how to avoid destructive commands.

To recursively check the paths of all files for instance all .mp3 in a folder and all its sub-folders, type the following into the Terminal (replacing the path and file extension to fit your need):

find /path/to/files/to/search -iname '*.mp3'

If everything looks alright, to copy the files use the following command (replacing the paths and the extension to fit your need - also note that this is a single line/command, but it's long so it may wrap to more than one line in your browser):

find /path/to/files/to/search -iname '*.mp3' -exec cp {} /path/to/destination/ ./mp3 \;

Important: make sure that the / is at the end of the destination path! (It will all be copied as a single file otherwise as it'll think that your destination is a file name and not a folder.)

Sunday, 3 February 2013

Passing a named associative array via URL

Problem:

How do I manually pass an associative array via URL?

Solution:

Let's say we have an associative array named 'arr_args' with the key-value pairs 'key1'=>'value1', 'key2'=>'value2', i.e. arr_args = [key1=>value1, key2=>value2]. Let's also assume we have a sample script called 'sample.php' that will be receiving this associative array.

One way that you can pass this into a URL as the following:

sample.php?arr_args[key1]=value1&arr_args[key2]=value2