Westwind Computing
 206-522-3530
 Sales Information
 Customer Feedback
Westwind Computing
[New to Westwind?]  [Customers]  [News & Information] 



Reference


OS X's BSD/unix command-line

File & Folder Manipulation Commands

By Gordon Davisson

Copyright (c) 2002, Westwind Computing inc.

Index:


Manipulating Files and Folders:

cp   - Copy a file (or directory).

Notes:
  • The cp command does not understand all of the characteristics of files on HFS+ partitions, and will not completely copy files that have resource forks, type&creator codes, or other Mac-specific characteristics. To fully copy such files, use the Finder, or the ditto -rsrcFork command.
  • If a file already exists under the name given for the copy, it will be deleted (and replaced with the copied file) without any warning or chance of recovery.
  • If a folder exists under the name given for the copy, the copy will be placed inside that directory, with the same name as the original file.
Examples:
cp foo bar
copy a file named "foo" (in the current directory); name the copy "bar"
cp foo ~/Documents
copy a file named "foo" (in the current directory) into your Documents directory
cp foo ~/Documents/bar
copy a file named "a" (in the current directory) into your Documents directory and name the copy "bar"
cp *.jpg ~/Documents
copy all files with names ending in ".jpg" into your Documents directory
cp -R Documents "Documents backup"
copy an entire directory named "Documents"; name the copy "Documents backup". The quotes are needed because of the space in the directory name.
sudo cp -Rp /Users "/Users backup"
copy the entire /Users directory (including all of the user home folders inside it), preserving as much as possible of the files' information (ownership, permissions, etc, but not resource forks) as cp knows how to; name the copy "Users backup". Root access is required to use -p, so the example uses sudo to get root access temporarily.

CpMac   - Copy a Mac file (or directory). This command is very similar to cp, but with different options.

Note:
  • The CpMac command is not installed as part of the standard installation of Mac OS X, but is installed with the optional Developer Tools disk. Also, since it's not placed in one of the standard binaries directories, it won't be available from the command line unless you either add /Developer/Tools to your PATH, or refer to it explicitly as /Developer/Tools/CpMac
  • CpMac has a -p option, documented as doing the came thing as cp's -p option, but in practice it doesn't preserve the file's ownership (user ID and group ID), even when run with root access.
Examples:
/Developer/Tools/CpMac foo bar
copy a file named "foo" (in the current directory); name the copy "bar"
/Developer/Tools/CpMac foo ~/Documents
copy a file named "foo" (in the current directory) into your Documents directory
/Developer/Tools/CpMac foo ~/Documents/bar
copy a file named "a" (in the current directory) into your Documents directory and name the copy "bar"
/Developer/Tools/CpMac *.jpg ~/Documents
copy all files with names ending in ".jpg" into your Documents directory
/Developer/Tools/CpMac -r Documents "Documents backup"
copy an entire directory named "Documents"; name the copy "Documents backup". As with the example under "cp", the quotes are needed because of the space in the directory name. Note that with CpMac, the -r option is lowercase, while cp takes it capitalized.
/Developer/Tools/CpMac -r -p Documents "Documents backup"
as above, copy the entire directory named "Documents" to "Documents Backup"; this time preserving additional file information (permissions, timestamps, etc, but not ownership). Note that CpMac doesn't allow multiple options after a single -, so -r -p must be used instead of just -rp.

ditto   - copy a directory, preserving many characteristics of the enclosed files. If used with root access, it also preserves security information (e.g. file ownership), very much like cp -Rp.

Note:
  • Unlike cp -R, with ditto if a directory by the destination name already exists, the existing contents will be merged with the contents of the directory being copied.
Examples:
ditto Documents "Documents backup"
copy an entire directory named "Documents"; name the copy "Documents backup". As with previous examples, the quotes are needed because of the space in the directory name.
ditto -rsrcFork Documents "Documents backup"
as above, but preserving the files' Mac-specific characteristics, such as resource forks, type and creator codes, etc.

cp, CpMac, and ditto all perform very similar functions, but with different limitations. Here's a table of capabilities to (hopefully) clarify which command can do what:
Command: cp CpMac ditto
Can copy single files: yes yes no
Can copy entire directories: yes (-R) yes (-r) yes
Can preserve resource forks, type&creator: no yes yes (-rsrcFork)
Can preserve ownership, permissions: yes (-p) no yes
Included with standard OS-X install: yes no yes

mv   - Move or rename a file or folder.

Notes:
  • Unlike cp, themv command does not lose resource forks and such on the files it manipulates; it just moves the file, it doesn't need to know much about the file itself.
  • As with cp, if a file already exists under the new name/location, it will be deleted (and replaced with the copied file) without any warning or chance of recovery.
  • If a folder exists under the new name/location, the file will be placed inside that directory, with the same name as it had originally.
Examples:
mv foo bar
rename a file (in the current directory) from "foo" to "bar"
mv foo ~/Documents
move the file "foo" from the current directory to your Documents directory
mv foo ~/Documents/bar
move the file "foo" from the current directory to your Documents directory and rename it "bar"
mv *.jpg ~/Documents
move all files with names ending in ".jpg" from the current directory to your Documents directory

rm   - Remove (delete) a file or directory.

Examples:
rm foo
delete the file named "foo"
rm a*
delete all files with names beginning with "a"
rm *.jpg
delete all files with names ending in ".jpg"
rm -R temp
delete the directory named "temp", and all of its contents

rmdir   - Remove directory - delete an empty directory. If you want to delete a directory that isn't empty, you either need to delete the contents first, or use the rm -R command instead.

mkdir   - Make directory - equivalent to the Finder's New Folder command.

Example:
mkdir foo
Create a new directory named "foo"

chmod   - Change protection mode on files and folders. It's rather complex, so read the man page before using. You must own the files (or be root) to change their protections.

Examples:
chmod u+w foo
Allow the user (file owner) write access to the file or folder named "foo"
chmod u-r foo
Disallow the user (file owner) read access to "foo"
chmod ug+x foo
Allow the user (file owner) and group members execute access to "foo"
chmod o+rX *
Allow "other"s read and maybe execute access to all files in the current directory. The capital "X" tells chmod to use a complicated set of rules for setting execute access only where it's appropriate; it generally works fairly well.
chmod o=rw foo
Allow "other"s read and write access, but disallow execute to "foo"
chmod -R ugo+rX ~/Documents
Allow everyone (user, group, and other) read and execute access to your Documents directory and everything in it (the -R means change permissions for the folder's entire contents, not just the folder itself)
chmod o-rwx ~/Public
Disallow "other"s (mainly guests) from accessing your Public directory

chflags   - Change a file or folder's flags. These flags are supported:

arch   - the archived flag
opaque   - the opaque flag
nodump   - the nodump flag
sappnd   - the system-controlled append-only flag
schg   - the system-controlled immutable flag
uappnd   - the user append-only flag
uchg   - the user immutable flag (on files, this is equivalent to locking the file in the Finder's Show Info box).
Most flags require root access to set or clear; the uappnd and uchg flags can also be controlled by the file's owner; the sappnd and schg flags cannot be removed (even by root) except in single-user mode. To remove a flag, use "no" in front of the flag's name (this can be a bit confusing - nouchg means the file can be changed, while uchg means it cannot).
Examples:
chflags uchg foo
Lock the file or folder named "foo" against changes
chflags uappnd foo
Make the file "foo" append-only (i.e. data can be added to the end of the file, but once added it cannot be deleted or changed - this can be useful for things like log files)
chflags -R nouchg ~/Documents
Unlock your Documents directory and everything in it

chown   - Change the owner and/or group of a file or folder. You must be root to use this command.

Examples:
sudo chown eva /Users/Shared/meeting-notes.txt
Switch to root (see sudo) and assign eva as the owner of the "meeting-notes.txt" file in the Shared directory.
sudo chown -R eva:staff /Users/Shared/tmp/
Assign eva as the owner and staff as the group for the "tmp" directory, and everything in it (the -R means change ownership/group for the folder's entire contents, not just the folder itself)
(See find for an example of using chown to reassign all of one user's files to another user)

chgrp   - Change the group of a file or folder. You must own the file, and can only assign to groups you're a member of (unless you're root).

Examples:
chgrp staff /Users/Shared/meeting-notes.txt
Assign staff as the group of the "meeting-notes.txt" file in the Shared directory.
chgrp -R staff /Users/Shared/tmp/
Assign staff as the group for the "tmp" directory, and everything in it (the -R means change group for the folder's entire contents, not just the folder itself)
Home | Specials | Products | Education | About Us | Services | Training | Support | Reference | Events | How-to
   Copyright © 2000-2004 Westwind Computing