Monthly Archives: March 2007

The Power of the ps Command

I have been using the Unix ‘ps‘ command for years to view my system processes. For some reason, I always forgot which command line switch to use. This morning, when Quicksilver locked up and my system is currently backed up so my MacBook ran like molasses in the winter. Not able to fire up Activity Monitor, I turned to my terminal and issue the following command:

ps -aux | fgrep -i quicksilver

To my disappoinment, the command combo did not return a single line. Simply speak, the ps command gave me a list of processes; then, the fgrep command picked out just the line for the Quicksilver process. So, what went wrong here? The culprit was in the flags I used. My terminal is limited to a traditional 80-column screen, but the -u flag displayed the list in long format and truncated part of the processes’ names. As the result, the name Quicksilver did not show up in the list at all. Clearly, this is a case where I incorrectly used the flags, so I made a point of learning a couple of flags from the ps(1) man page to make it right. After a couple of minutes, I succeeded with the following command:

ps -cx | fgrep -i quicksilver

The new ps flags made a big difference. Finally, I was able to locate the process ID (pid) of Quicksilver and kill it off. Let me explain some of the ps flags.

The -c flag

This flag tells ps to display only the name of the process instead of the full (lengthy) pathnames. It is the key to prevent ps from clipping text beyond the 80th column limit.

The -x flag

This flag tells ps to display all the processes that do not associate with any terminal. My translation: almost all the processes that I care about. Without it, ps only displays a handful of processes, not very useful.

The -o flag

This flag tells ps which column to display. The default output may show more than what I care to know. By using the -o flag, I am able to eliminate irrelevant columns. For example, to display just the process ID and the process name, I issue the following command:

ps -cx -o pid,command | sort -f -k 2

The -k 2 flag of the sort command express our wish to sort the second column (the process name). Meanwhile, the -f flag tells sort to perform case-insensitive sorting. For a list of column names, consult the man page for ps(1).

In summary, flags make a big different on how ps (and other commands) perform. Learn to use them correctly and you have a powerful tool in hand. Otherwise, your command line journey will be full of grievances.

Locate Files with Unix ‘find’ Command

I have been working with Unix for some years and have managed to learn a little bit of the find command, but I have always forget the syntax. That is why I am writing these lines to serve as a reminder as well as help for beginners.

Example 1: List all Python .pyc files in my current directory, including sub directories:

find . -name “*.pyc” -print


  1. The wildcards after the -name flag must be wrapped in double quotes and
  2. Users who run BSD systems (including Mac OS X) can omit the -print flag

Example 2: Remove all .tmp files from my USB key (which is mounted at /Volumes/hai_usbkey), including those in sub directories:

find /Volumes/hai_usbkey -name ‘*.tmp’ -delete


  1. We can wrap wildcards with single quotes
  2. The -delete flag tells the find command to remove the files. Please use this command with care since you cannot undo deletion

Copy Full Paths to Clipboard with Automator

Have you ever selected a file and wanted to access its full path? I ran into the same situation a couple of  times and decided to create a solution for it.  This solution involves only two “line” of automator’s actions.

To create the solution, I start automator, select “Finder” from the Library column, then drag the “Get Selected Finder Items” action from the Action column to the work area (the third column). I then select “System” from the library, then drag the “Copy to Clipboard” action to the work area, just below the first action. At this point, my workflow looks like this:

 To save the workflow, I select the “File” menu, then “Save As Plug-In…” menu item. Next, I give my plug in a name, such as “Copy Full Path”, choose “Finder” from the “Plug-in for” drop-down and click Save. Now it is time to test out my solution. From the Finder, I select one or more files or folders, the Ctrl+Click (or right-click) on one of them, select my just-saved workflow from the Automator menu and watch the menu bar to see if the workflow is running. Upon finishing, the clipboard now stores the full paths for those Finder items, one per line. I can check by pasting the contents of the clipboard into a text document.