Copy a directory with cpio

本文介绍了如何利用cpio工具进行文件及目录的备份、压缩、提取等操作,并提供了多种实用场景下的具体命令实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载自: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

转载于:https://www.cnblogs.com/lbsx/archive/2010/12/26/1917041.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值