Copy a directory with cpio

转载自:http://bradthemad.org/tech/notes/cpio_directory.php

Copy a directory with cpio

Most people live their whole lives without ever using the cpio utility.However, it is a useful thing, and is sometimes better suited to copyinglots of files around than tar or cp. Here's a quick primer.

Some important flags

(In case the man-page wasn't clear enough)

-o
Copy out mode: Read in files from disk, output is a cpio stream or file, similar to "tar -c"
-i
Copy in mode: Read cpio archive file or stdin, extract as files to disk, similar to "tar -x"
-p
Pass-thru mode: Like piping cpio -o into cpio -i
-V
Verbose mode: Prints a dot for each file processed (all modes)
-O
Outfile: Specify an archive file rather than stdout (copy-out mode)
-d
Creates leading directories where needed when extracting files (copy-in and pass-thru modes)
-a
Resets atime on files being read, so the original files won't appear to have just been accessed (copy-in and pass-thru modes)
-m
Preserves original mtime on files being written out, so the copy will have the same timestamps as the original (copy-out and pass-thru modes)

Basic usage

Create a cpio archive:

localhost% find path/ -depth -print | cpio -oaV > archive.cpio
localhost% find path/ -depth -print | cpio -oaV -O archive.cpio

Create a cpio archive and compress it:

localhost% find path/ -depth -print | cpio -oaV | gzip -c > archive.cpio.gz

Extract a cpio archive:

localhost% cpio -imVd < archive.cpio
localhost% cpio -imVd -I archive.cpio

List the contents of a cpio archive:

localhost% cpio -it < archive.cpio
localhost% cpio -it -I archive.cpio

Use cpio copy-pass to copy a directory structure to another location:

localhost% find path/ -depth -print | cpio -pamVd /new/parent/dir

Pathnames

Avoid using pathnames starting with /, as that can be inflexible andpossibly messy when extracting an archive. Good practice is to change tothe directory above the one you're copying. That way, extracted files willgo into their own directory, rather than the current directory, much like awell-behaved source tarball. For example:

localhost% pwd
/usr/src/linux-2.4
localhost% cd ..
localhost% find linux-2.4/ -depth -print | cpio -oaV -O linux-2.4.cpio

cpio over ssh

To cpio a local directory, send the output to ssh and feed it to cpio ona remote host:

localhost% find path/ -depth -print | cpio -oaV | ssh user@host 'cpio -imVd'

Ssh to a remote host, cpio a remote directory, and get its outputlocally:

localhost% ssh user@host "find path/ -depth -print | cpio -oaV" | cpio -imVd

cpio and rpm

Ever wanted to extract files from an RPM package without installing it?It's easy. RPMv4 includes a utility called "rpm2cpio", which creates a cpiostream of files from a given RPM. You can pipe this into cpio just like aregular archive or stream.

List the included files:

localhost% rpm2cpio foo.rpm | cpio -it
./usr/bin/foo
./usr/share/man/man1/foo.1.gz
39 blocks

Note that the pathnames in the cpio stream begin with "./" -- this isimportant to know if you want to name specific files to extract.

Extract all files:

localhost% rpm2cpio foo.rpm | cpio -imVd
..
39 blocks

Extract only the manpage from that package:

localhost% rpm2cpio foo.rpm | cpio -imVd ./usr/share/man/man1/foo.1.gz

你可能感兴趣的:(Directory)