Welcome to the private homepage of Stephan Klimek. Mainly this a place to publish articles of my current interests, to host some of my software projects in development and to provide some personal stuff. This site was developed using the excellent dokuwiki framework. For further info you can have a look at the Inprint page and use this Contact form for any feedback or requests.

Wiki Index

Command Line Utilities

IP addresses of local devices

Original sources of informationTo find all IP addresses of devices in your local network:

$ ping 192.168.1.255 -n 255 && arp -a

If you have a recent build of nmap installed, you can use the following to scan the whole subnet:

$ nmap -sP 192.168.1.1/24

Pipe to GUI Applications from stdin

Original sources of informationTry:

echo "<html><body>Moo</body></html>" | \
     open -a /Applications/Safari.app -f

One weird thing I noticed is that it does not seem to work with -b. For example, echo ”<html><body>Moo</body></html>” | open -b com.apple.safari -f is sent to TextEdit instead.

The -f flag can also be used to pipe to *other applications* as well, including other browsers. Unfortunately open saves the pipe output to a temporary file with a .txt extension, which the Mozilla Suite and Firefox browsers always interpret to mean a text/plain content-type.

Directory names with/out trailing slash

Original sources of information Original sources of information Original sources of information/Directory is not the same as /Directory/ for many command line utilities like cp, mv, scp, rsync, etc. The trailing / means “content of the directory” without the directory itself. Without the trailing / the directory is included.

Example:

# Copy all files of Afolder to the home directory
$ cp -R /Somewhere/Afolder/ ~/  
 
# Copy all files of Afolder to a new sub directory 
# of the home directory named AFolder
$ cp -R /Somewhere/Afolder ~/

Index of useful command line tools

ToolDescription
killallkills processes selected by name, as opposed to the selection by pid as done by kill( ). By default, it will send a TERM signal to all processes with a real UID identical to the caller of killall that match the name procname. For example, the command: killall Dock would restart the Dock application
KernelEventAgenthandles one of the core system services (events such as file systems being mounted and unmounted, low disk space, network connections going down, etc.)
asr(Apple Software Restore) efficiently copies disk images and volumes, and can also accurately clone volumes
blessis used to set volume bootability characteristics for Macintoshes. The command can be used to select a folder on a mounted volume to act as the blessed system folder, and optionally update Open Firmware to boot from that volume. It can also be used to format and setup a volume for the first time. Finally, it can be used to query the folder(s) that are blessed on a volume. Try the following (non-destructive) commands: $ sudo bless -verbose -plist -info /
defaultsis used to access (read, write and delete) Mac OS X user defaults from the command line. For example, the following will print out Desktop background settings (including the pathname for the desktop background image, if any):$ defaults read com.apple.desktop Background
disktoolis a command line utility for disk arbitration. It can be used to rename, eject, mount or unmount disks and volumes
diskutilis a utility for managing disks and volumes. It can be used to perform operations such as enabling/disabling HFS+ journaling, verifying and repairing permissions, erasing disks (including optical media), partitioning, creating and managing RAID sets etc. You typically need root access to use this utility
dittocopies files and directories to a destination directory. ditto can be used to “thin” “fat” (multiple-architecture) exectuables. It can also copy files selectively based on the contents of a BOM (“Bill of Materials”). One of the most useful features of ditto is that it can preserve resource fork and HFS meta-data information when copying files
drutilis a command line tool that uses the DiscRecording framework to interact with attached CD/DVD burning devices
fdiskdisplays or changes the DOS partition table found in the bootsector of x86 bootable disks
fs usagepresents an ongoing display of system call usage information pertaining to file system activity. By default this includes all system processes except the running ''fs usage' process, Terminal, telnetd, sshd, rlogind, tcsh, csh and sh
fstatidentifies open files (including sockets)
hdiutiluses the DiskImages framework to manipulate disk image files
installeris the Mac OS X system software and package installer tool
iostatdisplays kernel I/O statistics on terminal, disk and cpu operations
ipconfigcan be used to get the number of network interfaces active (the ifcount argument), and also to retrieve various options associated with these interfaces. For example, “ipconfig getoption en lease_time” prints the DHCP lease time of en if applicable. Finally, ipconfig can also be used to set an interface for BOOTP, DHCP etc
kdumpdisplays the kernel trace files produced with ktrace in human readable format
latencyis used for monitoring scheduling and interrupt latency. The tool can also be used to set real time or timeshare scheduling policies
lockfilecan be used to create one or more (conditional) semaphore files, with the provision of waiting for a specified number of seconds and a specified number of retries
lsbominterprets the contents of binary bom (bill-of-materials) files. bom is a file system used by the Mac OS X installer to determine which files to install, remove, or upgrade
lsoflists information about files opened by processes
lsvfslists known (currently loaded) virtual file systems
malloc_historyinspects a given process and lists the malloc allocations performed by it. It relies on information provided by the standard malloc library when debugging options have been turned on
netstatsymbolically displays the contents of various network-related data structures
objcopyobjcopy is part of binutils that you can download, compile and install. This utility copies the contents of an object file to another, using the GNU BFD (Binary File Descriptor) library to access the object files
objdumpobjdump is part of binutils. It displays information (including disassembly, if required) about one or more object files
openis a command line utility to open a file (or a directory or URL), just as if you had double-clicked the file's icon
osacompilecompiles the given files, or standard input if non are listed, into a single output script
osalangprints information about installed OSA (Open Script Architecture) languages
osascriptexecutes the given script file, or standard input if none is given. Scripts may be plain text or compiled scripts
pagestuffdisplays information about the specified logical pages of a file conforming to the Mach-O executable format
paxis a tool for reading, writing, and listing members of an archive file. It is also used to copy directory hierarchies. pax supports various archive formats such as cpio, bcpio, sv4cpio, sv4crc, tar, and ustar
pbcopyis used to copy standard input to the pasteboard buffer
pbpasteprints the contents of the pasteboard buffer
pdiskis a menu driven program which partitions disks using the standard Apple disk partitioning scheme
plutilcan be used to check the syntax of property list files, or convert a plist file from one format to another
pmsetchanges and reads power management settings such as idle sleep timing, wake on administrative access, automatic restart on power loss, etc
pstatdisplays open file entry, swap space utilization, terminal state, and vnode data structures
sayuses the Speech Synthesis manager to convert input text to audible speech and either play it through the sound output device chosen in System Preferences or save it to an AIFF file
screencapturecaptures the screen (a window selection or a mouse selection) to the clipboard or a file (as PDF)
securityprovides a command line interface to administer Keychains, manipulate keys and certificates, and do most things the Security framework is capable of
setregionis the command line utility for setting the DVD drive's “region”
softwareupdateis a command line utility to perform software updates under Mac OS X
tcpdumpdumps traffic on a network
topdisplays an ongoing sample of system usage statistics (such as cpu utilization, memory usage etc. for each process)
xxdcreates a hex dump of a given file or standard input. It can also convert a hex dump back to its original binary form

Run a program as another user

For example, to run Terminal as user joe, do:

$ su joe -c /Applications/Utilities/Terminal.app/Contents/MacOS/Terminal

textutil

There's a new command line (unix) utility in OS X 0.4 called textutil which will convert document formats known to the Cocoa layer. According to the man page (man textutil), the formats supported are: txt, html, rtf, rtfd, doc, wordml, or webarchive. Most usefully, you can convert rtf, doc and html documents to text format for further processing with other command line tools. The basic syntax is:

$ textutil -convert fmt filename

Where fmt is one of the formats above, and filename is the name of the file you wish to convert. The man page lists many other useful options, including info to display information about a file. This displays the type, size, length (in characters), and an abstract from the contents of the text file. You can also use textutil to change text encodings on text and html files (eg to/from UTF-8, iso8859, etc).

pbcopy, pbpaste

pbcopy and pbpaste allow you to get and set the clipboard from the command line. pbcopy takes the standard input and places it in the specified paste- board. If no pasteboard is specified, the general pasteboard will be used by default. The input is placed in the pasteboard as ASCII data unless it begins with the Encapsulated PostScript (EPS) file header or the Rich Text Format (RTF) file header, in which case it is placed in the pasteboard as one of those data types.

pbpaste removes the data from the pasteboard and writes it to the standard output. It normally looks first for ASCII data in the pasteboard and writes that to the standard output; if no ASCII data is in the pasteboard it looks for Encapsulated PostScript; if no EPS if present it looks for Rich Text. If none of those types is present in the pasteboard, pbpaste produces no output. Both commands support the following options:

OPTIONS -pboard {general | ruler | find | font} specifies which pasteboard to copy to or paste from. If no pasteboard is given, the general pasteboard will be used by default. -Prefer {ascii | rtf | ps} tells pbpaste what type of data to look for in the pasteboard first. As stated above, pbpaste normally looks first for ASCII data; however, by specifying -Prefer ps you can tell pbpaste to look first for Encapsulated PostScript. If you specify -Prefer rtf, pbpaste looks first for Rich Text format. In any case, pbpaste looks for the other formats if the preferred one is not found. So let's try out these commands. Since pbcopy reads from standard input, we'll use the echo command to feed some text to pbcopy.

$ echo 'Fluffy would taste good in a \ cat bisque.' | \
  pbcopy $ echo `pbpaste` Fluffy would taste good in a cat bisque.

There are two things to notice in those lines. In the first line we're using the echo command which simply writes to standard output whatever you give it as arguments. This output is “piped” (think of it as being passed to the next command) via the vertical bar operator to pbcopy. The second line which demonstrates pbpaste uses the back-tick operator which looks like a single quote character, but is actually a back-leaning quote (mostly likely it's the character on the same key as the tilde ~ character on your keyboard, up in the top left corner of your keyboard (on US-keyboards).

The back-tick operator is shorthand syntax for executing a command and routing the output of that command to standard input. In other words, the echo command gets its input from executing pbpaste in the next example I'll copy the contents of a file (assumes a file named “example.php$quot; in current directory):

$ cat example.php | pbcopy

Now the contents of the file is available for pasting in any other window. the cat command concatenates files to standard output. See the next tip for examples of that command. The following (or a variation of it) is something I use all the time. This example uses the unix find command to locate all files with the suffix .html, prints the full path for each, and pipes those paths into a new bbedit document. This requires that you have BBEdit and have installed BBEdit's command line tool. Find all files with .html extension and show paths in bbedit:

  - Grab a web page and copy the html to clipboard
$ find ~/Sites -name '*.html' -print | bbedit
$ curl http://www.google.com/ | pbcopy cat - concatenate files

Display Bonjour infos

Original sources of informationFor example, to view all of the ssh shares on Bonjour-enabled computers, try this in the Terminal:

$ dns-sd -B **ssh.**tcp .

You should receive a list of workstations that are broadcasting ssh services via Bonjour. Don't forget that you need to replace the spaces with hyphens and add a .local prefix when trying to connect. For example, to ssh to Macintosh HD found this way, you need to use ssh Macintosh-HD.local. To view AFP shares (maybe AppleTalk isn't turned on?), try this one:

$ dns-sd -B **afpovertcp.**tcp .

http://www.dns-sd.org/ServiceTypes.html This page lists more of the potential services you might be able to find. And, as always, you can read the man pages for more help (in 0.4, at least).

cat

cat echos one or more files to standard output and is typically used for joining multiple files into one, or processing multiple files with some other utility. For example, to count the total number of lines in three different files, we can use the wc word count command with the -l option to only display line statistics:

$ cat file1  file2 file3 | wc -l 178

Which printed to standard out a total line count for all files combined of 78. Without the -l option, it would have produced three numbers, the line count, word count and byte count.

say

This tool uses the Speech Synthesis manager to convert input text to audible speech and either play it through the sound output device cho- sen in System Preferences or save it to an AIFF file. You pass it a string to be spoken on the command line and the available options allow for choosing the voice to be used, a file to be spoken, or an output file to save the spoken text in AIFF format.

defaults

Defaults allows users to read, write, and delete Mac OS X user defaults from a command-line shell. Mac OS X applications and other programs use the defaults system to record user preferences and other information that must be maintained when the applications aren't running (such as default font for new documents, or the position of an Info panel). Much of this information is accessible through an application's Preferences panel, but some of it isn't, such as the position of the Info panel. You can access this information with defaults

host

host is a simple utility for performing DNS lookups. It is normally used to convert names to IP addresses and vice versa. When no arguments or options are given, host prints a short summary of its command line arguments and options. Example:

$ host www.apple.com

osascript, osacompile

osascript executes the given script file, or standard input if none is given. Scripts may be plain text or compiled scripts. osascript was designed for use with AppleScript, but will work with any Open Scripting Architecture (OSA) language. To get a list of the OSA languages installed on your system, use osalang( ). For documentation on Apple- Script itself, see http://www.apple.com/applescript.

open

The open command opens a file (or a directory or URL), just as if you had double-clicked the file's icon. If no application name is specified, the default application as determined via LaunchServices is used to open the specified files. If the file is in the form of a URL, the file will be opened as a URL. You can specify one or more file names (or pathnames), which are interpreted relative to the shell or Terminal window's current working directory. Examples: For example, the following command would open all Word files in the current working directory:

open *.docOpen all word documents in the current directory
open .Open a Finder window on the current directory
open ~Open a Finder window for your home directory
open /ApplicationsOpen a Finder window for the Applications directory
open http://www.google.com/Open a URL
open -a /Applications/TextEdit.appOpen a file named “foo.txt” with SimpleText
wiki/tools/commandline_utilities.txt · Last modified: 2006-12-05 22:03 by admin
init24.de = chi`s home Mac OS X Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Valid CSS Valid XHTML 1.0 Recent changes RSS feed