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
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

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

Lets Go Wild With Star!

Suppose I wish to list all files in my directory that have an extension of '.txt'. I want to tell 'rm' to remove files where the first part is any number of any characters, followed by '.txt'

The symbol '*' in Unix means just that: any number including zero of any characters. It is termed a wildcard

For example:

% ls -a
.  ..  .txt  f1.txt  f2.txt  txt  txt.f3  txt.f4

% rm *.txt
% ls -a
.  ..  .txt  txt  txt.f3  txt.f4

% rm txt.*
% ls -a
.  ..  .txt  txt

And:

% rm *

will remove all files.

Remember that you can use the '-i' option with 'rm', causing 'rm' to prompt for each file to be removed and hopefully avoiding accidentally removing all files from the wrong directory.

Other Wildcards

The character '?' matches any one character.

rm test?

will remove 'test1', 'testa', and 'test$'. It will not remove 'test' or 'test12'.

Enclose characters in '[ ]' to match any one of a specific list of characters.

% ls b[aeiou]g

will list 'bag', 'beg', 'big', 'bog', and 'bug'; but not 'bfg' or 'bags'.

To match any one of a range of characters, separate a pair of characters with '-'.

[a-z] matches any lowercase letter.
[0-9] matches any digit.

 
Tell Me More...

Dot Files

Notice that 'rm *.txt' does not remove '.txt', although it should do. There is a special rule that says leading dots in filenames must be matched explicitly.

% rm .*

will remove all hidden files.

More Complex Globbing

[a-z0-9]

matches any lowercase letter or digit.

Glob patterns may be combined as in:

% ls [mt]*day[0-9]

to match filenames that start with 'm' or 't', have any number of intervening characters, and end in 'day' followed by one digit.

Filename Order

When a glob pattern is expanded into a number of filenames, they are presented in alphanumeric order.


Recursion

Recursion is when a command is applied not only to all files in the specified directory, but to all files in each nested directory too. The command works the entire directory structure rooting out every last file.

Compare:

% ls ~

with:

% ls -R ~

Many commands can operate recursively, usually by including option '-R' or '-r'.


Copy a Directory - cp

We can now make use of a recursive 'cp' to copy directories.

The last time we tried we got this:

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

Now we try again with a recursive copy:

% cp -r new-dir new-dir-2

This worked, and made a copy of the directory. It doesn't matter that new-dir is empty; we still must use a recursive copy. If new-dir were to contain many files and deeply nested directories, the entire directory structure and all files would be reproduced in new-dir-2.

Remove a Directory - rm

Previously we used 'rmdir' to remove a directory, but learnt that the directory had to be empty. Clearly a directory hierarchy containing many files and nested directories is going to be difficult to remove.

The task is made easy using wildcards and recursion.

% rm -i *

will remove all files in the current directory. I have included the '-i' interactive option as a safety net - remember it prompts you for each file removal.

To delete a directory completely, use:

% rm -ri new-dir

Zap! The whole directory, including all sub-directories, was removed.

Note that we used the 'rm' command rather than rmdir. 'rm' applied recursively will remove directories as it empties them. Without option '-r' rm will not remove directories.

You may omit option '-i', but beware that a command such as:

% rm -r *

issued at the top level of your home directory will delete every file in every directory that you own! Be warned!

 
Tell Me More...

warning

The 'cp' command, like all pure Unix commands, does not understand Mac resource forks.

Hence, 'cp' applied to files or recursively to directories will copy only the data fork, and strip resource forks from the copied files.

Ditto

'ditto' is a copy command that does understand resource forks. It copies files and directories, recursively, optionally preserving resource forks. The copy is a carbon copy of the original, including owners, permissions and time-stamps.

For example, to create a backup of /Users/you to a disc called 'Backup', one could use:

% ditto -rsrc /Users/you/ /Volumes/Backup/Users/you

rm -rf *

The option '-f' says to force remove - i.e. don't issue any warning for files that are not writeable - just delete them. Silent but deadly!

How Big is My Directory

To report on how much disc space a directory occupies use the disc usage command 'du'. To query your home directory use:

% du ~

This displays the size of every directory from your home directory down. The size of a directory includes the size of all directories within it. The last line displayed will be the total size of your home directory.

The size is reported in file-system blocks. Each block is 512 bytes or 1/2k. Take the answer you get and divide by 2 to get the size in k-bytes.

To report on how much disc space a directory occupies, but without displaying that information for every sub-directory, use the '-s' or summary option.

% du -sk ~

Will print a single line displaying the total size of your home directory, and in k-bytes too (requested by the '-k' option).

To display how full each disc and partition is, use the 'df' command:

% df -k

Option '-k' causes sizes to be displayed in k-bytes.

More 'ls' Tricks

% ls -aF

will display each file with a trailing character to indicate the file type.

/ means directory
* means executable
@ means a symbolic link (like an alias)

% ls -Fsk

will also display file sizes in k-bytes.

'file'

The command 'file' attempts to determine the type of a file - like:

executable
text
HTML
MPEG, JPEG


Command Synopsises

Key:
[ ] - optional part
directory... - 1 or more directories

mkdir [-p] directory ... - make new directories. '-p' allows one to make a nest of directories.
rmdir [-p] directory ... - remove (delete) empty directories. '-p' removes a nest of directories if all are empty
rm [-i] [-f] -R directory ... - remove a directory structure (all files and nested directories). '-i' for interactive. -'f' to force removal.

cp -R source_directory ... target_directory - copy the entire directory structure from source to target

du [-s] [-k] directory - report on disc usage for directory. '-s' for a summary. '-k' to report sizes in k-bytes.
df [-k] - display free disc space. '-k' to report sizes in k-bytes.

In Part 4

In the next part to this column I will cover Unix file permissions, multiple users, passwords, and Mr root.

In the meantime, try this:

% last

Enjoy :-)



Discuss this article in the Learning Center forum




previous

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

next

Copyright © 2000-2010 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.