DirectNET

Data Center Management Solutions including UPS Systems, Data Center Cooling, KVM over IP & IP Power Strips, Server Racks and Server Rack accessories; KVM Switches and KVM Extenders; Rackmount Monitors and Rackmount Keyboards.


NAVIGATION
Home
Store
INSIDE MAC
Television Shows
Broadcast Shows
Daily News Shows
Special Shows
EVENTS
DAILY TIPS
Design
Mac OS X
Mac OS X UNIX
COMMUNITY
Forums
Surveys
NEWS
Current
Press
Archive
FEATURES
Editorial
Dr. Mac
Reviews
Reader Reports
RESOURCES
FAQ
Documentation
Learning Center
MAN pages
Glossary
Tutorials
Tips
Links

OUR PARTNERS

OS X | UNIX

back

Unix

Mac OS X Unix Tutorial

by Adrian Mayo - Senior Editor for Mac OS X Unix, Janice Mayo - Senior Editor for Mac OS X Unix

Part 3 - Playing With Directories (page 1 of 2)

The Story So Far

Part two covered the basic Unix commands needed to manage files, introducing 'cp' to copy; 'mv' to move and rename; 'rm' to remove; and 'cat, 'more', and 'less' to view files.

Time now to launch the Terminal again - which I'm sure you will have sitting in the Dock together with all your other favourite applications :-). In this episode I shall concentrate on commands to make, move, and remove directories.

And if you have not done so already, take a look at the menu bar, Terminal -> Window Settings... (Preferences... pre 10.2) and configure it to your liking. I would recommend a bigger 80 characters by 50 lines window with a large (or unlimited) scroll-back buffer.

Issue this command if you would like an transparent Terminal (OS X 10.2 has transparancy as an option in Terminal Preferences):

% defaults write com.apple.terminal TerminalOpaqueness 0.85

But first off I will tackle 'push' and 'pop', casually left hanging at the end of Part Two.


There and Back Again

If you've ever changed directory for a few commands and wished you could simply pop back from whence you came - well you can.

Instead of using 'cd' to change directory use 'push', which does exactly as 'cd' and also remembers the way back.

For example, we start off in some deeply nested directory and then move to ~ using 'pushd':

% cd ~/Sites/Tutorials/LearningCenter/UnixTutorials/LoseTheMouse/
% pwd
/Users/melkor/Sites/Tutorials/LearningCenter/UnixTutorials/LoseTheMouse
...
% pushd ~
~ ~/Sites/Tutorials/LearningCenter/UnixTutorials/LoseTheMouse
% pwd
/Users/Melkor

'pushd' remembers where we came from, and just to prove so displays both the new and old directories.

To get back to ~/Sites/..... simply issue a 'popd':

% popd
~/Sites/Tutorials/LearningCenter/UnixTutorials/LoseTheMouse
% pwd
/Users/melkor/Sites/Tutorials/LearningCenter/UnixTutorials/LoseTheMouse

'pushd' can be used many times in succession, and each time it stacks another stepping stone to the way home.

The command 'dirs' displays the current directory stack.

'popd' moves back down the directory stack one step at a time.

 
Tell Me More...

Stack Tricks

To print the directory stack one entry per line type:

% dirs -v

'pushd' and 'popd' also take option'-v'.

If you do not wish 'pushd' and 'popd' to display the directory stack each time, this can be arranged with:

% set pushdsilent

To find out more about the directory stack, view the tcsh manual with:

% man tcsh

then while viewing the man page search for information on a command such as 'pushd' by typing:

/pushd

Make a New Directory - mkdir

Now to get down to the business of playing with directories.

First, make sure you are in your home directory:

% cd ~

Now create a new directory called new-dir:

% mkdir new-dir
% ls
Desktop      Documents  Movies  Pictures  Shared  new-dir
Development  Library    Music   Public    Sites   osxfaq
% ls -al new-dir/
total 0
drwxr-xr-x   2 melkor   staff          24 Jul 15 16:09 .
drwxr-xr-x  24 melkor   staff         772 Jul 15 16:09 ..

'new-dir' will be empty except for the dot and dot-dot entries which are always present and created automatically.

That's all there is to creating new directories!

Now create a file in new-dir - we'll need it for the next part:

% touch new-dir/file

Copy a Directory - cpdir? cp?

Let's give it a try:

% cpdir new-dir new-dir-2
cpdir: Command not found.

Mmmm?

% cp new-dir new-dir2
cp: new-dir is a directory (not copied).

Oh? The secret to copying directories is to do it recursively. We will consider recursion on page two of this tutorial, and wildcards too. Only then will we be fully equipped to 'Play with Directories'.

Remove a Directory - rmdir

Directory 'new-dir' is just so last minute. Let's remove it:

% rmdir new-dir
rmdir: new-dir: Directory not empty

Oops! First point to note. A directory must be empty before it can be removed. So let's remove 'file' then try to delete:

% rm new-dir/file
% rmdir new-dir

Having to empty directories before we remove them is going to prove a pain. Again, recursion and wildcards will come to our rescue on page two.

Move or Rename a Directory - mv

The 'mv' command used to move files also moves directories. A directory can be renamed or moved to within another directory. When a directory is moved, all nested directories are moved too and remain nested.

This example shows moving and renaming directories:

% cd ~
% mkdir -p one/too/for/three

Rename 'too' to 'two' but don't move the directory:

% mv one/too one/two

Switch 'three' and 'for' around, renaming 'for'. First move 'three' from within 'for' to within 'two'. 'for' remains where is was:

% mv one/two/for/three one/two

Second move 'for' from within 'two' to within 'three' and at the same time rename it to 'four'

% mv one/two/for one/two/three/four

Now lets see what we have:

% ls -R one
one:
two

one/two:
three

one/two/three:
four

one/two/three/four:

Everything in order. Notice the use of -R, which has caused 'ls' to recursively list the nested directory structure.

Making a Nest

To create a number of nested directories with a single command use option '-p' with 'mkdir':

mkdir -p a/b/c/d

Without '-p' it is necessary to make a, and then b, then c, then finally d.

Removing the Nest

It is possible to remove nested directories, provided that each is empty:

rmdir -p a/b/c/d

As With Files

Remember that directories behave much as files do.

You can use auto completion (hitting tab to complete a path name).

Directory names may start with dot and will then be hidden by 'ls'.

'ls -al' Frustrations

If you wish to view the details of a directory, and try:

% ls -l directory

you will find yourself listing the contents of the directory, not the details of the directory itself. To prevent this use option '-d':

% ls -ld directory

Trailing '/' Frustrations

The 'mv' command seems to be a little fussy when it comes to trailing a '/' at the end of a directory name. You must differentiate between renaming and moving.

For example, renaming 'too' to 'two' with:

% mv one/too one/two/

fails. 'mv' assumes you wish to move the file or directory 'one/too' into the directory 'one/two/' keeping the name 'too', such that 'one/two/too' is formed. The trailing slash says to treat 'one/two/' as a directory. To actually rename it use:

% mv one/too one/two

Now 'mv' treats 'one/two' as a file and renames as expected.


Next Page

Page two will explain recursion, and how to copy directories and remove non-empty directories


previous

Part 3 - Playing With Directories (page 1 of 2)

next

Copyright © 2000-2009 Inside Mac Media, Inc. All rights reserved.
Apple assumes no responsibility with regard to the selection, performance, or use of the products or services. All understandings, agreements, or warranties, if any, take place directly between the vendors and prospective users.
Apple, the Apple logo, Mac, PowerMac G4, PowerMac G5, Xserve, Xserve RAID, PowerBook, iBook, Airport, AirPort Extreme, iMac, eMac, iLife, iMovie, iCal, iPhoto, iTunes, QuickTime, FireWire, iPod, iSight, AppleWorks, Macintosh, Jaguar, Panther, Mac OS, Mac OS X and Mac OS X Server are trademarks of Apple Computer, Inc.