System Administration Guide

Copying Directories Between File Systems using the cpio Command

You can use the cpio (copy in and out) command to copy individual files, groups of files, or complete file systems. This section describes how to use the cpio command to copy complete file systems.

The cpio command is an archiving program that takes a list of files and copies them into a single, large output file. It inserts headers between the individual files to facilitate recovery. You can use the cpio command to copy complete file systems to another slice, another system, or to a media device such as tape or diskette.

Because the cpio command recognizes end-of-media and prompts you to insert another volume, it is the most effective command (other than ufsdump) to use to create archives that require multiple tapes or diskettes.

During cpio operations, you frequently use commands like ls and find to list and select the files you want to copy and then pipe the output to the cpio command.

How to Copy Directories Between File Systems (cpio)

  1. Become superuser.

  2. Change to the appropriate directory.


    # cd /filesystem1
    
  3. Copy the directory tree using a combination of the find and cpio commands.


    # find . -print -depth | cpio -pdmu /filesystem2
    

    .

    Starts in the current working directory. 

    -print

    Prints the file names. 

    -depth

    Descends the directory hierarchy and prints file names on the way back up. 

    -p

    Creates a list of files. 

    -d

    Creates directories as needed. 

    -m

    Sets the correct modification times on directories. 

    The files from the directory name you specify are copied, and symbolic links are preserved.

    You may also specify the -u option. This option forces an unconditional copy. Otherwise older files will not replace newer files. This may be useful if an exact copy of a directory is desired, and some of the files being copied may already exist in the target directory.

  4. If appropriate, remove the source directory.


    # rm -rf /filesystem1
    
  5. Verify the copy was successful by displaying the destination directory contents.


    # cd filesystem2
    # ls
    

Example--Copying Directories Between File Systems (cpio)


# cd /data1
# find . -print -depth | cpio -pdm /data2
19013 blocks
# cd /data2
# ls
# rm -rf /data1
# cd filesystem2
# ls

See cpio(1) for more information.