Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates

Shared on Facebook

Shop at Amazon

Ad

OSXDaily on Facebook

Search Command Line

Make Mac OS X Speak (or Sing) the Output of Any Command Line Task

May 11, 2012 - 2 Comments

Speak the output of a command line task

Along the lines of vocally announcing task completion within the command line, you can also have Mac OS X speak the output of any executed command.

This is really easy, all you need to do is pipe the output of any command directly to “say” like so:

uptime | say

In that example, this will speak the output of uptime using the default OS X text-to-speech voice, though you can change the voice to anything else with the -v flag:

uname -a | say -V Samantha

Taking this a step further, it is possible to export the spoken command output to an audio file. Here’s a pointless example of saving the spoken output of the openssl manual page as an m4a file:

man openssl | say -v Samantha -o "talkopenssltome.m4a"

If you want to get ridiculous, you can use one of the singing voices to, well, sing the output of a command. And who doesn’t want an audio file of of the robotic Cellos voice or the awfully perky Good News singing the output of a manual page? Put it on your iPod for the drive home.

man openssl | say -v Cellos -o "serenadingmanpages.m4a"

If you don’t like the bundled voices you can always add new voices to OS X for free through Speech preference panel.

Get creative, because the ultimate utility of this is entirely up to you.

Thanks to Greg P for pointing this out in the comments

Announce When a Command Line Task is Completed in Mac OS X

May 8, 2012 - 10 Comments

Announce the completion of a command in Mac OS X Terminal

By appending the say command to the end of another command, Mac OS X will vocally announce when the initial task has finished running successfully. For example, to have OS X announce that a particular script has finished running the command could be:

python backup.py && say "jobs done"

The important part is the “&& say” portion, which can also be customized with other voices from Mac OS X’s text to speech options by using the -v flag followed by a voice name, like so:

dscacheutil -flushcache && say -v Alex your cache has been cleared sir

This is perfect for running scripts, making svn/git commits, compiling code, and other tasks that can take an indeterminate amount of time to complete and where it’s easy to become distracted by facebook^H^H^H^H^H^H^H^H other work.

This great tip was posted by @niels on Twitter, follow @osxdaily there too to get our latest posts and updates.

Empty Contents of a Specified File Without Deleting via the Command Line

May 7, 2012 - 3 Comments

Remove Contents from a specified file without deleting the file

If you’re working at the command line and need to quickly empty the contents of a file, throw a greater than symbol and a space in front of the filename like so:

> filename

All content within the target file will be immediately removed without warning, leaving it blank while retaining the files existence, filename, and permissions. This is often preferable and quicker than manually deleting and recreating a file.

A good practical example is for clearing out the contents of log files, for example:

> ~/Library/Logs/updates.log

You can also use this command to create a new 0 byte file at the location, similar to using the touch command.

Generate and Set a Random Valid MAC Address from the Command Line in OS X

May 2, 2012 - 3 Comments

Generate and set random MAC address in OS X

We’ve shown you how to generate MAC addresses randomly and then how to go about changing a MAC address in OS X Lion and OS X Mountain Lion, but why have those be two separate actions? Using the command line, you can combine the two events into a single action to generate a valid MAC and then set it immediately.

Launch the Terminal and paste the following onto a single line:

openssl rand -hex 1 | tr '[:lower:]' '[:upper:]' | xargs echo "obase=2;ibase=16;" | bc | cut -c1-6 | sed 's/$/00/' | xargs echo "obase=16;ibase=2;" | bc | sed "s/$/:$(openssl rand -hex 5 | sed 's/\(..\)/\1:/g; s/.$//' | tr '[:lower:]' '[:upper:]')/" | xargs sudo ifconfig en0 ether

There is no confirmation or output, the MAC address is set immediately which you can verify with the following command:

ifconfig en0 |grep ether

You will probably need to reconnect to the wireless router after issuing the command, and in some cases turn wi-fi on and off again.

If you intend on using this often, setting up an alias in .bash_profile would be a good idea to avoid having to cut and paste the massive block of text.

Thanks to osmium for the great tip

Open Terminal Faster by Specifying a Shell

Apr 30, 2012 - 2 Comments

Open Terminal faster by specifying a login shell

Whenever a new Terminal window is launched, a variety of system logs are read to provide information on the last login. One way to speed up launching a new terminal window is to delete those .asl log files, but you can achieve a substantial speed boost by simply setting Terminal to open a shell rather than /usr/bin/login. This is achieved through the same means of changing the shell and is just a few quick steps within Terminal settings:

  1. Open Preferences from within the Terminal pulldown menu
  2. Click the “Startup” tab
  3. Look for “Shells open with:” and check the box next to “Command (complete path):”

The default shell is /bin/bash but you’re free to choose another.

Specify a shell within Terminal to speed up opening new Terminal windows

Once this setting has been adjusted, launch a new Terminal window and you should immediately notice the difference. This works because the many .asl log files are bypassed, but as a side effect you will lose the “Last login” MOTD message at login unless specified otherwise.

Thanks for the tip Darren & Yorkster

Free Up Inactive Memory in Mac OS X with Purge Command

Apr 24, 2012 - 25 Comments

Free Up Memory in Mac OS X

Mac OS X has fairly good memory management but it’s not perfect, and sometimes RAM can be held unnecessarily in the “inactive” state despite the contents no longer being needed. If you’ve been participating in memory heavy activities or you just need to free up some available RAM you can actually force Mac OS X to clear out inactive memory.

  • Launch Terminal, found in /Applications/Utilities/ and enter the following command
  • purge

  • Give OS X a minute or two to complete the process

Open Activity Monitor to see the before and after results yourself, you’ll find dramatic changes at the “Free”, “Used”, and “Inactive” meters under System Memory.

The purge command forces disk and memory caches to be emptied, offering a ‘cold disk buffer cache’ which is similar to the state of the operating system after a reboot. Of course, the benefit of using purge rather than rebooting is that you don’t have to restart the machine and you can maintain currently active applications while still freeing up memory.

This is not necessary for most Mac users, but power users and those with heavy memory demands will undoubtedly find this command helpful in the future. If you feel like you are frequently hitting a memory ceiling learn how to check if your Mac needs a RAM upgrade and consider upgrading, it can dramatically improve overall system performance.

Note: You may need to have XCode & Developer Tools installed in order to use the purge command, that can be downloaded for free from the Mac App Store.

Change Length of Bash Command History or Disable Bash History Completely

Apr 12, 2012 - 2 Comments

Bash History

A users .bash_history file keeps a running tab of command line history, logging every command that has been entered into the bash prompt. These command history files make it very easy to find and recall past commands that may have been forgotten, and they’re also extremely useful for system administration. We will cover how to change the stored length of these files, how to disable it, and also how to quickly check a users bash history.

Changing Bash History Length

To increase the history length of a users command history, add the following line to .bash_profile:

HISTFILESIZE=2500

The example above will increase the history size to 2500 commands, that can be changed to any other number as deemed appropriate.

Disable Bash History

Setting the HISTFILESIZE number to 0 within .bash_profile will disable bash command history completely:

HISTFILESIZE=0

Having the history file disabled does not effect command recall, but it does prevent a super user from easily seeing the commands entered into another users shell.

Checking Bash History

There’s a few quick ways to see command history, to see your own type:

history

You can also export that command history to a file with the -w flag:

history -w pastbash.txt

To see another users command history, use cat with their .bash_history file instead:

cat /Users/USERNAME/.bash_history

Remember that if USERNAME has set their history file size to zero, nothing will be shown.

Practical Applications for Mac Users
Two of the most common applications for a Mac user is to keep track of defaults entries that have been entered into the Terminal and to quickly find past commands. Query command history and you won’t have to guess what that obscure command was that you entered four months ago that started with a ‘s’.

How to Create a Tar GZip File from the Command Line

Apr 5, 2012 - 7 Comments

How to Create a Tar GZip Archive Bundle

You’re probably familiar with making your own zip files if you’ve ever needed to transfer a group of files or if you’re managing your own backups outside of Time Machine. Using the GUI zip tools are easy and user friendly, but if you want some more advanced options with better compression you can turn to the command line to make a tar and gzip archive. The syntax will be the same in Mac OS X as it is in Linux.

Creating a Tar GZip Archive Bundle

From the command line (/Applications/Terminal/), use the following syntax:

tar -cvzf tarballname.tar.gz itemtocompress

For example, to compress a directories jpg files only, you’d type:

tar -cvzf jpegarchive.tar.gz /path/to/images/*.jpg

The * is a wildcard here, meaning anything with a .jpg extension will be compressed into the jpegarchive.tar.gz file and nothing else.

The resulting .tar.gz file is actually the product of two different things, tar basically just packages a group of files into a single file bundle but doesn’t offer compression on it’s own, thus to compress the tar you’ll want to add the highly effective gzip compression. You can run these as two separate commands if you really want to, but there isn’t much need because the tar command offers the -z flag which lets you automatically gzip the tar file.

Opening .tar.gz Archives

Unpacking the gz and tar files can be done with applications like Pacifist or Unarchiver (free), or by going back to the command line with:

gunzip filename.tar.gz

Followed by:

tar -xvf filename.tar

Generally you should untar things into a directory, or the present working directory will be the destination which can get messy quick.

Lock the Mac Desktop from the Command Line

Mar 30, 2012 - 1 Comment

Lock the Mac OS X Screen from the Command Line

With the help of a buried menu item, we can lock the Mac OS X screen right from the Terminal. This does not log a user out, it just brings up the standard Mac OS X lock screen and login window, requiring a valid user and password before the Mac can be used again.

Open Terminal and enter the following in a single line:

/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend

There isn’t any confirmation, the desktop is immediately locked and the lock screen appears regardless of what is currently going on with the active user account.

For those wondering, the menu item being used is the same Fast User Switching menu that displays a user name in the upper right corner, and the lock screen shown is identical to what is summoned if one were to select “Login Window…” from that same menu.

You can lock a screen using a keyboard shortcut as well, but using the command line offers two obvious advantages; it can be included within scripts or entered from SSH to remotely lock a Mac.

Quickly Get a Router IP Address from the Command Line

Mar 23, 2012 - 7 Comments

Router IP Address

To quickly retrieve the IP address of the router you are connected through, type the following at the command line:

netstat -rn |grep default

The result will look something like this, showing the router IP directly after “default”:

default 192.168.1.1 UGSc 108 0 en0

netstat will work for both wired and wireless connections, the difference of which will be shown as the interface. These are typically en0 and en1, though the port interfaces will vary slightly with Macs that don’t include ethernet ports or AirPort wireless card, and for Macs with only one form of network connectivity it will almost always be en0.

For users who don’t want to bother with the command line, the router IP can also be found from the Network system preference panel.

Thanks to Hans and Jen for the tip