Sunday, November 4, 2012

Mac OS X shell scripts / Automating Terminal Commands in Mac OS X with Bash

http://jonlandrum.com/2012/01/28/automating-terminal-commands-in-mac-os-x-with-bash/


Coming from the Linux world, Mac was somewhat familiar to me, yet somewhat foreign. One such example is the system hides most of the files and directories I was accustomed to seeing and using as a Linux user. Also, the terminal commands to show and re-hide these directories don’t follow standard Unix terminal syntax. So I had to do some researching to find what the commands even are, and then I could put them in a Bashscript that could be run with a single click. But first, the commands:
defaults write com.apple.finder AppleShowAllFiles TRUE
killall Finder
killall Terminal
The first line is the business end, the second one restarts Finder with the new setting, and the last makes sure Terminal closes afterward. And you can type these in a Terminal window right now and get the desired results. But what about automating this? Instead of typing all this out — and possibly misspelling it a time or two (ask me how I know) — let’s make a shell script that contains the commands, and keep it in our user directory for easy access:
1
2
3
4
#!/bin/bash
defaults write com.apple.finder AppleShowAllFiles TRUE
killall Finder
killall Terminal
The only difference this time is the new first line. That specifies which shell we’re using, in this case Bash. It’s called the shebang line, and it tells Terminal which program to use to interpret the commands. Copy and paste those three lines into TextEdit or your favorite text editor, and save it as show.sh or something similar. Then change the “TRUE” to “FALSE” at the end of the second line, like this:
1
2
3
4
#!/bin/bash
defaults write com.apple.finder AppleShowAllFiles FALSE
killall Finder
killall Terminal
and save that as hide.sh or something similar. Put these two files in your user root directory, in the same place where your Documents, Desktop, Music, and Pictures folders are.
An example directory layout
An example directory layout. Put show.sh and hide.sh in your user root like this.
Next we have to make them executable. Open Terminal (it will already be cd’d to your user root directory) and enter this command:
$ chmod +x show.sh && chmod +x hide.sh
This will make our two files executable. Now give them a whirl:
$ ./show.sh
$ ./hide.sh
The ./ tells Terminal to run that program. This should open your Finder window with hidden files and directories showing, but faded to show you the difference. And when you run hide.sh Finder will close and reopen, with those files and directories hidden. If that’s what you got, then continue to the final step to make them clickable. If not, go back through this tutorial and start from scratch. Make sure you’ve typed everything correctly.
The last step is open a Finder window and select show.sh, then type Command-i to bring up the information menu. Under the “Open with:” dropdown, select “Other…”, and in the window that pops up, scroll to the bottom to the “Utilities” directory, open that, and then scroll to Terminal and select it.
You may have to change the “Enable:” menu from “Recommended Applications” to “All Applications” in order to select it. Tick the “Always Open With” checkbox, and click “Add”. Repeat this last step for hide.sh.
Double-click show.sh and confirm it works, then do the same for hide.sh. If everything works as expected, you’re all set! You can access these programs from the command line or from Finder the way you’ve done just now.
If you have other commands you’d like to automate, you can simply replace what’s in either of these scripts with that command, save it as a new .sh file, make it executable, set it to open with Terminal, and you’re golden. For timed automation, you can even have these programs run with cron so that they’re executed for you on a regular schedule. How’s that for automation?
~Jonathan

Justine Ezarik


Justine Ezarik


iJustine aka Justine Ezarik















Convert DMG image to ISO image

http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man1/hdiutil.1.html

http://songoku.homelinux.com/wordpress/?p=361


How to put OSX Mountain Lion (10.8) on a single-layer dvd

Mac OSX, by Andy.
Hello,
the downloaded Mountain Lion image has a total size of 4.8 GB which is too big for a single-layer dvd, but if you open the image and check the size of all files you only have 4.1 GB. So here is what you have to do to get the files on a normal dvd.
  1. Open the terminal and go to the folder where the installer dmg is located (InstallESD.dmg)
  2. Convert the dmg file to an iso file with
    # hdiutil makehybrid -o MountainLion.iso
  3. Now take the iso file and burn it normally to the single-layer dvd
That should be all, have fun with the new OS 
You can create ISO images from any source disk or data by using the command line in Mac OS X. This isn’t too different than burning themthrough Terminal, and you can use either the hdiutil tool or dd command.
While the command line is generally reserved for advanced users, using it to create ISO’s isn’t too complicated and will save you the hassle of having to download any third party apps. If you’re new to the Terminal, remember that dragging & dropping files into the Terminal window will print their full path, making it easy to point to source files and preventing any navigation through the command line.

Creating an ISO with hdiutil

The most reliable method is uses hdiutil, here is the syntax:
hdiutil makehybrid -iso -joliet -o image.iso /path/to/source
Here’s an example, creating an iso from a Windows 7 installer disc, with the end result showing up on the desktop:
hdiutil makehybrid -iso -joliet -o ~/Desktop/Windows7.iso /Volumes/Windows\ 7\ Install
The -joliet flag is necessary to make the iso fully compatible with Windows and other OS’s, though if your only requirement is to use the iso on a Mac you can leave it off.

Making an ISO with dd

Another approach is by switching around a previously discussed dd command, which makes it go from burning an image to creating an image. This may not be as reliable and it requires additional steps, so use dd only if you have a good reason not to use the primary hdiutil method.
Use the ‘diskutil list’ command to discover the disks identifier that you will need to make an ISO with dd from.
dd if=/dev/dvd of=/destination/path/dvd.iso
dd is often faster than hdiutil, but it’s definitely for more advanced users.


Quickly convert Mountain Lion DMG into an ISO file

From the terminal, after downloading Mountain Lion from the App store:
hdiutil makehybrid -iso -hfs /Applications/Install\ OS\ X\ Mountain\ Lion.app/Contents/SharedSupport/InstallESD.dmg -o OSX-10.8.iso




Tuesday, October 23, 2012

Ways to monitor hard-drive activity and usage in OS X

http://reviews.cnet.com/8301-13727_7-20061984-263.html

fs_usage fseventer

fseventer for Mac

fs_usage fslogger

There are several ways to figure out what files are being accessed by a specific application in OS X, which can help when troubleshooting various settings and system features.




Every now and then it can be useful to track how a program or process is accessing the hard drive. There are times when troubleshooting when you may want to track what files get changed when application settings are adjusted, or which files are accessed when you load a particular feature. Apple provides the Activity Monitor utility that will show you the overall input and output rates of the hard drive, but this overview does not specify the files that are being accessed.
While there are numerous ways to monitor hard-drive activity and usage in OS X, I've found that the following three options seem to work very well for identifying specific files that are updated by program activity.
Finder
Oddly enough, the Finder or another filesystem browser can be exceptionally useful in isolating files that are updated by programs. When a file is edited its modification date will change, so if you suspect a particular file holds a specific system setting then you can go to that folder in the Finder and sort items in list view by their creation dates.
After doing this, keep the Finder window open and monitor the creation date values when toggling your setting of interest. If a file in the present folder is altered by the settings, then you should see it go to the top of the list and be updated to reflect the new creation date. Sometimes when doing this it helps to wait at least a minute between invoking setting changes so you can clearly see a time change in the file's modification date (the Finder by default only shows date changes to the nearest minute).
While the Finder is a useful tool, it is fairly limited in that it will only show you when a file has been changed and not when it's accessed, and it will also only do so for the files in a single directory. You can sometimes get creative and lucky by using smart searches and other Finder features to expand these limitations, but overall it is still a rather crude approach.
Applications
Instead of using the Finder, a few third-party applications are available that can really help determine when files are being accessed. The one I use the most, called Fseventer, builds a graphical filesystem tree of the files and folders that have been accessed after a given point in time. The utility is fast and shows all filesystem events, including those in hidden files and even temporary files.
Fseventer activity window
Fseventer will show numerous attributes about the files that have been changed in a given monitoring session.
To use the utility, just open it and then go to the application or setting you wish to test. Click the Play/Go button at the top of Fseventer's window and the program will start monitoring filesystem events. Then perform the action you are interested in and watch the Fseventer window to see what files are accessed. After this, to prevent Fseventer from showing activity from other processes, go back and click the Play/Go button again.
While Fseventer is an exceptionally useful program, it does rather blindly target all filesystem events, so if in the middle of your monitoring session the system performs a MobileMe synchronization or other similar action then you may see the window fill up with events that are not associated with the events you are monitoring. Therefore it may be useful to repeat monitoring sessions several times with Fseventer to confirm that the files being accessed are correct.
Terminal
Finally, in addition to the Finder and Fseventer, you can use the Terminal command "fs_usage" to show filesystem events. This uses the same underlying technology as Fseventer, but can be used to isolate the filesystem events that were invoked by a certain process name or id. To use the fs_usage command, you will need to do the following:
fs_usage running in the Terminal
The Terminal tool 'fs_usage' will output all filesystem activity in a list, but the tool's options can be used to better isolate activity from various programs.
  1. Open the Terminal utility
  2. Type the following into the Terminal:
    sudo fs_usage
  3. Optionally type an additional space followed by the name of the application you are monitoring. In the case of iCal, for example, the command would look like the following:
    sudo fs_usage iCal
After the command is typed, press Enter and supply your password (this must be done from an Admin account) and the command will output to the Terminal window each time the program performs a filesystem access operation. This is a basic use of the command, but if you read themanual page for the command you can supply flags and options to further specify the type of events the command will monitor. When you want to quit the process in the Terminal window, just press Control-C.

--------------------------------------------------------------------

http://hints.macworld.com/article.php?story=20091223115101622

Find files created or modified by an installer Install
Do you like to know which files and folders get created or modified when you install a new application? I can suggest two different solutions: Either use a tool to track all file system changes, or use a tool that searches the disk by creation/modification dates.

[robg adds: This hint includes information about using a free program, Find Any File, as one way to track file system changes. The author of this hint (Thomas Tempelmann) is the author of that application. However, because there's good information here in the hint, and because the app itself is free and seems to work well, I felt it was worth sharing. Read on for the discussion...]

Logging tools

First the "pro" method: Use a file system logging tool to trace all file operations during a period of time. The problem with these tools, though, is that you might see much more information than you want. Sorting out what's relevant may need some experience and/or patience.

For Terminal-savvy users, the tools of trade are: fs_usage and fslogger. I'll not go into detail here, as they require some reading, and there are man pages available. There's also fseventer, which provides a graphical user interface to fslogger. However, it doesn't offer convenient filters, so you'll usually get much more information than you seek.

Regardless of the tool used, the workflow is as follows:
  1. Start the logging tool
  2. Install the software
  3. Stop/pause the logging tool and analyze its output.
Searching for modifications afterwards

Another alternative is to use my program, Find Any File (FAF), which allows you to search for recent file and folder changes on your disk. Again, this might show you more items than just those of an install process, but the hierarchical view in FAF's results window should make it easy to see which folders and changes are related to the installation.

The workflow is as follows:
  1. Note the time when you start the application installation.
  2. Once the installation has finished, launch Find Any File, and change its search criteria to something like: Modification Date -- is within the past -- X minutes, replacing the "X" with the amount of time that passed since you started the installation.
  3. Hold down the Option key, which changes the Find button to Find All, then click the Find All button, which will relaunch FAF in root mode (and ask for your admin password) and start searching all areas of your hard drive.
  4. Once you see the results window, switch to the hierarchical view (Command-2) to see which folders have changed items.
  5. Repeat the search process for files with a recent Creation Date
Note: The are many other find tools similar to Find Any File, but FAF has the ability to search in root user mode, making it possible to find even such files that may be protected or hidden to the "normal" user.

Conclusion

None of the above options are perfect. The file system logging technique is safer than using a search tool (which still can be fooled if an installer resets creation dates of files and folders). I sometimes like to know what gets installed in my system, especially if it's an installer that asks me for my admin password. Usually, the "find" technique is sufficient, but if you really need to know every detail, go the logging route.

I think the best way to see which files get modified or installed by an app or its installer is to develop a special application which is based on fslogger or fs_usage, and which pays attention only to modifications invoked by the targeted app or its installer. A unix whiz may even be able to get this working just with fs_usage (it has quite a few helpful filters for this), but I imagine something with an easy-to-use UI. Any takers?

Mimmo97 Blog Archive