You'll get your Mac news here from now on...

Help TMO Grow

Subscriber Login

Advertising Info


The Mac Observer Express Daily Newsletter


More Info

Site Navigation

Home
News
Tips
Columns & Editorials
Reviews
Reports
Archives
Search
Forums
Links
Mac Links
Software
Reports
Contact

Mac OS X Command Line 101
by Richard Burton


Working With Files From The Command Line
Part V of this series...
May 24th, 2002

"File under 'Wheat'."
- Comment printed on REM's Green album

This series is designed to help you learn more about the Mac OS X command line. If you have any questions about what you read here, check out the earlier columns, write back in the comments below, or join us in the Hardcore X! forum.

Now that we have seen how to use commands with arguments and wildcards, let's see how we can use them to do some useful things. When you are on the command line, eventually you will have to know how to manipulate files and directories: how to move files, delete them, copy them, get information on them, etc.

Before we start, let's review the shell's approach to files and directories. When Terminal.app fires up a new shell, we can see where we are by pwd:

    Welcome to Darwin!
    [localhost:~] dr_unix% pwd
    /Users/dr_unix
    [localhost:~] dr_unix%
    

'/Users/dr_unix' is the full pathname of our current working directory. '/Users' is the parent directory of 'dr_unix'. A '/' seperates directories and files in pathnames. Also, recall that a lone '.', in the context of a file or directory name, is a shortcut for 'my current directory'.

    [localhost:~] dr_unix% ls .
    Adam.txt   Documents  Movies     Pictures   Sites      sigquotes  test_1.txt
    Desktop    Library    Music      Public     personal   temp.html  who_list
    [localhost:~] dr_unix% cd ./Desktop
    [localhost:~/Desktop] dr_unix% pwd
    /Users/dr_unix/Desktop
    [localhost:~/Desktop] dr_unix% 
    

Also, recall that '..' is a shortcut for 'my parent directory'.

    [localhost:~/Desktop] dr_unix% pwd
    /Users/dr_unix/Desktop
    [localhost:~/Desktop] dr_unix% cd ..
    [localhost:~] dr_unix% pwd
    /Users/dr_unix
    [localhost:~] dr_unix% 
    

We've already seen and used three common commands for manipulating files and directories: cd for moving through the directory structure, pwd for finding out where we are, and ls for listing files in a directory and information on them.

Let's continue with the simplest command, cp. (Mnemonic: short for 'copy'.) cp will copy a file or directory from one place to another. It simply takes the form cp sourcefile destinationfile:

    [localhost:~] dr_unix% ls
    Adam.txt   Documents  Movies     Pictures   Sites      sigquotes  test_1.txt
    Desktop    Library    Music      Public     personal   temp.html  who_list
    [localhost:~] dr_unix% cp who_list what_list
    [localhost:~] dr_unix% ls
    Adam.txt   Library    Pictures   personal   test_1.txt
    Desktop    Movies     Public     sigquotes  what_list
    Documents  Music      Sites      temp.html  who_list
    [localhost:~] dr_unix% ls -l wh*
    -rw-r--r--  1 dr_unix  staff  243 Apr  3 09:51 what_list
    -rw-r--r--  1 dr_unix  staff  243 Mar 18 10:47 who_list
    [localhost:~] dr_unix% 
    

We can see that the file was created at the proper time and has the same size. cp has some useful options, so check the man page (man cp) if your need is not quite what you had in mind--

Oh, you didn't want to copy that file? No problem. To get rid of a file, simply use the rm command. (Mnemonic: short for 'remove') Like most Unix shell commands, it is straightforward: rm file1 file2 file3 ...

    [localhost:~] dr_unix% ls w*
    what_list who_list
    [localhost:~] dr_unix% rm what_list
    [localhost:~] dr_unix% ls w*
    who_list
    [localhost:~] dr_unix% 
    

If you want to delete a directory and all its contents, you can use the '-r' option. This is not recommended; you can do yourself a mischief, as you can well imagine. If you do want to remove an empty directory, but don't want to accidentally remove any files that may be there but forgotten, use the rmdir command instead. (Mnemonic: short for 'remove directory'.)

    [localhost:~] dr_unix% ls -ld t*
    -rw-r--r--  1 dr_unix  staff   0 Mar 25 19:35 temp.html
    -rw-r--r--  1 dr_unix  staff  15 Mar 18 10:47 test_1.txt
    drwxr-xr-x  3 dr_unix  staff  58 Apr  7 15:19 test_dir
    [localhost:~] dr_unix% rmdir test_dir
    rmdir: test_dir: Directory not empty
    [localhost:~] dr_unix% ls test_dir
    temp.html
    [localhost:~] dr_unix% rm test_dir/temp.html
    [localhost:~] dr_unix% rmdir test_dir
    [localhost:~] dr_unix% ls -ld t*
    -rw-r--r--  1 dr_unix  staff   0 Mar 25 19:35 temp.html
    -rw-r--r--  1 dr_unix  staff  15 Mar 18 10:47 test_1.txt
    [localhost:~] dr_unix% 
    

And if you want to make a directory, there is, of course, a command for this, mkdir. (Mnemonic: short for, you guessed it, 'make directory'.)

    [localhost:~] dr_unix% ls -ld t*
    -rw-r--r--  1 dr_unix  staff   0 Mar 25 19:35 temp.html
    -rw-r--r--  1 dr_unix  staff  15 Mar 18 10:47 test_1.txt
    [localhost:~] dr_unix% mkdir test
    [localhost:~] dr_unix% ls -ld t*
    -rw-r--r--  1 dr_unix  staff   0 Mar 25 19:35 temp.html
    drwxr-xr-x  2 dr_unix  staff  24 Apr  7 15:30 test
    -rw-r--r--  1 dr_unix  staff  15 Mar 18 10:47 test_1.txt
    [localhost:~] dr_unix% rmdir test
    [localhost:~] dr_unix% ls -ld t*
    -rw-r--r--  1 dr_unix  staff   0 Mar 25 19:35 temp.html
    -rw-r--r--  1 dr_unix  staff  15 Mar 18 10:47 test_1.txt
    [localhost:~] dr_unix% 
    

If you want to rename a file, you could, I suppose, do a cp to the new name and then rm the original file. However, that would be silly, wasteful, and Not The Unix Way. (That is, you shouldn't have to type two commands when you can make do with one.) mv is the command for renaming/moving a file. (Mnemonic: short for 'mv'. See a pattern forming?)

    [localhost:~] dr_unix% ls t*.html
    temp.html
    [localhost:~] dr_unix% mv temp.html temp_1.html
    [localhost:~] dr_unix% ls t*.html
    temp_1.html
    [localhost:~] dr_unix% mv temp_1.html temp.html
    [localhost:~] dr_unix% ls t*.html
    temp.html
    [localhost:~] dr_unix% 
    
"But how do I view the contents of a file?" I hear you ask. "I will tell you," I hear myself reply. There are two commands (well, three) that let you do this. The first is cat, which concatenates and prints the files to the screen, cat file1 file2 .... This is fine when you are looking at a small file, or want to have a series of files viewed as one. If there are more lines in the file than lines on the screen, and there often are, more is the proper choice. more file1 file2 file3 ... It prints one screenful of a file at a time.
    [localhost:~] dr_unix% more test_1.txt
    This is a test.
    [localhost:~] dr_unix% 
    

You can type man more to see all the intricacies, but you can go a long way with 'q' to quit, 'b' to move back a screen, and ' ' to advance one screen's worth.

less is similar to more. (All "less is more" jokes may be sent elsewhere. I've heard them before.) less allows backward movement in the file in addition to forward movement. Also, less does not have to read the entire file before it starts its output, so on really large files, you don't have to wait as long. less' options are slightly different from more's, so consult your local man page for more information.

If you are interested in only seeing the start or end of a file, head and tail will do the trick. The commands are in the form:

    head -n file
    tail -n file
    

where 'n' is the number of lines at the top/bottom that you wish to see. (If no number is given, both default to ten lines.)

    [localhost:~] dr_unix% cat who_list
    10:47AM  up 20:34, 3 users, load averages: 0.19, 0.10, 0.00
    USER    TTY FROM              LOGIN@  IDLE WHAT
    dr_unix  co -                Sun02PM 20:33 -
    dr_unix  p1 -                 9:50AM     0 -
    dr_unix  p2 -                10:29AM     0 -
    [localhost:~] dr_unix% head -2 who_list
    10:47AM  up 20:34, 3 users, load averages: 0.19, 0.10, 0.00
    USER    TTY FROM              LOGIN@  IDLE WHAT
    [localhost:~] dr_unix% tail -1 who_list
    dr_unix  p2 -                10:29AM     0 -
    [localhost:~] dr_unix% 
    
tail does have one handy option that is worth mentioning. Let's say you have a process running which is generating a log file, 'logfile.txt'. (A web server comes to mind.) You want to see how the process is progressing and watch it live. tail -f logfile.txt will not stop when the end of the file is reached; rather, it will wait until additional data is appended to the file, then print it to the screen. And when you want it to stop, just hit 'control-c'. Neat, huh? (There is also a -F option that is similar but more powerful; I've never needed its power, though.)

In this example, head and tail were not too useful. After all, the file was only five lines long. If you want to know this beforehand, wc (word count) does this. wc requires one of three flags. wc -l counts the number of lines in a file, wc -w the number of words, and wc -c the number of characters. If the command is given a list of files, it tells the size for each file and gives a grand total.

    [localhost:~] dr_unix% wc -l who_list
           5 who_list
    [localhost:~] dr_unix% wc -c who_list
         243 who_list
    [localhost:~] dr_unix% wc -w who_list
          34 who_list
    [localhost:~] dr_unix% wc -l t*
           0 temp.html
           1 test_1.txt
           1 total
    [localhost:~] dr_unix% 
    

Now that we've seen how to manipulate files, we are nearly ready for some heavy-duty Unix wizardry. First, we must look at the shell's different forms of quotes, which will be the next column.

You are encouraged to send Richard your comments, or to post them below.


Most Recent Mac OS X Command Line 101 Columns

Command Line History & Editing Your Commands
November 22nd

Pico: An Easy To Use Command Line Editor
November 1st

Understanding The "grep" Command In Mac OS X
October 4th

Command Line History & Editing Your Commands
September 6th

Mac OS X Command Line 101 Archives

Back to The Mac Observer For More Mac News!


Richard Burton is a longtime Unix programmer and a handsome brute. He spends his spare time yelling at the television during Colts and Pacers games, writing politically incorrect short stories, and trying to shoot the neighbor's cat (not really) nesting in his garage. He can be seen running roughshod over the TMO forums under the alias tbone1.



Today's Mac Headlines

[Podcast]Podcast - Apple Weekly Report #135: Apple Lawsuits, Banned iPhone Ad, Green MacBook Ad

We also offer Today's News On One Page!

Yesterday's News

 

[Podcast]Podcast - Mac Geek Gab #178: Batch Permission Changes, Encrypting Follow-up, Re-Enabling AirPort, and GigE speeds

We also offer Yesterday's News On One Page!

Mac Products Guide
New Arrivals
New and updated products added to the Guide.

Hot Deals
Great prices on hot selling Mac products from your favorite Macintosh resellers.

Special Offers
Promotions and offers direct from Macintosh developers and magazines.

Software
Browse the software section for over 17,000 Macintosh applications and software titles.

Hardware
Over 4,000 peripherals and accessories such as cameras, printers, scanners, keyboards, mice and more.

© All information presented on this site is copyrighted by The Mac Observer except where otherwise noted. No portion of this site may be copied without express written consent. Other sites are invited to link to any aspect of this site provided that all content is presented in its original form and is not placed within another .