9 OS X Command Line Utilities you probably didn’t know about

OS X is ment to be a clean and a simple operating system,
yet under the hood it’s very powerful.

Here I shall go over 9, rather random, commands.
You can learn more about them by typing “man command-name” in the console.
Example, “man shutdown”.

Here we go!

1. shutdown
The command shutdown will shut down the system.

It must be run as root, by using the sudo prefix (which makes the command run as root)
Examples:
Reboot the computer
sudo shutdown -r now
Halt the computer (turn if off)
sudo shutdown -h now
Put the computer to sleep
sudo shutdown -s now
2. say
This tool uses the Speech Synthesis manager to convert input text to audible speech.

3. screencapture
The screencapture command can be used to capture a screenshot of the computer.
Example:
box:- petur$ screencapture screen.png
“Why on earth would you want to do that using the command line?”, you might ask. Because you can capture screenshots of remote computers, proven that you have root access to it via ssh.
It could also be useful in some situations to make a screenshot of a particular computer every x-minutes by combining ‘screencapture’ with the root crontab. Unfortunately crontab is, as I would say, an advanced topic and it will not be covered in this post.
4. arch
The arch command with no arguments, displays the machine’s architecture type.

The results can be read as follow:
i386 : 32-bit intel
x86_64 : 64-bit intel
ppc : 32-bit powerpc
ppc64 : 64-bit powerpc
5. top
The top CLI (Command Line Interface) equvalient of the Activity Monitor.


6. file
The file command is used to determine a file type.

Usage: file filename

7. iostat
iostat reports I/O statistics.
In other words, the load on your hard disk and the cpu.

Those numbers can be a little difficult to understand at first,
tps stands for Transfers per Second
KB/t stands for kilobytes per Transfer
MB/s stands for megabytes per second.

8. df
df stands for “display free”, as in display free disk space.
It’s one of those utilities you can’t be without!
-l filters out all non-local filesystems
-h makes the output more human friendly (Gi and Mi instead of plain Bytes)


9. vm_stat
Virtual memory Statistics
the hit rate is perhaps the most important information,
as a high rate indicates that you should add more RAM (memory) to your computer.


 
Please share your thoughts with me,
if you have any questions don’t hesitate to ask!

Change to your Favorite directories on the bash shell by scripting ‘cd’.

[ad#Banner]

I’m sick and tired of having to remember and ‘cd’ long paths.
I decided to create my own console based Favorites-script which makes use of a definition file which is easy to change.
A newer version has been released, please visit:
http://www.petur.eu/blog/?p=190.
“cd /home/petur/Documents/school/2010/fall/chemistry” becomes “cdf chemistry”
and “cd /var/log” becomes “cdf log”

The setup process is pretty straight forward, here we go (single user setup):
Save the following script as ~/bin/cdf

#!/bin/bash
#
# Pétur Ingi Egilsson ( petur@petur.eu )
#
# cdf (Change to favorites)
# Usage: cdf favoriteALIASES=~/bin/cdf.conf
fullpath=$(grep $1, $ALIASES|cut -d, -f2)
if [ ${#fullpath} -ne 0 ]
then
cd $fullpath
else
echo "Error: '$1' has not been defined in $ALIASES"
echo -n "Do you want to edit the file? (y/n): "
read editFile
case $editFile in
[yY])
if [ ! -n "$EDITOR" ]
then
# Use the nano editor because
# the EDITOR env has not been set.
nano $ALIASES
else
$EDITOR $ALIASES
fi
;;
[nN])
;;
*)
echo "Please use y,Y,n or N."
exit 1
esac
fi

Make it executable

petur@klettur:~$ chmod +x ~/bin/cdf

Save the following file as ~/bin/cdf.conf

# Definition file for cdf
#
# ATTENTION: Do not use the ~ (tilda).
# WRONG: documents,~/Documents
# RIGHT: documents,/home/petur/Documents
#
# format: name,/path/to/directoryetc,/etc
logs,/var/logs
mnt,/mnt
root,/root
var,/var

Bash scripts are executed in a subshell as child processes.
This behavior is undesirable as the bash child-process cannot tell the parent to change to the new directory.
In order to work around this you’ll need to execute the script by placing a dot-space(. ) in front of it.
If you are like me you would like to avoid having to do that so create an alias like this in .bashrc :

petur@klettur:~$ echo alias cdf=\". ~/bin/cdf\" | tee -a ~/.bashrc

Do you have any comments?