Recent Comments

Shared on Facebook

Support Us

Mac Picks

iPhone & iPad Picks

Mac from PC Picks

Search Command Line

Set IP Address from the Mac Command Line

set ip address mac command line
The quickest way to set your IP address from the command line is to issue the following command in the Terminal:
sudo ipconfig set en1 DHCP
This will renew your DHCP lease and you will be issued a new IP address from the DHCP server. FYI: en1 is generally wireless/airport, en0 is generally ethernet.

You can check that the IP is set by getting your current IP address from the command line with:
ipconfig getifaddr en1
Doing this before and after will insure you have a new IP.

You can specify an IP address to set via the command line with the following:
sudo ipconfig set en1 INFORM 192.168.0.150

You can also get a new IP address from a DHCP server by bringing down the interface and starting it back up again:
sudo ifconfig en1 down ; sudo ifconfig en1 up

Note: for whatever reason, when you are setting the IP address manually via the command line the Mac OS X Network Preferences doesn’t necessarily catch up to the changes. Do not be surprised if the Network preference pane is telling you that you “Airport does not have an IP address and cannot connect to the Internet.” when in fact, you do have one and you are online. You can verify that you are connected to the LAN or internet by using the ping command.

Check the PATH of your Mac with echo $PATH

Anytime that you run a command through the Terminal directly by a command name like ls or dscacheutil, your Mac is looking through a series of directories for that command to exist. This list of directories is called the PATH, and it’s a carry over from the unix underpinnings of Mac OS X. As you may have guessed, checking your PATH in Mac OS X is the same as it is in most other unix variants:

echo $PATH

Executing that command, you’ll see something like:
$ echo $PATH
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin

The directories listed are those that are searched for commands. So the next time someone asks you if a directory is in your path, now you know where to look.

Print a random tweet from the command line

By pasting the following small function into your .profile, you’ll be able to print random tweets from any Twitter feed:

Once the code is saved into your .profile just type ‘randomosx’ to get a random tweet:
$ randomosx
New: iMac Touch runs both Mac OS X and iOS http://osxdaily.com/2010/08/23/imac-touch-runs-mac-ios/ #apple #mac

In the example code, we are using the OS X Daily Twitter feed, but “shitmydadsays” and “ConanOBrien” both make for a hilarious random tweets. You can also change the function name to something like ‘randomtweet’ or whatever else to fit the feed you are randomizing.

This works in Mac OS X and Linux, and should work in other unix environments as well.

If you like this, why not learn to post tweets from the command line too? Have fun!

Good command line usage habits and tips

If you use the command line frequently, chances are you may have some bad command line habits. IBM’s DeveloperWorks site has posted 10 good UNIX usage habit tips, some of them are just pretty handy tricks in general and if you’re new to the Mac OS X Terminal, you’ll probably learn something since practically all of them work within the Mac OS X command line. Here’s one of my personal favorites since it hit home with my command line activities:

* Change the path to unpack something into rather than moving the archive file itself, in this example by using the -C flag with the tar command:

tar xvf -C path/to/unpack newarc.tar.gz

I’m certainly guilty of moving archives around, but that’s partially because I like to keep all of them in a central location. But if you’re going to just delete the archive anyway, there’s no point in moving the archive file around just to unpack it. Save yourself the keystrokes.

Here’s the full list of tips in the IBM DeveloperWorks article:

* Make directory trees in a single swipe
* Change the path; do not move the archive
* Combine your commands with control operators
* Quote variables with caution
* Use escape sequences to manage long input
* Group your commands together in a list
* Use xargs outside of find
* Know when grep should do the counting — and when it should step aside
* Match certain fields in output, not just lines
* Stop piping cats

Check them out: IBM DeveloperWorks: Learn 10 good UNIX usage habits

How to use Twitter from the command line

Twitter-iconOften overlooked, the terminal can be used to accomplish all sorts of fun stuff. Something that you may find handy, is the ability to Tweet using built in Mac OS X command line utilities. Now of course this is not a full featured Twitter client by any means, but if you are looking for potential geek “cred” this is a fun trick to demonstrate.

Copy and paste the entire text in the grey boxes. When you paste it in your Terminal.app, it will show up as one single line.

To display a list of tweets (replace osxdaily with a twitter username of your choice):
curl -s http://twitter.com/osxdaily | grep '' | cut -d">" -f2 | cut -d"<" -f1

To update your twitter status:
curl -u your_user:your_password -d status='This is My update' https://twitter.com/statuses/update.xml

That's it! Imagine all of the fun automation possibilities !

Install Watch Command on OS X

If there was one command I would really complain about not being on Mac OS X, it would be “watch”. Watch is one of those great pieces of software that is tiny and completely out of the way, but when needed it will be a life saver.
Read more »

Merge directories in Mac OS X

You can quickly merge any two directories within Mac OS X by using the command line tool ditto. Launch the Terminal and use the following syntax:

ditto directory1 directory2

If a directory already exists at the destination (directory2) then the contents of the source (directory1) will be merged with the contents of the destination (destination2). So I want to merge pictures from “August 2010″ into “Summer 2010″ I will use:

ditto "August 2010" "Summer 2010"

The man page for ditto describes further:

In its first form, ditto copies one or more source files or directories
to a destination directory. If the destination directory does not exist
it will be created before the first source is copied. If the destination
directory already exists then the source directories are merged with the
previous contents of the destination.

If you’re not familiar with the command line, you may want to just use the GUI to perform this type of action.

Repair disk permissions from the command line

You can initiate the same Repair Disk Permissions functionality that is seen in Disk Utility via the Terminal by typing the following command in the Terminal:
diskutil repairPermissions /

This will repair the disk permissions on your Mac’s main drive, if you want though you can run it on another disk by specifying it rather than / at the command line. Once the command has been executed you will see a message like:
Started verify/repair permissions on disk0s2 Mac HD

The time it takes to repair the disk permissions depends on various factors, but the command will update as permissions are repaired and will end itself when diskutil is finished. If you have verified disk permissions and you find a bunch of problems, you might want to cross check them with this list from Apple for errors that you can safely ignore.

Combine ping and traceroute with MTR

I came across an excellent alternative to the command line ‘ping’ and ‘traceroute’ tools recently. It’s a utility called mtr, which combines the statistics and prints their functionality into a single network diagnostic tool, reporting details on the network connection between your host machine and whatever the destination host is, determining the address of each network hop (like traceroute), and sending ICMP ECHO requests along the way (like ping) to determine the response quality of each network link.

mtr for mac

To install and use mtr you will either need DarwinPorts or to be a familiar with the command line and compiling from source on your Mac, both of which requires the installation of Xcode.

You can download the source of MTR at the developers home or the more user friendly installation through DarwinPorts.

I found mtr while reading old posts on OneThingWell, and the screenshot is borrowed from them as well.

List all third party kernel extensions

If you’re troubleshooting a machine it can be helpful to list what kernel extensions are activated, particularly third party kexts. Here’s an easy way to list all third party kexts, using the command line:
kextstat | grep -v com.apple

Of course you can always list all kernel extensions (Apple’s official Mac OS X kexts included) by just typing:
kextstat