Animation of “New Folder with Selection” Finder Function

In OS X’s Finder, if you select some things then right-click you have an option called “New Folder with Selection”. ilikeitverymuch. So a while back, I though it would be alsonice to have something analogous in the command shell. I’ve been using just such a command for about six months now and let me assure you that it is very alsonice.

What does it do, under the hood?

$ mkdir somefolder # if it doesn't already exist
$ mv stuff things yetmore somefolder/
$ cd somefolder # optional step

Here are some example uses:

Examples

$ ls
LASTs/      node03.du   node06.du   node09.du   node12.du
node01.du   node04.du   node07.du   node10.du   node13.du
node02.du   node05.du   node08.du   node11.du   node14.du
$ collect DUs *.du
$ ls
LASTs/ DUs/
$ collect . DUs/*; rmdir DUs
$ ls
LASTs/      node03.du   node06.du   node09.du   node12.du
node01.du   node04.du   node07.du   node10.du   node13.du
node02.du   node05.du   node08.du   node11.du   node14.du

The ‘collectd’ command is identical to collect, except it also does pushd on the collection directory.

$ pwd
~/datafiles
$ ls
LASTs/      node03.du   node06.du   node09.du   node12.du
node01.du   node04.du   node07.du   node10.du   node13.du
node02.du   node05.du   node08.du   node11.du   node14.du
$ collectd DUs *.du
~datafiles/DUs ~datafiles
$ ls
node01.du   node04.du   node07.du   node10.du   node13.du
node02.du   node05.du   node08.du   node11.du   node14.du
node03.du   node06.du   node09.du   node12.du    
$ pwd
~/datafiles/DUs
$

The collectd command probably needs renaming because it isn’t a server and it sounds like one. I probably won’t get around to renaming it.

This last example uses collect all-fancy-like in loops to organize files by basename:

$ ls
node01.du   node04.du   node07.du   node10.du   node13.du
node01.last node04.last node07.last node10.last node13.last
node02.du   node05.du   node08.du   node11.du   node14.du
node02.last node05.last node08.last node11.last node14.last
node03.du   node06.du   node09.du   node12.du
node03.last node06.last node09.last node12.last
$ for file in *.du
> do nodename=`basename $file '.du'`; collect $nodename $nodename*
> done
$ ls
node01/ node03/ node05/ node07/ node09/ node11/ node13/
node02/ node04/ node06/ node08/ node10/ node12/ node14/
$

Get it

You can find the dead-simple code for collect and collectd on github.