Posts tagged terminal

HandBrakeCLI –start-at and –stop-at in

Icon from the Gnome desktop project

When trying to use HandBrakeCLI to take a clip from movie, I couldn't figure out why it was ignoring the durations in seconds I was defining with --start-at and --stop-at.

Turns out, you need to append the word "duration", "frame" or "pts" before each value. For example, to create a 20 second clip starting at the 1 minute mark:

HandBrakeCLI […] --startAt duration:60 --stop-at duration:20

Amazing what one can learn if one reads the manual page before wasting half an hour figuring out what's going wrong!


Caution: filename not matched unzip error

Torchlight ZIP icon

Quick tip I picked up this afternoon. If you try to extract a series of archives with unrar or unzip and you get this error:

% unzip *zipcaution: filename not matched

… you can get around it by escaping the asterisk.

% unzip *zip

I wonder why it works like this when virtually nothing else does?


Few quick Mac Terminal TrueCrypt tips

TrueCrypt with filled in Volume combo box

For those of you like me who use TrueCrypt on Mac OS X, a few more quick tips I’ve picked up.

Use encrypted volumes from the Terminal

Assuming /usr/local/bin is in your $PATH and TrueCrypt is installed in /Applications:

% cd /Applications/TrueCrypt.app/Contents/MacOS
# ln -s TrueCrypt /usr/local/bin/tc

Now you can mount and unmount volumes without fuss:

% tc [volume]
% tc -d [volume]

I suppose instead you could create an alias in your shell config file for tc to point to the full TrueCrypt application path, but the above will work from any user account.

Launch TrueCrypt GUI from the Terminal

This will launch the TrueCrypt graphical application with the complete path of the volume you want to mount conveniently entered into Volume combo box. You'll still need to choose a volume slot and click the Mount button.

% open -a TrueCrypt [volume]

Explain yourself

Unlike Windows and most other *nix systems that spray required files all over the place, applications in Mac OS X are generally distributed in self contained packages which makes them easy to install and run from the Finder. From the Terminal, the true executables you’re interested in are located in the application’s ./Contents/MacOS/ folder, such as TrueCrypt.


Friday night shell adventure

Icon from the Tango Desktop project

Here's something to try if you find yourself at home on a Friday night and you want some excitement. For legal purposes, I must state you should not attempt this.

Open a Terminal or shell in Mac OS X, FreeBSD, Linux, OpenSolaris or your other *nix of choice and navigate to the root directory by entering the following commands:

% cd /
% echo "Grilled cheese sandwiches with pickle"

Now keeping in mind that root is the highest you can navigate to in a file system, attempt to navigate one directory further by entering this command while smacking yourself repeatedly on the head with a breadboard:

% cd ..

You should see a number of stars flashing across your eyes before you head hits the table. Last time I tried I saw William H. Macy and Alan Alda. Brilliant!


Partially recovering damaged tar files in Mac OS X

Archive Utility having trouble with a malformed/damaged tar file
Archive Utility having trouble with a malformed/damaged tar file

Okay enough political nonsense, back to writing about technology and software!

Unlike zip or proprietary rar files, in true "do one thing and do it well" Unix style, most Unix-like file compressors (such as bzip2, rzip or gzip) only compress one file at a time. An intermediate container format is needed if multiple files in one compressed archive are needed, the most common of which is the Tape Archive file/tarball or tar.

Archive Utility

Mac OS X's Archive Utility (which you invoke from the Finder) has native support for tar, but I'm surprised by the number of times I see the error message above after double clicking and attempting to open one. Usually this is a result of a malformed or broken tar file, but the nature of tar files is that generally some files can be salvaged. The Finder refuses to do this, which worries me. How many GUI proficient Mac users throw away tar files that they could salvage at least some files out of?

The solution is a quick trip to the Terminal and using the cd command (same way as DOS) to navigate to the folder where the tar file you're having trouble with is located. Then enter:

% tar xvf [FILENAME].tar

In a nutshell, xvf tells tar to verbosely display files as they're extracted. In this circumstance this is very useful!

You'll see a stream of filenames fill up your window, followed by a message similar to the following if you're tar file was corrupted.

[…]tar: End of archive volume 1 reached
[FILENAME].tar: Unexpected EOF on archive file

While you some of your files may have been extracted, clearly in this case you've received a poorly formed tar file. If possible, you should attempt to download it again from where you got it, or if you made it yourself try to find a backup.


Ifconfig versus ipconfig versus ifconfig

This has happened to me so many times I thought I'd post a clarification here to help myself remember, while passing it off as a legitimate blog post topic. Clever yes? Don't answer that.

Having moved to Unix-like systems from Windows when I finished high school I'm used to firing up my shell and entering ifconfig with either the -a option or by specifying a specific network interface such as eth0 to find out DHCP assigned IP addresses, subnet masks, grilled cheese sandwiches and the like.

ipconfig on FreeBSD
Xfce Terminal on FreeBSD with ThinIce theme

As shown below, on Windows the command is deceptively similar in all but one letter: ipconfig. As with the classic DOS goodness Windows replaced the commands also accept flags with forward-slashes instead of short dashes.

ipconfig on FreeBSD
Command Prompt on XP with the least ugly Windows theme

ASIDE: According to the completely infallible Wikipedia, ifconfig sloppily stands for "interface configurator", whereas ipconfig stands for "internet protocol configuration". Don’t you just love unambiguous commands?

It's interesting how our minds change isn't it? I started using our DOS desktop back before I even started primary school and I got so used to the commands they became second nature. Since moving to the wonderful world of FreeBSD and cross pollinating my knowledge on similar GNU/Linux, NetBSD, Mac OS X and OpenSolaris machines, my DOS knowledge hasn't been forgotten, but it's become something I have to "think" to use. If I had a cent for every time I attempted to type and use ls and top on Windows machines now, I'd have at least enough cash for a grilled cheese sandwich with at least avocado and onions.

Mac OS X Leopard Terminal.app
Mac OS X Leopard Terminal.app

Two grilled cheese sandwich references in one post, three including this sentence. I'm on a roll. Wait I'm not discussing eating a "roll", it's a "grilled cheese sandwich". Wait, that's four times. Grilled cheese sandwich.


Viewing images from the Terminal in Mac OS X

Viewing images from the Terminal in Mac OS X
You can pass multiple images to the Preview app, very nice!

The more I learn and love FreeBSD the more I've been using the Terminal application on my Macs, to the point now where I'm using it more than any other application except perhaps a web browser: for file file compression and management the command line is so much quicker and simpler. As a result I've been trying to modify my workflow to allow me to work inside the Terminal even more, and I thought I'd share some tips I've picked up here.

One such example is viewing images and other media from the Terminal. Just as you probably use the bundled Preview.app to do this when you're in the Finder or from the OPENSTEP days, you can also call it from the Terminal to do the same thing. For example, to load a picture called Geass.jpg:

/Applications/Preview.app/Contents/MacOS/Preview_Geass.jpg

And voila, Preview opens showing you the image you told it to load… from the Terminal! You can pass multiple images seperated with spaces, you can even mix in a few PDFs into your list. The scripting potential is just making me quiver with excitement!

A Mac OS X package

Why didn't I just call the Preview.app application itself? Apps in Mac OS X are actually neatly contained in bundles (a special kind of folder) alongside other resources such as help files. The executable you want to call from the Terminal therefore is the program itself contained in the bundle, not the bundle itself.

Unfortunately there's bad news. As a guy learning UNIX I thought I'd be able to just generate a symbolic link to the executable so I could use a much shorter command in a manner line this:

% sudo ln -s \
/Applications/Preview.app/Contents/MacOS/Preview \
/usr/local/bin/preview

Alas when I attempted to use the new command, I was given a plist error.

% preview_geass.jpg
2009-01-03 02:28:47.810 preview[1470:807] No Info.plist file in application bundle or no NSPrincipalClass in the Info.plist file, exiting

It seems for now you can just enter the previous full command using [TAB] auto-completion to make it easier, but as far as I know there’s no way to shorten it. Writing a tiny shell script to interface with the app generated an identical response:

% less preview.sh
==> #!/bin/sh/Applications/Preview.app/Contents/MacOS/Preview $_

There's probably a foolproof, ridiculously simple solution to this that I'm just not seeing right now. I'll keep you posted if I learn anything else.


How to forcibly eject drives in Mac OS X

This device could not be ejected
But Finder… I don't have any other applications open!

One of the things I can appreciate about learning how to use FreeBSD is that a lot of the knowledge can can also be used to work more productively in Mac OS X too because the commands are either identical or very similar. In many ways OS X and BSD are more similar than BSD and GNU/Linux.

One such function that Mac OS X shares with BSD is the umount command (Darwin man page, FreeBSD man page) which you can use to unmount drives that the Finder refuses to because it claims the drive is in use. Once you unmount a drive, you can safely unplug it.

To unmount a drive so you can eject it when the Finder refuses, launch Terminal.app from the /Applications/Utilities/ folder, then enter the following command including the quotation marks. Replace [DRIVE LABEL] with the name of the drive as you see it on your desktop including any spaces.

% umount "/Volumes/[DRIVE LABEL]"

If umount also refuses to eject the drive, there's a pretty good chance there still is an application using a resource on the drive. If you're positive you have no open files open on that drive and you're in a hurry, you can forcibly eject by using the -f option.

% umount -f "/Volumes/[NAME OF DRIVE]"

Take care when forcibly unmounting volumes!

If the Finder and umount refuse to unmount and eject a drive, they generally have a good reason. Attempt to close all open applications that could feasibly be using any data on the drive the attempt ejecting first.

You should only forcibly unmount a volume as an absolute last resort.

Troubleshooting

If you attempt to use umount and you get a "not currently mounted" error, then the name that Finder shows for the drive might not be the same name Mac OS X mounted it with. To check your drive volume names, fire up the Terminal again, then enter ls /Volumes/

Using the Terminal to display a list of mounted drives


Thinning universal binaries with ditto

In November of 2006 I uploaded a post called A Closer Look At Apple’s Universal Binaries where I tried to describe what UB's are and how to use the lipo command in the Terminal to remove unnecessary code. Since then I've learned a bit more, and have found a slightly easier way to do it.

Previous post in a nutshell: If you've used Mac computers at all since 2005 you're probably aware of Universal binaries, the fancy name Apple gave to applications that have native code for PowerPC and Intel processors. While they really simplify distribution, they store code on your machine you don't actually need.

Fortunately in Tiger Apple bundled the ditto (and lipo) utility which you can use to create a thin version of a universal binary that only contains code for your processor.

TAKE NOTE! Some older applications that are only compiled for PowerPC CPUs require shared libraries or resources from other applications, some of which may have been updated as Universal. Therefore if you start deleting PowerPC code from them, you may start braking things. If you're not sure, always keep the original universal binary just in case you need to restore it!

ditto --arch i386 FooBar.app ThinFooBar.app
ditto --arch ppc FooBar.app ThinFooBar.app

The first line thins down FooBar.app to include only i386 code, the second preserves only PowerPC code.

Shakugan no Shana
The use of an unnecessarily long sword to spit universal binaries is not recommended… unless you're a flame haze… I'm sorry, but I can't stand dry weblog posts that don't have pictures. The thought of weblog posts without pictures keep me awake at night, as I'm sure it does for you too.

Given my current obsession with tables, below is a selection of my favourite Mac open source applications I thinned down on my MacBook Pro to compare the difference between their universal and thin binaries:

Application Version Fat universal Thin i386 % original % saved ↓
VLC VLC 0.8.6c 75.2 MiB 43.1 MiB 57.31 42.69
Camino 1.5.1int 53.7 MiB 34.2 MiB 63.69 36.31
Inkscape Inkscape 0.45.1 84.1 MiB 56.4 MiB 67.06 32.94
iTerm iTerm 0.9.5.x 4.1 MiB 3.2 MiB 78.05 21.95
Gimpshop Gimpshop 2.2.11 191.0 MiB 149.3 MiB 78.17 21.83
Smultron Smultron 3.1 10.1 MiB 9.4 MiB 93.06 6.94

And here's a similar table looking at bundled Apple applications:

Application Version Fat universal Thin i386 % original % saved ↓
Safari Safari 3.0b3 6.5 MiB 5.1 MiB 78.46 21.54
iTunes iTunes 7.4.1 113.0 MiB 98.8 MiB 87.43 12.57
Terminal Terminal 1.5 5.0 MiB 4.6 MiB 92.00 8.00
TextEdit 1.4 2.2 MiB 2.1 MiB 95.45 4.55

Some pretty interesting results, the most noticeable of which I would think is that none of the applications even approached a 50% reduction in file size by removing half their compiled instructions. This is due to applications having shared resources such as images, text files and whatnot that are used by both the PPC and Intel code.

In this case, we can see that VLC, Camino and Safari had a sizable amount of specialised code, whereas the bulk of the TextEdit and Smultron applications consisted of shared resources. We can infer just by looking at these results that rendering video and webpages require more processor specific instructions compared to, say, a text editor.

Plus it gave me the chance to show some Mac icons. I use KDE on FreeBSD and NetBSD and have used all the flavours of Windows at some point, and the Mac is still the prettiest ;).


UFRaw on Mac OS X to open RAW, RAF files

One of the features I love about my FinePix S9600 (yes, a prosumer one Julee, rrrrr) is the ability to save images in an uncompressed RAW file format. Only catch is the RAW format FujiFilm cameras exports is RAF, which my beloved open source photo editing GIMPShop can't open, nor can the default Mac OS X Preview. Adobe Photoshop can open them, but the GIMP does everything else I ever used Photoshop for, and it's a lot cheaper (read: free!).

Fortunately as usual the open source world has come to the rescue in the form of UFRaw, a GTK+ based RAW image manipulator and exporter.

Assuming you already have MacPorts installed already, fire up a Terminal and type in sudo port -v install ufraw. Go grab a cup of coffee or something while you're waiting, it will take a while.

ufraw_term.png

Because this is an X11 app and not a native Mac Aqua one you need to start it with X11 (obviously). So once the MacPorts install has finished, fire up X11 in /Applications/Utilities.

At this point you could just open an X11 Terminal and start it, but I like having a neat menu item I can access from the X11 icon in the Dock. So go to the X11 Application menu and click Customize Menu…, click Add and fill in the command as /opt/local/bin/ufraw (assuming you're using the MacPorts defaults):

ufraw_x11.png

Now you can open X11 and click UFRaw in the Applications menu whenever you need it ^^. It has a very nice set of features, or optionally you can just export to JPEG if you want as well.

Here's UFRaw running on my MacBook Pro with a crappy RAF image I took outside my window this evening of an urban Singapore sunset:

Screenshot of UFRaw on Mac OS X