Copying and Moving Directories
Perhaps the most basic of all system administration
of copying and moving files and directories. This article
three ways of copying/moving directories. The first
two are basic
"one-liners" using tar and cpio; the third
is a move directory shell script, mvdir, that uses the
move command, mv.
The tape archive command, tar, can be used to copy all
and directories from one location to another. To copy
all files and
directories from under /usr/ed to /usr/fred, type:
cd /usr/ed; tar cf - .|(cd /usr/fred; tar xf - )
This command changes to the source directory and creates
archive. The "f" option with a dash means
use standard output
and the period includes the current directory in the
this command is piped, the standard output of the archive
the standard input of the tar extraction creating the
copy. The parenthesis groups commands before the tar
The copy input-output command, cpio, can also be used
cd /usr/ed; find . -depth -print|cpio -pd /usr/fred
The pass mode, "-p," of cpio allows a list
piped from the standard input to be copied to the target
/usr/fred. Be sure to change to the source directory
do a find. A find from root such as
find /usr/ed -depth -print...
will create a directory structure under /usr/fred
with the full path of the source directory. For example,
a file /usr/ed/sample
would be moved as /usr/fred/usr/ed/sample.
The mvdir Script
As useful as these one-liners are, they are strictly
a copy and not
a true move. With the advent of SVID release 2, the
UNIX move command,
mv, supports moving directories:
mv srcdirectory destdirectory
where each argument is a directory and "destdirectory"
does not exist. The mvdir script takes advantage of
The pseudo-code for mvdir is as follows:
1. If the destination directory exists, assume
the source directory basename moves to the destination
keeping the same name.
2. If the destination directory does not exist,
assume the source directory basename moves to the the
3. If the source and destination directories do
not exist on the same file system, perform a copy. If
the source and
destination directories do not exist on the same file
cannot be moved across file systems.
Source Code Explanation
Lines 19 through 23 check if the source directory is
being moved to
itself. For example, mv does not allow the following
mv . destdir
If the destination directory exists, the script appends
the basename directory to the destination directory
(lines 32 through
If the destination directory does not exist, the awk
(lines 36 through 60) returns either
a) the string NO-PATH if no slashes are found,
signifying a directory name with no path, or
b) the full path of the directory name minus the basename.
For example, if the destination directory were /usr/eds/doc,
the string /usr/eds would be returned.
The awk script splits the destination string into an
and rebuilds the directory string up to the last directory
41 through 55). (Note: awk array usage is non-conventional;
see the sidebar "Arrays in awk," for more
If the full path string is neither a directory nor a
an error returns (lines 61 through 65).
If the destination directory is a file or a directory,
an error returns
(lines 68 through 72).
If the source directory shares a common path with the
directory, an error returns (lines 74 through 79).
If the first character of the source and destination
not a slash, the script assumes this is a valid move
(lines 81 through
To check for a possible file system move, the code finds
file systems of the source and destination directories,
if those are
not provided (lines 97 through 101).
The awk script (lines 102 through 112) returns a 1 if
directories are on different file systems, else a zero.
of the code fails if there is a link between the file
system and another
directory. For example, if /apps is linked to /jet
and you attempt to mvdir /usr/eds/doc/some.dir to /apps/some.dir,
the awk script cannot determine that /apps and /usr
are separate file systems.
If the file systems are different, the script changes
to the source
directory, makes the destination directory, and then
copies the source
directory to the destination directory (lines 116 through
the file systems are the same, the script performs a
Copying files and directories is simplified by using
or cpio one-liners. If a copy is not sufficient, use
Prata, Stephen, and Donald Martin. UNIX System
V Bible: Commands and Utilities. The Waite Group, 1987.
Aho, Alfred, Brian Kernighan, and Peter Weinberger.
The Awk Programming Language. Addison-Wesley, 1988.
About the Author
Ed Schaefer is an Informix software developer and UNIX
administrator at jeTECH Data Systems of Moorpark, CA,
where he develops
Time and Attendance Software. He has been involved with
UNIX and Informix
since 1987 and was previously head of software development
Callendar Pie Shops of Orange, CA.