Quantcast

Special Sponsored Section


The video player requires Flash 8 Player or later. Please download the latest Flash Player.


Maclife Hottest Articles
Thumbnail
FEATURE
100 Snow Leopard Tips, Tricks, and Features
Browser
FEATURE
OS X Browser Speed Wars: May the Fastest App Win
iTunes History
FEATURE
The Complete iTunes History -- SoundJam MP to iTunes 9
iTunes Tips
FEATURE
iTunes 9 Tips and Tricks - Solve the Mysteries of the New iTunes

Four Handy Command-Line Tricks on Mac OS X
Posted 09/15/2008 at 12:19:41pm | by Thomas Myer

Just like there are Mac people and PC people, there are GUI people and CLI (command line interface) people. GUI people use gestures, icons, and visual cues to get things done. CLI people build interfaces out of words—some of them made-up, quirky words that sound more like gastronomical noises then real commands.

Why use those funny commands at all when you’ve got a great GUI? Sometimes you just need to get under the hood, and the Mac’s Terminal lets you do just that. In this article, I’m going to show you four must-know commands that will introduce you to the power of the command line.

Opening Terminal

To access the command line, you need to open Terminal, which is stored under Applications > Utilities. When you open Terminal, you’ll see a command line prompt similar to the one in the figure below.

You type your commands at the blinking cursor. Ready to go? Let’s get started.

terminal

Where am I? The pwd command

To find out where you’re at, use pwd, which stands for “print working directory.” Type this command and press return and you’ll get back a full filesystem path of your current location. This way you’ll never be lost.

Macintosh:~ useracct$ pwd

/Users/useracct

As you can see above, I am currently in my home directory.

*Note: useracct will/should be replaced with your user account name.

 

Moving around with the cd command

You can move around with the cd (“change directory”) command. For example, to move to the desktop directory, you can type:

cd desktop

Running the pwd command confirms that I’m now in /Users/useracct/desktop:

Macintosh:desktop useracct$ pwd

/Users/useracct/desktop

By the way, if you were in some other part of the filesystem (say, /Applications and wanted to cd to your desktop, you could type either one of the following commands:

cd ~/desktop

cd /Users/useracct/desktop

In Mac OS X, the tilde (~) is shorthand for “my home directory”. It’s a nice shortcut to know, as it saves plenty of typing.

 

Listing files with the ls command

Once you’re in a directory, use the ls command to list out the files that are in it. Normally, when you run ls, you get a listing like this:

Macintosh: ~ useracct$ ls

file1.gif file5.doc

file2.jpg file6.html

file3.doc

You can get a lot more information by using the ls –l command (the ls command with the –l argument, and that’s the letter l not a number!). What you get is this:

Macintosh:desktop useracct$ ls -l

total 280

-rw-r--r-- 1 useracct useracct 78815 Jul 2 16:17 file1.gif

-rwx------@ 22 useracct useracct 748 Aug 20 14:45 file2.jpg

-rw-r--r--@ 1 useracct useracct 0 Sep 17 2007 file3.doc

-rwxr-xr-x@ 7 useracct useracct 238 Aug 19 16:25 file5.doc

-rw-r--r-- 1 useracct useracct 6553 Aug 19 16:27 file6.html

This display gives you the permissions for each file, along with owns the file, the file size, the date/time the file was last modified, and the file’s name.

 



Searching with the grep command

If you’re looking for a file that contains a certain word or phrase, you can use the grep (“global regular expression print”) command to do so. For example:

grep the *

will return any line that contains the string “the” in any file (*) of the current directory.

file6.html: What do we want them to do? (click, call, visit)

file6.html: The creative brief is where we discuss high-level strategy

 

Bonus: Combining Commands

These simple commands are all powerful in their own right, but they can also be combined with pipes. That’s because the output of any command can be redirected with the pipe character (“|”) to become the input for the next command.

For example, let’s say that you want a listing of all files in the current directory that were modified in August. Using a pipe, we combine the ls –l and grep commands, like so:

Macintosh:desktop useracct$ ls -l | grep "Aug"

-rwx------@ 22 useracct useracct 748 Aug 20 14:45 file2.jpg

-rwxr-xr-x@ 7 useracct useracct 238 Aug 19 16:25 file5.doc

-rw-r--r-- 1 useracct useracct 6553 Aug 19 16:27 file6.html

The ls –l command results in a list of files. The list of files is fed to the grep command, which matches any lines that contain the string Aug, then prints out any matches.

COMMENTS: 2
TAGS:  Terminal
COMMENTS
avatarAwesome

er..
cd
/awesomeness/rb/thisarticle/rocks/
ls
mrmeowgithanksyou.jpg

Login or register to post comments
avatarAccuracy counts

If you're going to come off as informing/instructing then you need to proof your work. "grep the *" will never output the line "file6.html: The creative brief is where we discuss high-level strategy" because grep is case-sensitive. Unless you've aliased "grep" to "grep -i", which is a command you haven't mentioned. But full points for correctly attributing the meaning of grep. It's a mash up of the "ex" command that used to be the only way to search, before grep was written: "g/re/p" (global - every line in the file, re - regular expression, print lines that match). Good try, but you can do better.

Login or register to post comments