Go to main content

Managing File Systems in Oracle® Solaris 11.4

Exit Print View

Updated: November 2020

Copying Directories Between File Systems Using 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 copies a list of files into a single, large output file. This command 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.

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 create archives that require multiple media.

With the cpio command, you can use the ls and find commands to list and select the files you want to copy, and then to pipe the output to the cpio command.

How to Copy Directories Between File Systems Using the cpio Command

  1. Change to the appropriate directory.
    # cd filesystem1
  2. Copy the directory tree from filesystem1 to filesystem2 by using a combination of the find and cpio commands.
    # find . -print -depth | cpio -pdm filesystem2

    Prints the file names


    Descends the directory hierarchy and prints file names from the bottom up


    Creates a list of files


    Creates directories as needed


    Sets the correct modification times on directories

    The files from the directory name that is specified are copied. The symbolic links are preserved.

    You can also specify the –u option to perform unconditional copy. Otherwise, older files do not replace newer files. This option might be useful if you want an exact copy of a directory, and some of the files being copied might already exist in the target directory.

    For more information, see the cpio(1) man page.

  3. Verify that the copy was successful by displaying the contents of the destination directory.
    # cd filesystem2
    # ls
  4. If required, remove the source directory.
    # rm -rf filesystem1
Example 18  Copying Directories Between File Systems Using the cpio Command

This example shows how to copy directories between file systems using the cpio command.

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