9.2 Archiving

You can pass a list of files produced by find to a file archiving program. GNU tar and cpio can both read lists of file names from the standard input – either delimited by nulls (the safe way) or by blanks (the lazy, risky default way). To use null-delimited names, give them the ‘--null’ option. You can store a file archive in a file, write it on a tape, or send it over a network to extract on another machine.

One common use of find to archive files is to send a list of the files in a directory tree to cpio. Use ‘-depth’ so if a directory does not have write permission for its owner, its contents can still be restored from the archive since the directory’s permissions are restored after its contents. Here is an example of doing this using cpio; you could use a more complex find expression to archive only certain files.

find . -depth -print0 |
  cpio --create --null --format=crc --file=/dev/nrst0

You could restore that archive using this command:

cpio --extract --null --make-dir --unconditional \
  --preserve --file=/dev/nrst0

Here are the commands to do the same things using tar:

find . -depth -print0 |
  tar --create --null --files-from=- --file=/dev/nrst0

tar --extract --null --preserve-perm --same-owner \
  --file=/dev/nrst0

Here is an example of copying a directory from one machine to another:

find . -depth -print0 | cpio -0o -Hnewc |
  rsh other-machine "cd `pwd` && cpio -i0dum"