转载自:http://bradthemad.org/tech/notes/cpio_directory.php
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.
(In case the man-page wasn't clear enough)
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
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
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
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