04. Using cpio

Another command similar to tar is cpio. This program allows you to create archives from lists of filenames. Additionally, it is used to extract tar archives and copy files from a source to a device or file.

The cpio command is especially useful since you don't need to create an intermediate file before moving it to another disk.

Three modes

There are three modes that come with the cpio command.

1) Copy-out mode

The copy-out mode, denoted with the -o or --create options, allow you to make an archive and copy files into it. Simply pass in a list of filenames (one per line) into the standard input.

An easily way to generate a list of filenames is with the find command.

$ find ./sample
./sample
./sample/file1
./sample/file2
./sample/file2
$ find ./sample | cpio -o > /media/myusb/sample.cpio
1 block

This is great, but the file is uncompressed. To compress it, use gzip.

$ find ./sample | cpio -ov gzip > /media/myusb/sample.cpio.gz

Other options you may use include -v for verbose and -depth to specify folder depth.

2) Copy-in mode

Whereas copy-out mode creates an archive, we can use copy-in to extract the archive contents. Simply pass in the -i or --extract option, and pass in the archive through standard input.

$ cpio -i < /media/myusb/sample.cpio
# If compressed:
$ gunzip -c /media/myusb/sample.cpio.gz | cpio -i

The -c option of gunzipoutputs the contents to standard out.

3) Copy-pass mode

The third mode is copy-pass, which is useful for moving files from one directory tree to another without creating an intermediate archive. This mode can be thought of as a combination of the two mentioned above. To activate copy-pass mode, use -p or the --pass-through option.

More options

Here is a list of more options you can use with the cpio command.

-a|--reset-access-time
Reset the access time of each file so it doesn't appear to have been read.
-A|--append
For use with copy-out mode - appends data to an existing archive.
-F|--file=filename
Archive the filename instead of using the default standard in.
-I filename
Use specified filename instead of standard in.
--no-absolute-filenames
Extract relative to the current directory, even if a full pathname is given.
-O filename
Use specified filename instead of default standard out.
-u|--unconditional
Replace all files without asking for verification.

Take your Linux skills to the next level!

The Linux Command Line

Take your Linux skills to the next level! Try Linux & UNIX

The Linux Command Line takes you from your very first terminal keystrokes to writing full programs in Bash, the most popular Linux shell. Along the way you'll learn the timeless skills handed down by generations of gray-bearded, mouse-shunning gurus: file navigation, environment configuration, command chaining, pattern matching with regular expressions, and more.

$ Check price
39.9539.95Amazon 4.5 logo(274+ reviews)

More Linux & UNIX resources

Aching back from coding all day?

Prism Glasses

Aching back from coding all day? Try Back Problems

Ever feel achy from sitting crunched up on your computer table? Try lying down with these optical glasses that allow you to work on your laptop while lying flat on your back. This is the perfect solution with those with limited mobility or those who wish to prevent neck cramps and back strains.

$ Check price
4.454.45Amazon 4 logo(128+ reviews)

More Back Problems resources

Ad