Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Capture commands that launched processes UNIX
If you know the name of a running command line process, but don't know the precise command line with which it was invoked, you can run a command such as this one to return the complete command:
ps -axwwo command | grep lame | grep -v grep > ~/Desktop/lame_command.txt
This example would save a file to your desktop containing the commands that initiated running processes which included the term lame, for example.
  Post a comment  •  Comments (7)  
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[5,400 views]  View Printable Version
A shell script to empty older items from the trash UNIX
After finally getting tired of remembering to "Empty Trash" at regular intervals, I decided to write a script (36KB download; source code on macosxhints) that automatically cleans files out of the trash after they've been there for seven days. It will even use sudo to clean out things that I don't have permission to erase -- if that option is available. There are many options to choose from, just use --help to see them all.

This script is designed to be very efficient to run, so I run it hourly. There is not (yet) any provision for maintaining the Trash at a particular size. I recommend adding a line to your cron file like this:
@hourly python /your/path/to/dirscan.py
This will clean things out after a week, and checks each hour for new items to remove. It pays attention only to top-level entries within .Trash, so any directories you put in the trash will be deleted automatically.

[robg adds: The code download link is from the author's site, and will be the best place to grab the latest version of the script. I mirrored the source as of today on macosxhints.com, just in case the parent site goes away at some point. Note that I haven't tested this one. Finally, for those interested in a size-limited trash can, this hint and the associated comments offer a few solutions.]
  Post a comment  •  Comments (18)  
  • Currently 2.00 / 5
  You rated: 3 / 5 (6 votes cast)
 
[14,648 views]  View Printable Version
Burn a playable DVD from a VIDEO_TS folder UNIX
After searching the forums and trying various things, I still couldn't find a quick, reliable, free method of burning a VIDEO_TS folder to a pure UDF DVD, so that it would play in regular DVD players, as well trigger DVD Player to start up automatically. Anyway, as often is the case, Terminal had the answers. Just type in this command and change the paths to suit:
hdiutil makehybrid -udf -udf-volume-name DVD_NAME \
-o MY_DVD.iso /path/to/VIDEO_TS/parent/folder
Make sure that /path/to/VIDEO_TS/parent/folder is the path to the folder containing the VIDEO_TS folder, not the VIDEO_TS folder itself. Once the .iso file has been created, drag this to Disk Utility and hit the Burn button.
  Post a comment  •  Comments (21)  
  • Currently 4.39 / 5
  You rated: 3 / 5 (23 votes cast)
 
[194,443 views]  View Printable Version
An AppleScript to spawn multiple Terminal windows UNIX
The first thing I do when I start to work is launch a slew of Terminal windows to log into various servers and/or interact with repositories and local files. I quickly found that is was helpful to color code them so, for instance, if I want to grab the one logged into server X, I can see them in Exposť and be able to choose the window I want by remembering its background color -- even without being able to read the characters. So I wrote a little AppleScript to automate the process; it can likely be improved as it's the first AppleScript I've written:

set RGBGreen to {0, 10000, 0} as RGB color
set RGBRed to {10000, 0, 0} as RGB color
set RGBBlue to {0, 0, 10000} as RGB color
set RGBBlack to {0, 0, 0} as RGB color
set RGBWhite to {65535, 65535, 65535} as RGB color

set RGBcolors to {RGBGreen, RGBRed, RGBBlue, RGBBlack}

repeat with curColor in RGBcolors
  tell application "Terminal"
    activate
    with timeout of 1800 seconds
      do script with command "pwd"
      tell window 1
        set background color to curColor
        set cursor color to RGBGreen
        if curColor = {65535, 65535, 65535} then
          set normal text color to RGBGreen
        else
          set normal text color to RGBWhite
        end if
        set bold text color to "red"
        set title displays shell path to true
        set title displays window size to true
        set title displays device name to true
        set title displays file name to true
        set number of columns to 120
        set number of rows to 40
      end tell
    end timeout
  end tell
end repeat

Obviously colors, the number of windows and the like are easily tweakable.

[robg adds: I haven't tested this one...]
  Post a comment  •  Comments (8)  
  • Currently 1.50 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[17,293 views]  View Printable Version
A Unix shell script to work with Growl UNIX
I have written a Unix shell script to call Growl -- here's the source. My script is named growl, and I keep a copy in $HOME/bin. I generally use my Growl script to notify me when a long-running Unix script finishes. If you are imaginative, you could have a cron job that calls Growl when disk space is low:
#!/bin/sh
df -m / | awk '
    /Filesystem/ { next }
    200 > $4 { system("growl root only has " $4 "MB available") }
'
Or if a critical system has gone off-line:
#!/bin/sh
if ! ping -c 1 -t 10 critical.system.com; then
    growl critical.system.com is off-line
fi
read more (115 words)   Post a comment  •  Comments (12)  
  • Currently 4.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (5 votes cast)
 
[32,425 views]  View Printable Version
Compile *nix programs faster multi-core Macs UNIX
If you have a multicore processor in your Mac, then instead of doing the traditional compile and install steps for a *nix program...
$ ./configure
$ make
$ make install
...use this version instead:
$ ./configure
$ make -j n
$ make install
The n in the second line is the number of jobs you want make to start. So to reduce compile time, replace n with the total number of cores in your Mac.

[robg adds: At some point, I'd like to test this with a large compile, but I haven't had the time as of yet. If you've done any testing on compile time reductions versus cores used, please post your results in the comments. I think this should work on multi-core G5s and all the Intel Macs...]
  Post a comment  •  Comments (20)  
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[19,383 views]  View Printable Version
A script to create multi-DVD-spanning backups UNIX
This hint is based on this previous hint, and alluded to in this comment to that hint. I went ahead and created a DVD-spanning script using tar. Copy and paste this code into your favorite pure text editor, and save it as tar2dvd.sh. In Terminal, make it executable (chmod 755 tar2dvd.sh), and store it somewhere on your path for easy access.

To use it, just type tar2dvd.sh folder_to_backup in Terminal. For example, you could do this:
$ cd ~/Documents
$ tar2dvd.sh MyBigFolder
When the processing is done, you'll find a new folder called MyBigFolder.backup, which will contain numbered subdirectories, one per DVD.

[robg adds: This worked well in my testing, even on folders with spaces in their names. Note that if you use Apple's Backup app, you'll probably want to change the line that reads TARDIR=$BACKUPDIR.backup -- the .backup extension will make it appear as though the end result is a Backup package bundle. I changed the extension to simply .bkup, and then the end result is a folder, as expected.]
  Post a comment  •  Comments (10)  
  • Currently 2.80 / 5
  You rated: 3 / 5 (5 votes cast)
 
[15,641 views]  View Printable Version
grep faster and more effectively than grep, using grep UNIX
I see many hints here on Mac OS X Hints that use the grep utility to filter text. However, many of these hints use grep to search for a plain string, rather than a regular expression. There's nothing wrong with that goal, but using grep for that is inefficient (you're invoking the full power of the regex engine just for a plain string), and requires that you escape any regex characters in the pattern:

 grep 'Price: $' partslist.txt
 # Incorrect: Only matches "Price: " at the end of a line

 grep 'Price: $' partslist.txt
 # Correct: Matches "Price: " followed by a dollar sign
There's a better way. grep also exists under the name fgrep, which searches for a fixed (hence the 'f') string -- that is, a plain string; no characters are special. You can search for any string this way, with no escaping needed (see note below):
 fgrep 'Price: $' partslist.txt 
 # Correct: Matches "Price: " followed by a dollar sign
You can also do this with grep -F, but fgrep is shorter. fgrep and grep are the same program, just with different behavior, so all the other options (-n, -o, -H, etc.) are supported by both.

Note: Of course, you may need to escape quotes or backslashes according to the rules of your shell, but you would have to do this anyway. My point is that fgrep does not require quoting where grep itself would.
  Post a comment  •  Comments (12)  
  • Currently 1.60 / 5
  You rated: 1 / 5 (5 votes cast)
 
[20,058 views]  View Printable Version
Find default passwords for certain network devices UNIX
This tip will work on almost any *nix OS with curl installed. Create a text file called dfltpwd on your Desktop. Copy and paste the following into the text file:
#!/bin/bash
curl -s http://www.phenoelit.de/dpl/dpl.html | 
grep -i $1 | sed "s/<[^>]*>/ /g"
Save the file, then switch to Terminal and type these three commands (without the $):
$ cd ~/Desktop
$ sudo mv ~/Desktop/dfltpwd.txt /usr/bin/dfltpwd
sudo chmod +x /usr/bin/dfltpwd
After doing the above, test the script with something like dfltpwd "belkin". The results should be a listing of default passwords for any Belkin devices from the phenoelit.de site.

[robg adds: I don't tend to save things to /usr/bin, for fear that I overwrite something someday if I ever reinstall the OS. Instead, I have a local user bin folder on my path where such files reside.

And before the comments start, this script is not a way to "hack" networked devices. The script merely makes it easier to find the default password for a given networked device. Speaking as someone who has looked up too many of these in manuals (can you say "AirPort Express?"), this is a handy timesaver. There are quite a few sites that provide such lists, and this little shell script merely makes it easier to view the data from one such site. Also, if you happen to have any network devices that still have their default passwords, this is a good time to mention that you really should change them, because they are about as far from secure as I am from a good golfer...]
  Post a comment  •  Comments (12)  
  • Currently 3.25 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (4 votes cast)
 
[10,798 views]  View Printable Version
Run a cron process more than every minute UNIX
Yesterday I had the need to run a cron process more than once a minute -- I wanted to grab a still frame from a web camera every 15 seconds, in order to create a time-lapse movie. The standard cron syntax lets you specify the minute(s) at which a task runs, but you cannot (to my knowledge) specify an interval less than once a minute (using */1).

After much Googling, I finally found the solution, posted in some newsgroup somewhere (sorry, I didn't mark the page): create a simple shell script that repeatedly runs the task, with a sleep command between each call to the job. In my case, my shell script looked like this:
#!/bin/sh
open /path/to/getpic.app
sleep 15
open /path/to/getpic.app
sleep 15
open /path/to/getpic.app
sleep 15
open /path/to/getpic.app
sleep 15
This will run the task four times over the course of a minute (not allowing for processing time). I then created my cron entry to call the above script, with the minute interval set to "every minute" (*/1), and sat back to see what would happen (not for the full twelve hours, of course!). The final result? Not too bad, but not perfect -- I captured a frame about every 25 seconds, instead of every 15. I think I need to reduce the sleep value to allow for the processing time of the getpic.app, so I'll try that later today. As for exactly what it was I was capturing for 12 hours, well, that too is for another day. (It will probably show up on my blog, not here -- though I may write up the 'acquire image' bit as a separate hint, as it was a little tricky.)

In summary, if you need a cron task to run more than once a minute, create a simple shell script using sleep, and have it call your other task the required number of times per minute, then tell cron to run that shell script. I'd love to hear if there are better ways to do this (via cron, launchd, or any other mechanism...).
  Post a comment  •  Comments (12)  
  • Currently 2.60 / 5
  You rated: 2 / 5 (5 votes cast)
 
[30,561 views]  View Printable Version