You are currently browsing the archives for the "command line" tag.

Command Line Goodness
Tuesday April 17th 2007, 2:09 am
By Theron Parlin
Tags: Unix, command line, terminal

So one of the reasons I switched to apple way back when is because underneath the hood of Apple’s beautiful graphical user interface lies the best performing, most reliable operating system in the world, Unix. Coming from the Unix world, I tend use the command line to do a lot of my work, so I thought I’d share some of the techniques I use to make my OS X command line experience more user friendly, as well as show you some neat tools.

Open Terminal
So how do you get to the command line on a mac? It’s simple, from your finder, go to “GO ->Utilities -> Terminal.” The terminal is your window to the world of Unix.

Aliases
Aliases are just like shortcuts, but for the command line. Instead of typing ‘ls -CF -G’ every time you want to list your files, you could simply alias it to ‘ls’. Here are a couple of my aliases:

alias ls="ls -CF -G"
alias dev="cd /Users/tparlin/dev/geezeo"

You can store aliases in your .bash_profile file in your home directory.

ls.jpg

As you can see, my aliased version of ‘ls’ provides me with colors and the ability to determine the type of file I’m looking at (e.g. blue is a directory, black is a text file, etc).

Human Readable Command Output
A few of the Unix file commands have a -h option, which stands for “human readable.” For instance, if I type ‘ls -l,’ I’ll get the following directory listing:

[tparlin@mbpro tparlin]$ ls -l
total 56
drwxr-xr-x    6 tparlin  tparlin    204 Apr 13 01:28 Backups/
drwx——    5 tparlin  tparlin    170 Apr 17 00:00 Desktop/
drwx——   11 tparlin  tparlin    374 Apr 15 09:54 Documents/
drwxr-xr-x    4 tparlin  tparlin    136 Mar 23 12:54 Incomplete/
drwx——   45 tparlin  tparlin   1530 Apr 15 03:43 Library/
drwx——    4 tparlin  tparlin    136 Mar 29 01:04 Movies/
drwx——    9 tparlin  tparlin    306 Mar 21 23:28 Music/
drwx——   12 tparlin  tparlin    408 Apr  3 13:36 Pictures/
drwxr-xr-x    5 tparlin  tparlin    170 Mar  8 22:41 Public/
drwxr-xr-x    3 tparlin  tparlin    102 Apr 13 01:28 Shared/
drwxr-xr-x    5 tparlin  tparlin    170 Mar  7 14:09 Sites/
drwxr-xr-x    4 tparlin  tparlin    136 Mar 17 23:37 allpeers/
drwxr-xr-x    7 tparlin  tparlin    238 Apr 11 13:17 dev/
-rw-r–r–    1 tparlin  tparlin   5459 Apr 10 10:42 geezeo.pem
drwx——    4 tparlin  tparlin    136 Apr 10 10:28 mail/
drwxr-xr-x    7 tparlin  tparlin    238 Apr 13 02:51 operajs/
-rw-r–r–    1 tparlin  tparlin    296 Apr 14 09:05 searchform.php
-rw-r–r–    1 tparlin  tparlin  13652 Apr 15 08:42 style.css
[tparlin@mbpro tparlin]$

This isn’t very helpful if I’m trying to determine the size of the files. Sorry, my brain just has trouble quickly converting bytes to megabytes, so instead I add the -h option to ls and I get the following human readable output:

[tparlin@mbpro tparlin]$ ls -lh
total 56
drwxr-xr-x    6 tparlin  tparlin   204B Apr 13 01:28 Backups/
drwx——    5 tparlin  tparlin   170B Apr 17 00:00 Desktop/
drwx——   11 tparlin  tparlin   374B Apr 15 09:54 Documents/
drwxr-xr-x    4 tparlin  tparlin   136B Mar 23 12:54 Incomplete/
drwx——   45 tparlin  tparlin     1K Apr 15 03:43 Library/
drwx——    4 tparlin  tparlin   136B Mar 29 01:04 Movies/
drwx——    9 tparlin  tparlin   306B Mar 21 23:28 Music/
drwx——   12 tparlin  tparlin   408B Apr  3 13:36 Pictures/
drwxr-xr-x    5 tparlin  tparlin   170B Mar  8 22:41 Public/
drwxr-xr-x    3 tparlin  tparlin   102B Apr 13 01:28 Shared/
drwxr-xr-x    5 tparlin  tparlin   170B Mar  7 14:09 Sites/
drwxr-xr-x    4 tparlin  tparlin   136B Mar 17 23:37 allpeers/
drwxr-xr-x    7 tparlin  tparlin   238B Apr 11 13:17 dev/
-rw-r–r–    1 tparlin  tparlin     5K Apr 10 10:42 geezeo.pem
drwx——    4 tparlin  tparlin   136B Apr 10 10:28 mail/
drwxr-xr-x    7 tparlin  tparlin   238B Apr 13 02:51 operajs/
-rw-r–r–    1 tparlin  tparlin   296B Apr 14 09:05 searchform.php
-rw-r–r–    1 tparlin  tparlin    13K Apr 15 08:42 style.css

Pretty neat, eh? You can use the -h on a couple of other commands, for instance, how about a quick snapshot of your filesystem?

[tparlin@mbpro tparlin]$ df -h
Filesystem                Size   Used  Avail Capacity  Mounted on
/dev/disk0s2              111G    35G    77G    31%    /

How much disk space am I using in my home directory?

[tparlin@mbpro tparlin]$ du -hcs .
 13G    .
 13G    total

How about in my Music folder?

[tparlin@mbpro tparlin]$ cd Music/
[tparlin@mbpro Music]$ du -hcs .
 11G    .
 11G    total

Pretty neat, aye?

Using find
The all mighty spotlight is great for most searches, but what if you want to find all files that contain a specific phrase? Spotlight is no longer your friend, but the Unix command “find” is!

find . -exec grep -l "position: relative" {} \;
./dev/geezeo/trunk/public/stylesheets/geezeo.css
./dev/geezeo/trunk/public/stylesheets/geezeoforms.css
./dev/geezeo-mobile/public/stylesheets/geezeo.css
./dev/geezeo-mobile/public/stylesheets/geezeoforms.css
./dev/geezeo_wordpress_theme/style.css

In the above command I did a search for all files containing the phrase “position: relative,” quite naturally, I got back a bunch of css files. I actually got back close to 100 results, but I cut them out for the sake of brevity.

The spotlight search for the same phrase also returned results, however, none of them were accurate.

spotlight.jpg

Other Utilities
Here is a list of some other very useful utilities that you can use on the command line:

man

man formats and displays the on-line manual pages. If you specify section, man only looks in that section of the manual. name is normally the name of the manual page, which is typically the name of a command, function, or file. For instance, to learn all there is to know about man, type “man man.”

top

The top program periodically displays a list of processes on the system in sorted order. The default key for sorting is pid, but other keys can be used instead. Various output options are available. Think of top as the command line equivalent to the activity monitor in OS X. Type ‘q’ to exit.

pwd

Writes the absolute pathname of the current working directory to the standard output. It’s basically like in-car navigation that tells you where you are if you get lost.

clear

Sometimes you just need to clear your head (and your terminal). It’s very easy, just type ‘clear’. If clear is a command that you use often, just alias it, ‘alias c=clear’.

more

Say you want to view a file without actually opening it in a text editor, just type ‘more filename.’

file

What if you want to know what type of file you’re dealing with before you open it? In the finder, you could right click on the file and “get info.” On the command line, you can simply type ‘file filename’.

[tparlin@mbpro Sites]$ file index.html
index.html: HTML document text

Of course, looking at the extension can tell you the same thing, but sometimes files don’t have extensions and file can help you determine if what you’re looking at is a binary file or a script.

wc

The wc utility displays the number of lines, words, and bytes contained in a given file.

[tparlin@mbpro Sites]$ wc index.html
     125     799    5754 index.html

You can also run it on a bunch of files:

[tparlin@mbpro controllers]$ wc *
      22      50     669 application.rb
      61     185    1649 forums_controller.rb
      10      20     235 moderators_controller.rb
      20      56     679 monitorships_controller.rb
     122     503    4923 posts_controller.rb
      63     179    2169 session_controller.rb
      98     359    3355 topics_controller.rb
     106     368    3631 users_controller.rb
     502    1720   17310 total

Want just lines of code?

[tparlin@mbpro controllers]$ wc -l *
      22 application.rb
      61 forums_controller.rb
      10 moderators_controller.rb
      20 monitorships_controller.rb
     122 posts_controller.rb
      63 session_controller.rb
      98 topics_controller.rb
     106 users_controller.rb
     502 total

tail

tail is one of my favorite commands. With tail you can view a log file in real time as it’s being written to. Use it by typing ‘tail -f filename’ and watch the log grow before your very eyes, oooh magic!

tty

Use tty to learn the the name of the terminal that’s currently attached to standard input. Why on earth would you ever want to do this? See the next command.

script

I LOVE this command. Most users won’t have really need this command, but it’s great if you’re logged into a remote mac (e.g. helping someone out). You can use it as a way of showing someone what you’re typing in real time. That’s where the tty command comes into play. Have the other person type tty and tell you the results to get the location of their terminal. Then, you can start up a script session and direct the output to their terminal: ’script -a /dev/ttyp0′ for example. At this point, the other person can sit back and watch your terminal session in real time. You can also send the output to a file.

reboot

Yes, you can actually reboot your mac from the command line, just type ’sudo reboot,’ enter your password and say bye bye.

So as you can see, the OS X command line can provide you with some useful tools, and by using aliases, you can make those tools a little more friendly. Enjoy.