Tag Archives: command line

Using BBEdit’s shell worksheet to rename files

BBedit’s shell worksheet can be a really handy tool for batch renaming files. I was inspired to give it a shot after seeing a script that handed off a directory listing for editing. Here’s a really trivial example. Let’s say I have a directory containing the following files (and I’ve cd‘d to it in the worksheet):

foo1.txt
foo2.txt
foo3.txt

And let’s say those files are actually Markdown, so I want to change the extension to .md.

First, type ls and execute it with Control-Return to get the listing of those files in your worksheet window. The result looks like the example above.

Next, highlight the listing, and bring up the “Find” dialogue. Make sure that “Grep” and “Selected text only” are selected.

Enter the pattern (.*?)(\.txt), meaning a lazy search of any characters up to a literal .txt, broken into two tokens. The first token is the file name, the second token is the extension. You want to replace that with mv \1\2 \1.md;. The result will look like this:

mv foo1.txt foo1.md;
mv foo2.txt foo2.md;
mv foo3.txt foo3.md;

Select those lines and execute them using Control-Return. If you do another ls, you’ll see your files have been renamed.

This example is really simple, just to get you thinking about the kind of file renaming you can do. Remember that a shell worksheet in BBEdit is a regular editing window. Any snippets, scripts, or text factory you have set up are available to you.

An easier way to kill processes

I’ve moved on from ps aux | grep -i processname; kill PID. The Mac (along with many other Unix-likes) has two handy utilities: pgrep and pkill. pgrep does a case-insensitive search for processes matching the expression you provide, and returns all matching PIDs, one per line. pkill does the same search, but just kills the matching processes instead of returning the PIDs.

A common use case for me is middleman hanging when I change the config.rb. I could kill it using the following:

kill `pgrep middleman`

pkill middleman

One of the benefits of using pkill is that you can run the command interactively. Handy if you end up searching on the scripting language running the process, rather than something a bit more unique.

Quickly adding a link to the iOS Simulator

I was looking for a way to easily add a link to the iOS Simulator in my ~/Applications folder, but every explanation that worked for Mavericks seemed unnecessarily complicated to me. Here’s what I figured out myself:

  1. Open Xcode and launch the simulator as normal
  2. Control-click on the simulator icon in the dock, and select Options → Show in Finder
  3. Open a terminal, and type ln -s
  4. Drag the simulator icon to the terminal window
  5. Drag the destination for the link to the terminal window, and hit Enter

Cleaning formatting from text on the Mac OS X pasteboard

There are a number of apps floating around that can strip rich text formatting from whatever you’ve copied to the pasteboard. I’m not sure why you’d need to buy an app for this. Here are two ways to get plain text.

⌘⌥⇧V – Paste and match style will give you plain text if you’re pasting into plain text. Otherwise you’ll get rich text that matches the formatting applied to wherever you’re pasting the text. I use this one a tonne in spreadsheets where I’m collecting information from web pages.

pbpaste | pbcopy – run this at the command prompt. It just pipes the pasteboard contents back into the pasteboard, but will remove all formatting as a result.

Hiding the WD Smartware virtual CD on a Mac

I really like Western Digital MyBook Essential drives. They’re inexpensive, they last (my 1TB drive is four or five years old, and survived moving between continents), and they’re reasonably quick. They have one drawback: a virtual CD they mount constantly. I’ve hidden the VCD on one of my drives using the WD software, but the other one keeps coming back. You can follow the process I gave for hiding partitions to hide this VCD:

  • First, run diskutil list | grep SmartWare to get your device identifier
  • Second, get your UUID following the process in the other post
  • Lastly, using vifs, add a line to your fstab that looks like this: UUID=9B5B83B0-58E6-3EC1-8472-4B300F6853E5 none hfs ro,noauto (replacing my UUID with your own)